all groups > dotnet windows forms > april 2005 >
You're in the

dotnet windows forms

group:

Load image from DB into PictureBox control


Load image from DB into PictureBox control K. Wilder
4/30/2005 3:00:53 PM
dotnet windows forms:
In VB.Net, does anyone know how to load an image from, example Northwind DB
employees table, to a picture box control?

The Image is in Byte format and I can't figure out how to convert it to an
image.

Thanks,

Re: Load image from DB into PictureBox control Tim Wilson
4/30/2005 6:21:08 PM
If the image is in a Byte array then you could create a MemoryStream using
this Byte array, then call the shared FromStream method of the Image class
passing in the MemoryStream. There's some sample code at the link below.

http://www.google.ca/groups?hl=en&lr=&selm=erQHZobLEHA.2388%40TK2MSFTNGP09.phx.gbl

--
Tim Wilson
..Net Compact Framework MVP

[quoted text, click to view]

Re: Load image from DB into PictureBox control K. Wilder
4/30/2005 6:25:16 PM
Tim,

I tried your Google example and got an "Invalid parameter used" error.

This is my code:

Dim ba() as Byte
ba = .Photo
Dim ms As New IO.MemoryStream(ba)
Me.picBox.Image = Image.FromStream(ms)

Any ideas?

Thanks,

King Wilder


[quoted text, click to view]
Re: Load image from DB into PictureBox control Jim Hughes
4/30/2005 7:22:24 PM
ba = CType( .Photo, Byte() )

[quoted text, click to view]

Re: Load image from DB into PictureBox control Tim Wilson
4/30/2005 10:33:54 PM
At what point do you get the exception? Are you sure that the ".Photo" value
represents a Byte array?

--
Tim Wilson
..Net Compact Framework MVP

[quoted text, click to view]

Re: Load image from DB into PictureBox control K. Wilder
5/1/2005 9:54:35 AM
Jim,

When I try the CType conversion I get the following error in my Task list:

Value of type '1-dimensional array of Byte' cannot be converted to 'Byte'.

I know it can be done. I've seen sample Northwind applications that have
done it but that was a long time ago and I never needed to know. Now I
don't remember what those applications were.

Any other ideas?

Thanks again,

King

[quoted text, click to view]
Re: Load image from DB into PictureBox control K. Wilder
5/1/2005 9:58:02 AM
Tim,

The error occurs on the line that follows:

Me.picBox.Image = Image.FromStream(ms)

I'm assuming its the ms parameter of the FromStream method.

Also I get an error in my Task List that indicates its a byte array:

Value of type '1-dimensional array of Byte' cannot be converted to 'Byte'.

I'm kina frustrated. Any other ideas?

Thanks,

King

[quoted text, click to view]
RE: Load image from DB into PictureBox control chuck rudolph
5/1/2005 12:55:01 PM
King, I recall seeing that the image in the NWDB had some extra header stuff
on it that you needed to skip over. I can't find that doc for you. But I can
give you some C# code that will write an image and read one. I'm sure you can
then get your vb to run.
Holler back with more questions or you figure out what the byte offset is
for northwind so I can write it down this time...Chuck

The code is driving a sql db, the image table has a id and an image column.
Here is the save the images into the db:

this.sqlInsertCommand1.Parameters["@testId"].Value =
int.Parse(this.txttestId.Text);
// save the data to a stream
MemoryStream ms = new MemoryStream();
this.pBox1.Image.Save(ms,ImageFormat.Jpeg);
// now save the stream as a byte array
byte[] imageBytes = new byte[ms.Length];
ms.Seek(0,SeekOrigin.Begin); // must rewind from the write
ms.Read(imageBytes,0,(int)ms.Length);
this.sqlInsertCommand1.Parameters["@testPic"].Value = imageBytes;
this.sqlInsertCommand1.ExecuteNonQuery();

And here is the read code:

SqlCommand sc = new SqlCommand("Select testPic from imageTest where
testId=@testId;",this.sqlConnection1);
sc.Parameters.Add(new SqlParameter("@testId",SqlDbType.Int));
sc.Parameters["@testId"].Value = int.Parse(this.txttestId.Text);
byte[] dbData = (byte[])sc.ExecuteScalar();
MemoryStream ms = new MemoryStream();
ms.Write(dbData,0,dbData.Length);
Bitmap bm = new Bitmap(ms);
this.pBox1.Image = bm;
Re: Load image from DB into PictureBox control Tim Wilson
5/1/2005 1:29:58 PM
So it sounds like the MemoryStream may not be holding bytes that represent a
valid image. Again, are you sure that the bytes of the image are stored
properly in the byte array?

--
Tim Wilson
..Net Compact Framework MVP

[quoted text, click to view]

Re: Load image from DB into PictureBox control K. Wilder
5/1/2005 7:54:02 PM
Tim,

No, I'm not sure. I don't really know how the Photo binaries in the
Northwind database are. Is there a way to find out?

Thanks,

King

[quoted text, click to view]
RE: Load image from DB into PictureBox control K. Wilder
5/1/2005 7:59:01 PM
Chuck,

I modified my code to use ms.Write(MybyteArray) and the Bitmap, but I still
get the same error saying "Invalid parameter used" on the Bitmap line with
the ms parameter.

I can't tell if it's something wrong that I'm doing or that the images in
the database are not actually images.

Thanks for the help. I'll keep looking around.

King

[quoted text, click to view]
Re: Load image from DB into PictureBox control Tim Wilson
5/2/2005 12:02:01 AM
Try digging through some of the threads at the link below. They look like
they have some additional information.
http://groups-beta.google.com/groups?q=northwind+database+photo+group:microsoft.public.dotnet*&start=0

--
Tim Wilson
..Net Compact Framework MVP

[quoted text, click to view]

RE: Load image from DB into PictureBox control chuck rudolph
5/2/2005 9:22:47 AM
King, I think that message says that there is no data in the database. You
can get there pretty easy by forgetting to rewind the memory stream on the
"put the data into the database" side after you bitmap.save(memorystream) and
before you output the memory stream to the byte array...Chuck

AddThis Social Bookmark Button