473,796 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Image from SQL to PictureBox

I have a column named "PictureIma ge" 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.datasetProdu ct.Tables("Prod uct").Rows(0)(" PictureImage")

Dim stmBLOBData As New MemoryStream(by tBLOBData)

Me.pictureProdu ct.Image = Image.FromStrea m(stmBLOBData)

The last line of code (image.fromstre am) 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 11909
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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Dim strConn As String

Dim conn As SqlClient.SqlCo nnection

Dim daCustomer As SqlClient.SqlDa taAdapter

ds = New DataSet

' strConn = "Provider = Microsoft.Jet.O LEDB.4.0;"

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

strConn = "Server = " + Environment.Mac hineName + "\VSdotNet; "

strConn &= "Database = NorthWind;"

strConn &= "Integrated Security = SSPI;"

conn = New SqlClient.SqlCo nnection(strCon n)

daCustomer = New SqlClient.SqlDa taAdapter("Sele ct * from Categories", conn)

ds = New DataSet

daCustomer.Fill (ds, "Categories ")

ListBox1.DataSo urce = ds.Tables("Cate gories")

ListBox1.Displa yMember = "CategoryNa me"

End Sub

Private Sub ListBox1_Select edValueChanged( ByVal sender As Object, ByVal e As
System.EventArg s) Handles ListBox1.Select edValueChanged

Dim dr As DataRow = ds.Tables("Cate gories").Rows(L istBox1.Selecte dIndex)

Dim ms As New System.IO.Memor yStream

Dim bm As Bitmap

Dim arData() As Byte = dr.Item("Pictur e")

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

bm = New Bitmap(ms)

PictureBox1.Ima ge = bm

End Sub

Ken

-----------------
"Gary Shell" <gs****@fuse.ne t> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
I have a column named "PictureIma ge" 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.datasetProdu ct.Tables("Prod uct").Rows(0)(" PictureImage")

Dim stmBLOBData As New MemoryStream(by tBLOBData)

Me.pictureProdu ct.Image = Image.FromStrea m(stmBLOBData)

The last line of code (image.fromstre am) 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***@bellsout h.net> wrote in message
news:u$******** *****@TK2MSFTNG P15.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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Dim strConn As String

Dim conn As SqlClient.SqlCo nnection

Dim daCustomer As SqlClient.SqlDa taAdapter

ds = New DataSet

' strConn = "Provider = Microsoft.Jet.O LEDB.4.0;"

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

strConn = "Server = " + Environment.Mac hineName + "\VSdotNet; "

strConn &= "Database = NorthWind;"

strConn &= "Integrated Security = SSPI;"

conn = New SqlClient.SqlCo nnection(strCon n)

daCustomer = New SqlClient.SqlDa taAdapter("Sele ct * from Categories", conn)
ds = New DataSet

daCustomer.Fill (ds, "Categories ")

ListBox1.DataSo urce = ds.Tables("Cate gories")

ListBox1.Displa yMember = "CategoryNa me"

End Sub

Private Sub ListBox1_Select edValueChanged( ByVal sender As Object, ByVal e As System.EventArg s) Handles ListBox1.Select edValueChanged

Dim dr As DataRow = ds.Tables("Cate gories").Rows(L istBox1.Selecte dIndex)

Dim ms As New System.IO.Memor yStream

Dim bm As Bitmap

Dim arData() As Byte = dr.Item("Pictur e")

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

bm = New Bitmap(ms)

PictureBox1.Ima ge = bm

End Sub

Ken

-----------------
"Gary Shell" <gs****@fuse.ne t> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
I have a column named "PictureIma ge" 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.datasetProdu ct.Tables("Prod uct").Rows(0)(" PictureImage")

Dim stmBLOBData As New MemoryStream(by tBLOBData)

Me.pictureProdu ct.Image = Image.FromStrea m(stmBLOBData)

