Image from SQL to PictureBox

G

Gary Shell

I have a column named "PictureImage" in a SQL database defined as
datatype=image. It has a Bitmap picture in it. (I verified the type by
creating an Access project and a looking at the data, which access reports
as "Bitmap Image".)

In my VB.NET app I use the following code:
Dim bytBLOBData() As Byte =
Me.datasetProduct.Tables("Product").Rows(0)("PictureImage")

Dim stmBLOBData As New MemoryStream(bytBLOBData)

Me.pictureProduct.Image = Image.FromStream(stmBLOBData)



The last line of code (image.fromstream) always produces an Invalid
Parameter error.

Does anyone have any working samples of similar code. I've done a Google
search and see that other folks have similar errors, but no solutions.

Gary
 
K

Ken Tucker [MVP]

Hi,

Here is a quick example. Loads the northwind databases category
names into a listbox (listbox1) and displays the image in a picture box
(picturebox1).

Dim ds As DataSet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strConn As String

Dim conn As SqlClient.SqlConnection

Dim daCustomer As SqlClient.SqlDataAdapter

ds = New DataSet

' strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

'strConn &= "Data Source = Northwind.mdb;"

strConn = "Server = " + Environment.MachineName + "\VSdotNet;"

strConn &= "Database = NorthWind;"

strConn &= "Integrated Security = SSPI;"

conn = New SqlClient.SqlConnection(strConn)

daCustomer = New SqlClient.SqlDataAdapter("Select * from Categories", conn)

ds = New DataSet

daCustomer.Fill(ds, "Categories")

ListBox1.DataSource = ds.Tables("Categories")

ListBox1.DisplayMember = "CategoryName"

End Sub



Private Sub ListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ListBox1.SelectedValueChanged

Dim dr As DataRow = ds.Tables("Categories").Rows(ListBox1.SelectedIndex)

Dim ms As New System.IO.MemoryStream

Dim bm As Bitmap

Dim arData() As Byte = dr.Item("Picture")

ms.Write(arData, 78, arData.Length - 78)

bm = New Bitmap(ms)

PictureBox1.Image = bm

End Sub



Ken

-----------------
I have a column named "PictureImage" in a SQL database defined as
datatype=image. It has a Bitmap picture in it. (I verified the type by
creating an Access project and a looking at the data, which access reports
as "Bitmap Image".)

In my VB.NET app I use the following code:
Dim bytBLOBData() As Byte =
Me.datasetProduct.Tables("Product").Rows(0)("PictureImage")

Dim stmBLOBData As New MemoryStream(bytBLOBData)

Me.pictureProduct.Image = Image.FromStream(stmBLOBData)



The last line of code (image.fromstream) always produces an Invalid
Parameter error.

Does anyone have any working samples of similar code. I've done a Google
search and see that other folks have similar errors, but no solutions.

Gary
 
G

Gary Shell

Looks like the key here is that you are dropping the first 78 bytes of the
image retrieved from the image that Access stored and creating the image
from that data.

After I left the note I realized that even though I was using a SQL Server
to store the data and Access was pointing to that database, that Access was
adding an OLE wrapper around the image and then storing it. The VB.net code
didn't know about that and rejected the image. Your trick would have worked
, I think.

But what I did was implement a paste from clipboard function in the picture
box and the ability to save the picture box image to the database, all in
the VB.NET code. Now I don't rely on Access to add the pictures to the
database. Images that I save this way work fine with the original code.
And I don't have to worry about those pesky 78 bytes!

Thanks though!
Gary
 
C

Cor Ligthert

Ken,

This is the sample with Ole wrapped images, I think that it is right that
you tell that with it next time, normally that 78bytes should not be cut of.
(The Northwind database uses that wrapped images).

I thought as well direct that this was the problem from Gary, therefore the
sample was in my opinion OK. (I had given it when you had not done it
already).

Cor
 
K

Ken Tucker [MVP]

Hi,

The sample code did use sql server. I forgot to delete the old
connection string for access. I switch the code back and forth between
access and sql server depending on the question.

