none
在picturebox中显示从SqlServer取出的图片问题 RRS feed

  • 问题

  • 大家好 ,帮我看看这段代码 在执行到 PictureBox1.Image = New System.Drawing.Bitmap(meorystream) 这句时出现问题,提示“GDI+出现一般错误”  代码如下
     Dim dbcommand As SqlClient.SqlCommand
        Dim str As String
                   str = "select picimage  from picInfo where picid=" & id
                dbcommand = New SqlClient.SqlCommand(str, dbConnection)
            Dim dr As SqlClient.SqlDataReader = dbcommand.ExecuteReader
                Do While dr.Read
        Dim meorystream As System.IO.MemoryStream
        Dim buffer As Byte()
                    buffer = CType(dr("picimage"), Byte())
                    meorystream = New System.IO.MemoryStream(buffer)
                    PictureBox1.Image = New System.Drawing.Bitmap(meorystream)
                       meorystream.Dispose()
                Loop
                Label1.Text = "共存有图片" & DataGridView1.RowCount - 1 & "张"
                dr.Close()
            End If
            dbConnection.Dispose()
            dbConnection.Close()
    数据库连接没有问题
    各位帮我看看 先谢过了
    昨夜西风凋碧树,独上高楼,望尽天涯路!
    2010年2月1日 8:02

答案

  • 你好!

    我昨天按照你的方式试了下未发现你所述的问题。下面的测试代码:

    Public Sub Save()
        '
    ' * CREATE TABLE sys_Images(
    ' * Id uniqueidentifier NOT NULL,
    ' * Images image NOT NULL,
    ' * Primary Key (Id))
    '
       
        Dim imgPath As String = "C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"
       
        Using conn As New SqlConnection("Data Source=X01;Initial Catalog=X;Integrated Security=SSPI;")
            Dim comm As SqlCommand = conn.CreateCommand()
            comm.CommandText = "INSERT INTO sys_Images VALUES(@Id, @Images)"
            comm.Parameters.AddWithValue("@Id", Guid.Empty)
            comm.Parameters.AddWithValue("@Images", GetFileStream(imgPath))
            conn.Open()
            comm.ExecuteNonQuery()
        End Using
    End Sub
    Public Function GetFileStream(ByVal fFilePath As [String]) As Byte()
        Using fileStream As New FileStream(fFilePath, FileMode.Open)
            Dim vals As Byte() = New Byte(CInt(fileStream.Length) - 1) {}
            fileStream.Read(vals, 0, vals.Length)
            Return vals
        End Using
    End Function
    Public Sub ShowImage()
        Using conn As New SqlConnection("Data Source=X01;Initial Catalog=X;Integrated Security=SSPI;")
            Dim comm As SqlCommand = conn.CreateCommand()
            comm.CommandText = "Select Id, Images FROM sys_Images"
            conn.Open()
           
            Using dr As SqlDataReader = comm.ExecuteReader(CommandBehavior.CloseConnection)
                While dr.Read()
                    Dim buffer As Byte() = DirectCast(dr("Images"), Byte())
                    Dim meorystream As New System.IO.MemoryStream(buffer)
                    pictureBox1.Image = New System.Drawing.Bitmap(meorystream)
                    meorystream.Dispose()
                End While
            End Using
        End Using
    End Sub


    知识改变命运,奋斗成就人生!
    2010年2月2日 2:19
    版主
  • Do While dr.Read -> If dr.Read()
    【孟子E章】
    2010年2月2日 7:07
    版主

全部回复

  • 你好!
         这个问题一般是对Image,Bitmap 等对象没有释放,然后直接又多这个对象进行写回源文件的操作引起的:
         你检查一下看看!
    周雪峰
    2010年2月1日 11:24
    版主
  • 你好! 谢谢 应该不会是对象没有释放 因为在运行这段代码前没有没有执行过Iimage、Bitmap之类的代码, 你有没有比较好的代码让我学习学习,呵呵
    昨夜西风凋碧树,独上高楼,望尽天涯路!
    2010年2月2日 1:42
  • 你好!

    我昨天按照你的方式试了下未发现你所述的问题。下面的测试代码:

    Public Sub Save()
        '
    ' * CREATE TABLE sys_Images(
    ' * Id uniqueidentifier NOT NULL,
    ' * Images image NOT NULL,
    ' * Primary Key (Id))
    '
       
        Dim imgPath As String = "C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"
       
        Using conn As New SqlConnection("Data Source=X01;Initial Catalog=X;Integrated Security=SSPI;")
            Dim comm As SqlCommand = conn.CreateCommand()
            comm.CommandText = "INSERT INTO sys_Images VALUES(@Id, @Images)"
            comm.Parameters.AddWithValue("@Id", Guid.Empty)
            comm.Parameters.AddWithValue("@Images", GetFileStream(imgPath))
            conn.Open()
            comm.ExecuteNonQuery()
        End Using
    End Sub
    Public Function GetFileStream(ByVal fFilePath As [String]) As Byte()
        Using fileStream As New FileStream(fFilePath, FileMode.Open)
            Dim vals As Byte() = New Byte(CInt(fileStream.Length) - 1) {}
            fileStream.Read(vals, 0, vals.Length)
            Return vals
        End Using
    End Function
    Public Sub ShowImage()
        Using conn As New SqlConnection("Data Source=X01;Initial Catalog=X;Integrated Security=SSPI;")
            Dim comm As SqlCommand = conn.CreateCommand()
            comm.CommandText = "Select Id, Images FROM sys_Images"
            conn.Open()
           
            Using dr As SqlDataReader = comm.ExecuteReader(CommandBehavior.CloseConnection)
                While dr.Read()
                    Dim buffer As Byte() = DirectCast(dr("Images"), Byte())
                    Dim meorystream As New System.IO.MemoryStream(buffer)
                    pictureBox1.Image = New System.Drawing.Bitmap(meorystream)
                    meorystream.Dispose()
                End While
            End Using
        End Using
    End Sub


    知识改变命运,奋斗成就人生!
    2010年2月2日 2:19
    版主
  • Do While dr.Read -> If dr.Read()
    【孟子E章】
    2010年2月2日 7:07
    版主