从数据库中图片的流文件,显示在 image 控件里面要怎么做。谢谢。谢谢

解决方案 »

  1.   

    楼主可以参照下边这个过程:将表中的图片字段生成文件,然后要将这个图片文件放入picturebox应该没问题吧....Sub ReadPic() 
            Try
                Dim fs As IO.FileStream                 
                Dim bw As IO.BinaryWriter    
                Dim conn As New SqlClient.SqlConnection("server=(local);database=tempdb;uid=sa;pwd=;")           
                Dim logoCMD As New SqlClient.SqlCommand("select id,photo from testPhoto", conn)
                Dim bufferSize As Integer = 100      
                Dim outbyte(bufferSize - 1) As Byte  
                Dim retval As Long                   
                Dim startIndex As Long = 0
                Dim FN as String            Dim pub_id As String = ""                       conn.Open()
                Dim myReader As SqlClient.SqlDataReader = logoCMD.ExecuteReader(CommandBehavior.SequentialAccess)            Do While myReader.Read()
                    pub_id = myReader("id")
                    FN="image" & pub_id & ".bmp"
                    fs = New IO.FileStream(FN, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
                    bw = New IO.BinaryWriter(fs)                startIndex = 0
                    retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)
                  
                    Do While retval = bufferSize
                        bw.Write(outbyte)
                        bw.Flush()
                        startIndex += bufferSize
                        retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)
                    Loop                bw.Write(outbyte, 0, retval - 1)
                    bw.Flush()                bw.Close()
                    fs.Close()
                Loop
                conn.Close()
                myReader.Close()
            Catch ex As Exception
                ........
            End Try    End Sub
      

  2.   

    *.aspx代码中加:
    <asp:Image ID="imgCtrl" runat="server"></asp:Image>
      

  3.   

    *.cs中加:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace Blast
    { public class editpic : PageBase
    {
    protected System.Web.UI.WebControls.Image imgCtrl;
    protected System.Web.UI.WebControls.TextBox tbTitle;
    protected System.Web.UI.WebControls.TextBox tbDescription;
    protected System.Web.UI.WebControls.Label lbTitle;
    protected System.Web.UI.WebControls.Button SaveButton; private void Page_Load(object sender, System.EventArgs e)
    {
    if (!IsPostBack)
    {
    // 从数据库中载入图片的属性
    int picid = Convert.ToInt32(Request["id"]);
    string title, description;
    Globals.GetPicInfo(picid, out title, out description); // 设置要修改的字段
    tbTitle.Text = title;
    tbDescription.Text = description; // 设置由genimage产生的图片
    imgCtrl.ImageUrl = "genimage.ashx?thumbnail=yes&id=" + picid;
    }
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    { InitializeComponent();
    base.OnInit(e);
    }
    private void InitializeComponent()
    {    
    this.SaveButton.Click += new System.EventHandler(this.SaveButton_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void SaveButton_Click(object sender, System.EventArgs e)
    {
    // 更新数据库
    int picid = Convert.ToInt32(Request["id"]);
    Globals.UpdatePicInfo(picid, tbTitle.Text, tbDescription.Text);
    Response.Redirect("viewpic.aspx?id="+picid);
    }
    }
    }
      

  4.   

    简单啊楼主byte[] bytes = (byte[])dataTable.Rows[0]["Image"];MemoryStream ms = new MemoryStream(bytes);Image img = Image.FromStream(ms);
      

  5.   

    一个例子,有两个页面
    1 显示图片的页面,调用取图片的页面
    我在页面里的DataGrid放了一个图片,用了一个连接到另一个页面取图片
    <asp:Image id=Imagebutton1 runat="server" ImageUrl='<%# "BookCover.aspx?ImageID="+DataBinder.Eval(Container,"DataItem.BookGuid")%>'></asp:Image>BookCover.aspx  读取图片的页面string str=System.Configuration.ConfigurationSettings.AppSettings["cn"];
    SqlConnection cn=new SqlConnection(str);
    SqlCommand cmd=new SqlCommand();
    cmd.CommandText="select Cover from Books where BookGuid='"+this.Request["ImageID"]+"'";
    cmd.Connection=cn;
    cn.Open();
    this.Response.ContentType="image/*";
    SqlDataReader dr=cmd.ExecuteReader();
    while(dr.Read())
    {
    this.Response.BinaryWrite((byte[])dr["Cover"]);
    }
    cn.Close();
    代码是写在private void Page_Load(object sender, System.EventArgs e)里的
      

  6.   

    楼上的代码就可以了,不过while(dr.Read())似乎没有必要,一个页面不能显示两个图片,还是改成if(dr.Reade())比较好