大家好,我是菜鸟,请教一个问题,我在SQL2005数据库中存储了图片的路径,对应的字段为imagePath,通过传递ID的值来调用图片,将其显示在页面上,一条路径下对应的图片有很多张,即路径中我没有存储图片名字,如存储路径为D:\My pictures\,路径下有image1.jpg;image2.jpg等多张图片,请问我要怎样才能就将图片显示出来呢,显示图片用哪个控件好呢,我打算用FromView,不知道是否可取

解决方案 »

  1.   

    那你就 手动 更具id  image+id 显示
      

  2.   

    FromView这是什么?
    绑定图片显示可以用repeater,里面套个img<img src='%# Eval("ImagePath")%> ' />
      

  3.   

    <img alt="" width="130px" height="110" src="My pictures/<%#Eval("imagePath").ToString()%>" />
      

  4.   

    应该用户相对路径
    <img src="My pictures/<%#Eval("imagePath").ToString()%>" />
      

  5.   

    问题补充:每条路径下的图片数可能不同,还要判断是否有图片,没有图片的情况是相应imagePath字段为空
      

  6.   

    那你就要先使用IO ,读取文件夹,并判断是否有文件,及文件格式.绑定时用上html控件<img>就可以了,上面有很多例了.另外,因为你存的是文件夹路径.所在在<%#Eval("path")+"文件名"%>后面必须加上图片的完整名称.
    这个名称可以在你刚才使用IO读取文件夹时 读取并保存下来.
      

  7.   

    从数据库读取出图片的路径,后赋给<img alt="" src="路径"/>这样就可以了
      

  8.   

    直接粘贴到load中测试吧:
    引入命名空间using System.Text.RegularExpressions和using System.IO;
            DirectoryInfo di = new DirectoryInfo(Server.MapPath("~\\res\\"));
            FileInfo[] fi = di.GetFiles();
            for (int i = 0; i < fi.Length; i++)
            {
                if (Regex.IsMatch(fi[i].Name.ToLower(), @".(bmp|png|jpg|jpeg|gif)"))
                {
                    Image img = new Image();
                    img.ImageUrl = fi[i].FullName;
                    Form.Controls.Add(img);
                }
            }
      

  9.   

    遍历下那个文件夹下的图片文件。然后再绑定到IMAGE
      

  10.   

    过节回来了,谢谢大家,14楼的朋友,我还是不太懂,那个("~\\res\\")是什么意思啊,测试时显示路径有问题,我存储的每一条记录中都包含了一条路径和一个ID,希望通过ID来传值检索
      

  11.   

    前台代码如下,不知道出错没?
    <asp:FormView ID="FormView3" runat="server">
                      <ItemTemplate>
                          <img alt="" src='<%#Eval("hPath") +"1.jpg" %>' style="width:90px; height:90px"/>
                      <img alt="" src='<%#Eval("hPath") +"2.jpg" %>' style="width:90px; height:90px"/>
                      </ItemTemplate>
                  </asp:FormView>
                  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=db1;Integrated Security=True"
                  SelectCommand="SELECT * FROM housesPics WHERE (ID = @ID)"  >
                  <SelectParameters>
                         <asp:QueryStringParameter Name="hID" QueryStringField="hID" Type="Int32" />
                     </SelectParameters>
                     </asp:SqlDataSource>
    后台要写代码吗?要怎样写啊
      

  12.   

    ~表示网站更目录
    res是网站更目录下的文件夹,你可以修改为你自己的名称
      

  13.   


    他这写的DirectoryInfo di = new DirectoryInfo(Server.MapPath("~\\res\\"));
    res 他这是项目下的绝对路径,就是res文件夹下的内容
     你可以读取你数据库中的字段
    改下就行
      

  14.   

    因为我一条路径下有多张图片,我想了很久,最后采用这种方法,如果用其他方式存储,冗余度就很大,只是现在不知道怎么把图片名加上去 ,下面这样由于没有具体到图片名称不可以得出结果,请问要怎样把图片名加进去呢
    <img alt="" runat="server" src='<%#Eval("Path")%>' style="width:220px; height:220px"/>
      

  15.   


    <asp:FormView ID="FormView3" runat="server" DataSourceID="SqlDataSource1">
    <ItemTemplate>
    <asp:Repeater ID="Repeater1" runat="server" DataSource='<%# GetPics(Eval("hPath") as string) %>'>
    <ItemTemplate>
    <img alt="" src='<%# Container.DataItem %>' style="width:90px; height:90px"/>
    </ItemTemplate>
    </asp:Repeater>
    </ItemTemplate>
    </asp:FormView><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=db1;Integrated Security=True"
    SelectCommand="SELECT * FROM housesPics WHERE (ID = @hID)"  >
    <SelectParameters>
    <asp:QueryStringParameter Name="hID" QueryStringField="hID" Type="Int32" />
    </SelectParameters>
    </asp:SqlDataSource> <script runat="server">
    public ArrayList GetPics(string path)
    {
    if (string.IsNullOrEmpty(path))
    return null;

    path = ResolveUrl(path);
    if (!path.EndsWith("/")) path = path + "/"; ArrayList files = new ArrayList();
    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath(path));
    System.IO.FileInfo[] fi = di.GetFiles();
    for (int i = 0; i < fi.Length; i++)
    {
    if (Regex.IsMatch(fi[i].Name.ToLower(), @".(bmp|png|jpg|jpeg|gif)"))
    {
    files.Add(path + fi[i].Name);
    }
    }
    return files;
    }
    </script>
      

  16.   

    谢谢楼上的朋友,现在总是提示不是有效的虚拟路径,下面这句话是不是要去掉啊
    if (!path.EndsWith("/")) path = path + "/";
    因为我存储的路径是D:\My pictures\地区图片\,路径下有image1.jpg;image2.jpg等多张图片,图片名称和路径中包含中文没问题吧
      

  17.   

    不好意思啊,网速不好。网站的根目录是D:\My Documents\Visual Studio 2005\Projects\WebSite1
    图片保存的目录是D:\My Documents\MyPictures\,下面有很多图片,image1.jpg,image2.jpg;
      

  18.   

    嗯,问题来了
    这么说来,你的图片根本不在网站下面,你想用户怎么能够访问到你的图片呢?为今之计,你有两条出路:
    其一,把图片目录改到你的网站下面,当然这样你可能要重写图片保存部分的程序,数据库里路径也要重写。
    其二,在IIS中另建一个虚拟目录,指向D:\My Documents\MyPictures,访问图片时通过这个虚拟目录访问
      

  19.   


    <img src="My pictures/<%# Eval("imagePath") %>" />这样子就可以了吧!
      

  20.   

    谢谢jshi123,我已经把图片目录放到网站下面,即把“地区图片”这个文件夹放到WebSite1 文件夹下了,为什么还是不行呢
      

  21.   

    不好意思,老掉线,我也很急,D:\My Documents\Visual Studio 2005\Projects\WebSite1 \地区图片下面分不同的地区,不同地区下再存的图片,比如D:\My Documents\Visual Studio 2005\Projects\WebSite1 \地区图片\湖南\changsha.jpg
      

  22.   

    网速不好,谢谢理解。数据库里的路径也改过来了,存到了图片名称的上一级,包括“\”,如D:\My Documents\Visual Studio 2005\Projects\WebSite1 \地区图片\湖南\
      

  23.   

    在GetPics方法里要加一句话:
    public ArrayList GetPics(string path)
    {
        if (string.IsNullOrEmpty(path))
            return null;    path = Regex.Replace(path, ".*(地区图片.*)", @"~\$1");
        path = ResolveUrl(path);
        if (!path.EndsWith("/")) path = path + "/";    ArrayList files = new ArrayList();
        System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath(path));
        System.IO.FileInfo[] fi = di.GetFiles();
        for (int i = 0; i < fi.Length; i++)
        {
            if (Regex.IsMatch(fi[i].Name.ToLower(), @".(bmp|png|jpg|jpeg|gif)"))
            {
                files.Add(path + fi[i].Name);
            }
        }
        return files;
    }另外,楼主确实应该考虑在数据库里保存相对路径,不要用绝对路径,并且最好把图片的文件名也带上,如果是多个文件用逗号隔开。
      

  24.   

    问题解决了,我改成了相对路径存储的,非常感谢大家,尤其是jshi123,一定尽快给分,但是我还有一个问题,如果我在原来的基础上还增加一个FromView模块,放置其他图片,该图片的路径与“地区图片”类似(“地区图片”对应数据表中的“hPath”字段),路径也差不多,只是名字不同,和“地区图片”是并列的,假如是叫“规划图片”,同样是存储在该数据表中,对应的字段为“jPath”,我要在另一个块中显示出“规划图片”,代码如下:
    <asp:FormView ID="FormView3" runat="server" DataSourceID="SqlDataSource1">
        <ItemTemplate>
            <asp:Repeater ID="Repeater1" runat="server" DataSource='<%# GetPics(Eval("hPath") as string) %>'>
                <ItemTemplate>
                    <img alt="" src='<%# Container.DataItem %>' style="width:90px; height:90px"/>
                </ItemTemplate>
            </asp:Repeater>
        </ItemTemplate>
    </asp:FormView><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=db1;Integrated Security=True"
        SelectCommand="SELECT * FROM housesPics WHERE (ID = @hID)"  >
        <SelectParameters>
            <asp:QueryStringParameter Name="hID" QueryStringField="hID" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource> <asp:FormView ID="FormView4" runat="server" DataSourceID="SqlDataSource2">
        <ItemTemplate>
            <asp:Repeater ID="Repeater1" runat="server" DataSource='<%# GetPics(Eval("jPath") as string) %>'>
                <ItemTemplate>
                    <img alt="" src='<%# Container.DataItem %>' style="width:90px; height:90px"/>
                </ItemTemplate>
            </asp:Repeater>
        </ItemTemplate>
    </asp:FormView><asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=db1;Integrated Security=True"
        SelectCommand="SELECT * FROM housesPics WHERE (ID = @hID)"  >
        <SelectParameters>
            <asp:QueryStringParameter Name="hID" QueryStringField="hID" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource> 
    再共享GetPics(string path)函数,这样应该是没问题啊,为什么会出错呢?系统提示未能映射路径。
    因为我这方面还是新手,所以问得比较详细,谢谢大家的包容,这里高手很多,我很乐意向大家学习
      

  25.   

    [code=C#:图片存储]
    //导入命名空间
    using System.IO;
    using System.Text; //用StringBuilder需要导入
    using System.Drawing;//导入图片操作的命名空间
    StringBuilder builder = new StringBuilder();
     //上传按钮
        protected void btnUpLoad_Click(object sender, EventArgs e)
        {
            HttpFileCollection files = HttpContext.Current.Request.Files;
           
            if (files.Count <= 0)
            {
                return;
            }        for (int i = 0; i <files.Count; i++)
            {
                if (!UpFile(files[i], i))
                {
                }
            }        Response.Write("要保存到数据库的字符串:" + builder.ToString());
            string[] str;
            str = builder.ToString().Split('@');
            for (int i = 0; i < str.Length; i++)
            {                                                         
                Response.Write("截取后的字符串:"+str[i].ToString() +"<br/>");
            }    }    //上传图片
        private bool UpFile(HttpPostedFile file, int index)
        {
            if (!string.IsNullOrEmpty(file.FileName))
            {
                try
                {
                    //逻辑:从上传的文件的文件流中获得到文件信息,在内存中重构这张图片
                    //如果图片重构成功则是一张正常的图片,重构失败就不是图片文件
                    //这个操作会导致异常 一定要放到try中去做
                    Bitmap a = new Bitmap(file.InputStream);
                    string fileName = DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss-fffff") + "-product" + ".jpg";
                    builder.Append(fileName);
                    builder.Append("@");
                    string path = Server.MapPath("~/Upload") + @"\" + fileName;
                    //file.SaveAs(path);
                    Response.Write(string.Format("第{0}行{1} 上传成功!<br/>",index+1,file.FileName));
                }
                catch
                {
                    Response.Write(string.Format("第{0}行{1} 上传的文件不合法!<br/>", index + 1, file.FileName));
                    return false;
                }            return true;
                   
            }
            else
            {
                Response.Write(string.Format("第{0}行{1} 文件路径不能为空!<br/>", index + 1, file.FileName));
                return false;
            }
        }
    [/code]建议先从数据库中取出来,然后再进行字符串截取,再进行重绑。
    [code=C#:图片读取]
    Image showImage = e.Row.FindControl("showImage") as Image;
                string[] str;
                str = showImage.ImageUrl.Split('@');
                for (int i = 0; i < str.Length; i++)
                {
                    str[i].ToString();
                }
                showImage.ImageUrl = str[0].ToString();
                //MessageBox.Show(this,showImage.ImageUrl);
    [/code]
      

  26.   

    1,路径中你只保存图片名称,而不是路片路径,更不能保存 d:// 这样的硬盘路径,这是一种脑残的做法,你只保存xxx.jpg就行了,如果你怕有重复的名称,就上传的时候改一下文件名.
    2,读取的时候,你的图片必须在网站目录中,假设在image目录中,那么在image控件中imageUrl路径为~/image/<%#Eval("你的数据字段名")%>.这样就好了.
    3.你在一个FM中写两个image控件无非就是在<%#Eval("你的数据字段名")%>中该一下你的数据字段名,只要你的FM已经绑定了数据,就没有问题.用image服务器控件,而不是img静态标签.