Ken
-----------------
Looks like the key here is that you are dropping the first 78 bytes of the
image retrieved from the image that Access stored and creating the image
from that data.

After I left the note I realized that even though I was using a SQL Server
to store the data and Access was pointing to that database, that Access was
adding an OLE wrapper around the image and then storing it. The VB.net code
didn't know about that and rejected the image. Your trick would have worked
, I think.

But what I did was implement a paste from clipboard function in the picture
box and the ability to save the picture box image to the database, all in
the VB.NET code. Now I don't rely on Access to add the pictures to the
database. Images that I save this way work fine with the original code.
And I don't have to worry about those pesky 78 bytes!

Thanks though!
Gary
 
H

Herfried K. Wagner [MVP]

Gary Shell said:
I have a column named "PictureImage" in a SQL database defined as
datatype=image. It has a Bitmap picture in it. (I verified the type by
creating an Access project and a looking at the data, which access reports
as "Bitmap Image".)

HOW TO: Read and Write a File to and from a BLOB Column by Using ADO.NET and
Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;316887>

HOW TO: Read and Write a File to and from a BLOB Column by Using Chunking in
ADO.NET and Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;317034>
 
C

Cor Ligthert

Herfried,

In my opinion is nothing wrong with the sample from Gary, except when there
is an Object Linked Embeded image used.

Your samples are not different than his code. While Ken's shows an sample
wiht the offset that Gary can use. (The offset is as well in your second
sample, however not is told why and what).

Just as attention for the next time.

Cor
 
G

Gary Shell

Thanks all!

Is it possible to tell when to drop the 78 bytes and when NOT to? Is there
any way to determine at run time if a particular image does or does not have
the extra 78 bytes from the OLE wrapper?

Gary
 
C

Cor Ligthert

Gary,

As I remember me, can you see that in the byte Array, however I have not
documented that.

Just debug and see what is it in the quickwatch.

Cor
 
G

Gary Shell

Cor,

Unfortunately I converted all of my images to the non-OLE form and don't
have an instance of Northwind installed.

If you'd be so kind as to capture the first 78 bytes for me I'll try to
dissect it. (Ideally from two or three images so I can compare.)

Thanks,
Gary
 
C

Cor Ligthert

Gary,

I made a sample just in general.
I think with this you see how you can do it.

\\\needs a form with two textboxes
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
Dim conn As New SqlClient.SqlConnection("Server=Kamer;" & _
"DataBase=Northwind; Integrated Security=SSPI")
Dim sqlstr As String = _
String.Format("SELECT Photo FROM Employees WHERE EmployeeID = 1")
Dim cmd As New SqlClient.SqlCommand(sqlstr, conn)
conn.Open()
Dim rdr As SqlClient.SqlDataReader = cmd.ExecuteReader()
rdr.Read()
Dim arrImage() As Byte = DirectCast(rdr.Item("Photo"), Byte())
Dim enc As System.Text.Encoding = System.Text.Encoding.ASCII
Dim str As String = enc.GetString(arrImage, 0, 78)
TextBox1.Text = str.Replace(chr(0), "."c)
Dim sb As New System.Text.StringBuilder
For Each chr As Char In str
sb.Append(Asc(chr).ToString)
Next
TextBox2.Text = sb.ToString
rdr.Close()
conn.Close()
End Sub
///

The result from this was

Textbox1

/....
....!.Bitmap Image.Paint.Picture.........PBrush......... T..

See that I replaced the zeros by dots to get it printable

Textbox2
2128470200013014020033012712712712766105116109971123273109971031010809710511011646801059911611711410101500200070008066114117115104000000000328400

I hope this helps a little bit?

Cor
 
G

Gary Shell

Thank you. Thank you! THANK YOU!
That does help. I need to compare against the non ole images but it looks
like it should be pretty easy to determine if the OLE wrapper is there by
looking for "Bitmap Image.Paint.Picture" in the first 78 bytes.

I'm going to look around and see if I can find any more info on the format
of this OLE wrapper to see if there might be other such strings to test for.
Something in the pit of my stomach tells me there just might be!

Gary
 

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