The last line of code (image.fromstre am) 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******** *****@TK2MSFTNG P12.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******** ******@TK2MSFTN GP12.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(ByVa l sender As System.Object, ByVal _
e As System.EventArg s) Handles MyBase.Load
Dim conn As New SqlClient.SqlCo nnection("Serve r=Kamer;" & _
"DataBase=North wind; Integrated Security=SSPI")
Dim sqlstr As String = _
String.Format(" SELECT Photo FROM Employees WHERE EmployeeID = 1")
Dim cmd As New SqlClient.SqlCo mmand(sqlstr, conn)
conn.Open()
Dim rdr As SqlClient.SqlDa taReader = cmd.ExecuteRead er()
rdr.Read()
Dim arrImage() As Byte = DirectCast(rdr. Item("Photo"), Byte())
Dim enc As System.Text.Enc oding = System.Text.Enc oding.ASCII
Dim str As String = enc.GetString(a rrImage, 0, 78)
TextBox1.Text = str.Replace(chr (0), "."c)
Dim sb As New System.Text.Str ingBuilder
For Each chr As Char In str
sb.Append(Asc(c hr).ToString)
Next
TextBox2.Text = sb.ToString
rdr.Close()
conn.Close()
End Sub
///

The result from this was

Textbox1

/....
....!.Bit map Image.Paint.Pic ture....... ..PBrush....... .. T..

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

Textbox2
212847020001301 402003301271271 271276610511610 997112327310997 103101080971051 101164680105991 161171141010150 020007000806611 411711510400000 0000328400

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.Pic ture" 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******** ******@TK2MSFTN GP10.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(ByVa l sender As System.Object, ByVal _
e As System.EventArg s) Handles MyBase.Load
Dim conn As New SqlClient.SqlCo nnection("Serve r=Kamer;" & _
"DataBase=North wind; Integrated Security=SSPI")
Dim sqlstr As String = _
String.Format(" SELECT Photo FROM Employees WHERE EmployeeID = 1")
Dim cmd As New SqlClient.SqlCo mmand(sqlstr, conn)
conn.Open()
Dim rdr As SqlClient.SqlDa taReader = cmd.ExecuteRead er()
rdr.Read()
Dim arrImage() As Byte = DirectCast(rdr. Item("Photo"), Byte())
Dim enc As System.Text.Enc oding = System.Text.Enc oding.ASCII
Dim str As String = enc.GetString(a rrImage, 0, 78)
TextBox1.Text = str.Replace(chr (0), "."c)
Dim sb As New System.Text.Str ingBuilder
For Each chr As Char In str
sb.Append(Asc(c hr).ToString)
Next
TextBox2.Text = sb.ToString
rdr.Close()
conn.Close()
End Sub
///

The result from this was

Textbox1

/....
...!.Bitm ap Image.Paint.Pic ture....... ..PBrush....... .. T..

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

Textbox2
212847020001301 402003301271271 271276610511610 997112327310997 103101080971051 1
011646801059911 611711410101500 200070008066114 117115104000000 000328400
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
13374
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
12803
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 the url :O) private void Zoom200_Click(object sender, System.EventArgs e) { PictureBox.Width = PictureBox.Width + (PictureBox.Width * 2);
2
7471
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 know a simple code tell me the url :O) private void Zoom200_Click(object sender, System.EventArgs e) {
6
2147
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 matter what icons are loaded. The first always shows up and any icons after that give me the error. Here is the offending code: The "DestinationPath" variable is the full path and filename of the icon
3
63443
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 display (in the status bar) the image coordinates of the mouse location. However, if I use the picturebox's MouseMove event, I am getting the coordinates of the mouse over the PICTUREBOX, not the actual image underneath that (which is stretched)....
7
11641
by: lgbjr | last post by:
Hello All, I¡¯m 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 is proving to be more difficult. These pictureboxes are bound to an AccessDB. If the user wants to add an image, they select an image using an OpenFileDialog: Dim result As DialogResult = Pic_Sel.ShowDialog() If (result = DialogResult.OK) Then
4
2575
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 examples I have seen so far are in c#. Can anyone please provide reference to a c++ managed version. Thanks. John
4
1487
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
1493
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 these PictureBox.Image setting across application shutdown and subsequent startup. my.Settings is just the ticket, but alas, doing this the only way I find won't work for me because the PictureBox - Application Settings - Property Binding doesn't expose...
3
22535
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 display (in the status bar) the image coordinates of the mouse location. However, if I use the picturebox's MouseMove event, I am getting the coordinates of the mouse over the PICTUREBOX, not the actual image underneath that (which is zoomed). i.e....
0
9683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9529
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10457
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10231
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.