2013-04-22 69 views
-1

我正在一個項目中,我有一個通過datagridview顯示的圖像列表。 我想在datagridview中點擊特定圖像時將圖像加載到圖像框。如何從datagridview加載圖像在鼠標單擊到vb.net中的圖片框

我的代碼

Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick 
     sql.Open() 

     cmd = New SqlCommand("select pic from detail4 where id='" & DataGridView1.CurrentRow.Cells(0).Value() & "'", sql) 
     Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte()) 
     If Not imageData Is Nothing Then 
      Using ms As New MemoryStream(imageData, 0, imageData.Length) 
       ms.Write(imageData, 0, imageData.Length) 
       PictureBox1.BackgroundImage = Image.FromStream(ms, True) 
      End Using 
     End If 

    End Sub 

我走出內存異常.. 什麼可能是問題。 請幫助

+1

是什麼'pic'列數據類型? – Sathish 2013-04-22 06:00:33

+0

可能重複的[如何將圖像從SQL Server加載到圖片框?](http://stackoverflow.com/questions/8084590/how-to-load-image-from-sql-server-into-picture-box) – Neolisk 2013-04-23 21:35:53

回答

0

聲明的圖象 - 爲對象

Dim cmd As SqlCommand 
cmd = New SqlCommand("select vchimage1 from producto where chrcodigoproducto='" & cCodProducto & "'", ClsCn.Cnx_Base) 
Dim imagedata As Object 
imagedata = cmd.ExecuteScalar() 
'Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte()) 

If IsNothing(imagedata) Or IsDBNull(imagedata) Then 

    ImgProducto.Visible = False 
    mLoadPictureImage = False 
Else 
    Using ms As New MemoryStream(imagedata, 0, imagedata.Length) 
     ms.Write(imagedata, 0, imagedata.Length) 
     ImgProducto.BackgroundImage = Image.FromStream(ms, True) 

     mLoadPictureImage = True 
    End Using 
End If 

www.jisoft-ingenieros.com

相關問題