469,327 Members | 1,275 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

Image from SQL to PictureBox

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)("Pictu reImage")

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
Nov 21 '05 #1
7 11476
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.SelectedInde x)

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

-----------------
"Gary Shell" <gs****@fuse.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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)("Pictu reImage")

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

Nov 21 '05 #2
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
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:u$*************@TK2MSFTNGP15.phx.gbl...
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.SelectedInde x)

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

-----------------
"Gary Shell" <gs****@fuse.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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)("Pictu reImage")

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

Nov 21 '05 #3
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:Oh*************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #4
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
Nov 21 '05 #5
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:eu**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #6
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
21284702000130140200330127127127127661051161099711 23273109971031010809710511011646801059911611711410 101500200070008066114117115104000000000328400

I hope this helps a little bit?

Cor
Nov 21 '05 #7
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:en**************@TK2MSFTNGP10.phx.gbl...
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
21284702000130140200330127127127127661051161099711 23273109971031010809710511
01164680105991161171141010150020007000806611411711 5104000000000328400
I hope this helps a little bit?

Cor

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Alberto | last post: by
1 post views Thread by marco | last post: by
2 posts views Thread by marco | last post: by
6 posts views Thread by Patrick Dugan | last post: by
7 posts views Thread by lgbjr | last post: by
4 posts views Thread by John Swan | last post: by
4 posts views Thread by rodchar | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.