472,373 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,373 software developers and data experts.

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 11746
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alberto | last post by:
I'm trying to load an image in a PictureBox but I want the PictureBox has always the same size and if the image is bigger, show a scrollBars. How can I do it? Thank you
1
by: marco | last post by:
I'm a newbie and i'm doing a little application that open image. I don't understand what i've to do to have a zoom of an image...i don't know where's the error. If anyone know a simple code tell me...
2
by: marco | last post by:
I'm a newbie and i'm doing a little application in C# for a Pocket PC that open image. I don't understand what i've to do to have a zoom of an image...i don't know where's the error. If anyone...
6
by: Patrick Dugan | last post by:
Hello, I'm trying to load different images (icons) into a PictureBox1.Image. The first image loads just fine, but the second image always returns the error "Invalid property used." It doesn't...
3
by: Tom | last post by:
I have a picturebox on my VB.NET form. The picturebox size mode is set to stretched. I then load an image into that form and display it. As the user moves the mouse over the form, I want to get and...
7
by: lgbjr | last post by:
Hello All, Im using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
4
by: John Swan | last post by:
Hello. I'm trying to create a simple program that amongst other things creates a thumbnail of an image (Bitmap) to a set size determined by the user in pixels? The problem is: All of the...
4
by: rodchar | last post by:
hey all, i have an image that i want to put on my form and was just wondering if there is a way to take away the white background on the image (make it transparent)? thanks, rodchar
0
by: dlamar | last post by:
I'm using VB.NET with 2.0 - I have a number of PictureBoxes in my application - all of which use a context menu to change the associated Image to one of 5 options. I need to maintain the state of...
3
by: Andrzej | last post by:
I have a picturebox on my C# .NET form. The picturebox size mode is set to zoom. I then load an image into that form and display it. As the user moves the mouse over the form, I want to get and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.