How to retrieve an image from Access DB and display in a pictureBox of Windows Form?
In a table of Microsoft Access DB, images are stored in an object-type column. Now I want to retrieve them out and display in a pictureBox of Windows Form, how to implement that with C# Using Graphics.DrawImage If so, how to construct such an image object Had better give me some sample codes if possible.

How to retrieve an image from Access DB and display in a pictureBox of Windows Form?
mTIE
Frank Hodgkinson
hello...
i got some sample from MSDN (i guess, i lost url)
its in VB but you can convert into C# as it is simple
=====================================
Public Function ExecuteReaderBinary(ByVal ConnectionString As String, ByVal QueryString As String, Optional ByVal Params() As OleDbParameter = Nothing) As Image
Dim conn As New OleDbConnection(ConnectionString)
Dim cmd As New OleDbCommand(QueryString, conn)
Dim dt As New DataTable
Dim img As Image
Try
With cmd
.CommandTimeout = 60
.CommandType = CommandType.Text
.CommandText = QueryString
If Not IsNothing(Params) Then
Dim i As Integer
For i = 0 To Params.Length - 1
.Parameters.Add(Params(i))
Next
End If
End With
Dim da As New OleDbDataAdapter(cmd)
da.Fill(dt)
Dim msPic As IO.MemoryStream
Dim abytPic() As Byte
' Signature bytes of an OLE container header.
Const OLEbyte0 As Byte = 21
Const OLEbyte1 As Byte = 28
' Number of bytes in OLE container header.
Const OLEheaderLength As Integer = 78
abytPic = CType(dt.Rows(0)("Picture"), Byte())
' Test for an OLE container header.
If (abytPic(0) = OLEbyte0) And (abytPic(1) = OLEbyte1) Then
' Use a second array to strip off the header.
' Make it big enough to hold the bytes after the header.
Dim abytStripped(abytPic.Length - OLEheaderLength - 1) As Byte
' Strip off the header by copying the bytes after the header.
System.Buffer.BlockCopy( _
src:=abytPic, srcOffset:=OLEheaderLength, _
dst:=abytStripped, dstOffset:=0, _
count:=abytPic.Length - OLEheaderLength)
' Load the new byte array into a MemoryStream.
msPic = New IO.MemoryStream(abytStripped)
Else
' Load the original byte array into a MemoryStream.
msPic = New IO.MemoryStream(abytPic)
End If
img = Image.FromStream(msPic)
Catch ex As Exception
CloseObject(conn, cmd)
End Try
CloseObject(conn, cmd)
Return img
End Function
=====================================
Kristijan_M
C# version of CType on your DataColumn sample from MSDN...
===========================
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])(ds.Tables["BLOBTest"].Rows[c - 1]["BLOBData"]);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
pictureBox1.Image= Image.FromStream(stmBLOBData);
===========================
http://support.microsoft.com/default.aspx scid=kb;en-us;317701
Wepiha
The code sample available for download includes a snippet of how to do this:
ADO.NET Code Generator for Microsoft Access:
http://www.eggheadcafe.com/articles/microsoftaccess_source_code_generator.asp
vbtricks
i used this code but still have invalid parameter error
da.SelectCommand = New OleDbCommand("select aks from doc", OleDbConnection1)
OleDbConnection1.Open()
da.SelectCommand.ExecuteNonQuery()
OleDbConnection1.Close()
da.Fill(ds, "doc")
'image
Dim curr As Integer
curr = 0
Dim bytImage() As Byte
bytImage = CType(ds.Tables("doc").Rows(curr).Item("aks"), Byte())
Dim imgbytes(bytImage.Length - 79) As Byte
Array.Copy(bytImage, 78, imgbytes, 0, bytImage.Length - 78)
Dim strmImage As New MemoryStream(imgbytes)
PictureBox2.Image = Image.FromStream(strmImage)
End If
santosh kumar gupta
Jung Yi,
When running your codes, I suffer from the the following exception:
System.ArgumentException: Invalid parameter used.
in the statement:
pictureBox1.Image= Image.FromStream(stmBLOBData);
Any idea in the exception
moonriver
JohnMathewmathan
The problem is that I don't know how to convert an Image object to a byte array (byte []) in C#. It seems that Convert.ChangeType() (Possibly similar to CType in VB .Net) method does not support any conversion to byte []. Please help me to overcome the final obstacle.
moonriver