我在Web页面上,想用Image服务器控件显示数据库中的图片:
SqlConnection Conn=new SqlConnection("user id=sa;data source=.;pwd=123321;initial catalog=master");
Conn.Open();
SqlCommand cmd=new SqlCommand("select top 1 * from images",Conn);
byte[] b=(byte[])cmd.ExecuteScalar();
MemoryStream MS=new MemoryStream();
MS.Write(b,0,b.Length);
Bitmap Bt=new Bitmap(MS);
问各位高手下一步怎么办.

解决方案 »

  1.   

    你是保存的编码吧,你读取出来后,直接response.write()就可以了
      

  2.   

    this.Image.image = Image.FromStream(MS);
      

  3.   

    showphoto.aspx
    ==================================================
    SqlConnection Conn=new SqlConnection("user id=sa;data source=.;pwd=123321;initial catalog=master");
    Conn.Open();
    SqlCommand cmd=new SqlCommand("select top 1 * from images",Conn);
    byte[] b=(byte[])cmd.ExecuteScalar();
    MemoryStream MS=new MemoryStream();
    MS.Write(b,0,b.Length);
    Bitmap Bt=new Bitmap(MS);
    Bt.save(Response.OutputStream,ImageFormat.Gif);
    ==================================================
    Image.ImageUrl = "showphoto.aspx"
      

  4.   

    先谢谢合位,Response这种方法我知道。
    我就是想绑定到WEB控件上去
      

  5.   

    showphoto.aspx
    ==================================================
    SqlConnection Conn=new SqlConnection("user id=sa;data source=.;pwd=123321;initial catalog=master");
    Conn.Open();
    SqlCommand cmd=new SqlCommand("select top 1 * from images",Conn);
    byte[] b=(byte[])cmd.ExecuteScalar();
    MemoryStream MS=new MemoryStream();
    MS.Write(b,0,b.Length);
    Bitmap Bt=new Bitmap(MS);
    Bt.save(Response.OutputStream,ImageFormat.Gif);
    ==================================================
    Image.ImageUrl = "showphoto.aspx"
    showphoto.aspx
    ==================================================
    SqlConnection Conn=new SqlConnection("user id=sa;data source=.;pwd=123321;initial catalog=master");
    Conn.Open();
    SqlCommand cmd=new SqlCommand("select top 1 * from images",Conn);
    byte[] b=(byte[])cmd.ExecuteScalar();
    MemoryStream MS=new MemoryStream();
    MS.Write(b,0,b.Length);
    Bitmap Bt=new Bitmap(MS);
    Bt.save(Response.OutputStream,ImageFormat.Gif);
    ==================================================
    Image.ImageUrl = "showphoto.aspx"
      

  6.   

    如果要用Image控件,我认为只能先把数据库中的图片读出后保存在数据器的某个文件夹下,请参考:http://blog.csdn.net/gaofeng2000/archive/2004/08/27/86264.aspx
      

  7.   

    从oracle中取图片,并显示
    Imports System.Drawing Dim conn As New OracleClient.OracleConnection()
            Dim cmd As New OracleClient.OracleCommand()
            Dim myReader As OracleClient.OracleDataReader
            Dim sql As String
            Dim fl As File        sql = "select jt from sd_gtzp"  '从数据库中取出图片数据 blob
            conn.ConnectionString = "Password=jlsbgis;User ID=jlsbgis;Data Source=sj"
            cmd.CommandText = sql
            cmd.Connection = conn        conn.Open()
            myReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
            myReader.Read()        Dim fs As FileStream                 ' Writes the BLOB to a file (*.bmp).
            Dim bw As BinaryWriter               ' Streams the binary data to the FileStream object.
            Dim bufferSize As Integer = 1000      ' The size of the BLOB buffer.
            Dim outbyte(bufferSize - 1) As Byte  ' The BLOB byte() buffer to be filled by GetBytes.
            Dim retval As Long                   ' The bytes returned from GetBytes.
            Dim startIndex As Long = 0           ' The starting position in the BLOB output.       
            Dim fpath As String        fpath = Server.MapPath(Request.ApplicationPath) & "\Image\photo.bmp" '将图片数据存成本地文件
            fs = New FileStream(fpath, FileMode.OpenOrCreate, FileAccess.Write)
            bw = New BinaryWriter(fs)        ' Reset the starting byte for a new BLOB.
            startIndex = 0        ' Read bytes into outbyte() and retain the number of bytes returned.
            retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize)        ' Continue reading and writing while there are bytes beyond the size of the buffer.
            Do While retval = bufferSize
                bw.Write(outbyte)
                bw.Flush()
                
                ' Reposition the start index to the end of the last buffer and fill the buffer.
                startIndex = startIndex + bufferSize
                retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize)
            Loop        ' Write the remaining buffer.
            bw.Write(outbyte)
            bw.Flush()
            
            ' Close the output file.
            bw.Close()
            fs.Close()        ' Close the reader and the connection.
            myReader.Close()
            conn.Close()               Dim logo As Image               '文件格式转换
            logo = Image.FromFile(fpath)
            fpath = Server.MapPath(Request.ApplicationPath) & "\Image\zkjhy.jpeg"
            logo.Save(fpath, System.Drawing.Imaging.ImageFormat.Jpeg)
            logo.Dispose()
            logo = Nothing
            
            Me.Image1.Width = New Unit(300) '调整显示大小
            Me.Image1.Height = New Unit(350)
            Me.Image1.ImageUrl = "image/zkjhy.jpeg"