PictureBox Question

W

wandii

Hi,
I have a table which contains a BLOB field for image. I would like
to copy a picture from
database directly to a picture box control and I have tried the
following:

Dim c as Integer
c = sDataSet.Tables(0).Rows.Count

Dim bytBLOBData() As Byte =
sDataSet.Tables(0).Rows(c-1)("Image")
Dim stmBLOBData As New System.IO.MemoryStream(bytBLOBData)

I get the error "Invalid Parameter user" after the following line:
Me.PictureBox2.Image = Image.FromStream(stmBLOBData)

I also tried changing the following line - still "Invalid Parameter
user" error.
Dim bytBLOBData() As Byte =
CType(sDataSet.Tables(0).Rows(c-1)("Image"), Byte())

Can anyone give me an idea please, thanks in advance.

Regards.
 
G

Guest

wandii,
try reading the data from your blob into a SqlBinary, and using this to
contruct your memory stream using the SqlBinarys Value property

hth
 
W

wandii

Hi guy,
Thanks for the reply. I am not familiar with the SqlBinary could
you please give
me a small example on this. Thanks in advance.
 
W

wandii

Hi guy,
Thanks for the reply. I am not familiar with the SqlBinary could
you please give
me a small example on this. Thanks in advance.
 
W

wandii

Hi guy,
Thanks for the reply. I am not familiar with the SqlBinary could
you please give
me a small example on this. Thanks in advance.
 
G

gene kelley

Hi,
I have a table which contains a BLOB field for image. I would like
to copy a picture from
database directly to a picture box control and I have tried the
following:

Dim c as Integer
c = sDataSet.Tables(0).Rows.Count

Dim bytBLOBData() As Byte =
sDataSet.Tables(0).Rows(c-1)("Image")
Dim stmBLOBData As New System.IO.MemoryStream(bytBLOBData)

I get the error "Invalid Parameter user" after the following line:
Me.PictureBox2.Image = Image.FromStream(stmBLOBData)

I also tried changing the following line - still "Invalid Parameter
user" error.
Dim bytBLOBData() As Byte =
CType(sDataSet.Tables(0).Rows(c-1)("Image"), Byte())

Can anyone give me an idea please, thanks in advance.

Regards.


This example reads a Blob in an Access database from the field named
"ImgData" and displays the result in PictureBox1.

Dim mStream As New System.IO.MemoryStream
Dim pData() As Byte = _
DirectCast(tblSat.Rows(idxRow).Item("ImgData"), Byte())
mStream.Write(pData, 0, Convert.ToInt32(pData.Length))
Dim bm As Bitmap = New Bitmap(mStream, False)
PictureBox1.Image = bm
mStream.Dispose()

Gene
 
G

GhostInAK

Hello gene,

If the image is stored as an OLE image you will need to start reading at
byte 78 instead of byte 0.

-Boo
 
G

gene kelley

Hello gene,

If the image is stored as an OLE image you will need to start reading at
byte 78 instead of byte 0.

-Boo

Yes. Somehow I always equate "BLOB" with "0" and Image/Picture with
"78".

Gene
 
G

Guest

Thanks for great example of reading Blob and/or OLE Image. Do you have an
example of how to write from a picture box image to the datatable? Thanks.
 
G

gene kelley

Thanks for great example of reading Blob and/or OLE Image. Do you have an
example of how to write from a picture box image to the datatable? Thanks.

Assume loading a PictureBox Image from a file (strFilename)

Dim myStream As IO.FileStream = New IO.FileStream(strFilename, _
IO.FileMode.Open, IO.FileAccess.Read)
With myStream
Dim myImageBuffer(CType(.Length, Integer)) As Byte
.Read(myImageBuffer, 0, Convert.ToInt32(.Length))

'Display the image
Dim PreviewlImage As Bitmap = New Bitmap(myStream)
me.PictureBox.Image = PreviewImage

'myImageBuffer is the BLOB
'Pass it to whatever Insert/Update method you are using
'Or you can store it in the Tag property for later use
Me.PictureBox.Tag = myImageBuffer

.Close()
End With


Tag usage:
Dim ImgData() As Byte = DirectCast(Me.PictureBox.Tag, Byte())

where ImgData() is what goes into the Blob field in the table.

Gene
 
G

Guest

Thanks for the technique.
--
Dennis in Houston


gene kelley said:
Assume loading a PictureBox Image from a file (strFilename)

Dim myStream As IO.FileStream = New IO.FileStream(strFilename, _
IO.FileMode.Open, IO.FileAccess.Read)
With myStream
Dim myImageBuffer(CType(.Length, Integer)) As Byte
.Read(myImageBuffer, 0, Convert.ToInt32(.Length))

'Display the image
Dim PreviewlImage As Bitmap = New Bitmap(myStream)
me.PictureBox.Image = PreviewImage

'myImageBuffer is the BLOB
'Pass it to whatever Insert/Update method you are using
'Or you can store it in the Tag property for later use
Me.PictureBox.Tag = myImageBuffer

.Close()
End With


Tag usage:
Dim ImgData() As Byte = DirectCast(Me.PictureBox.Tag, Byte())

where ImgData() is what goes into the Blob field in the table.

Gene
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top