Load image from DB into PictureBox control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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,

King Wilder
 
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
 
ba = CType( .Photo, Byte() )

K. Wilder said:
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
 
At what point do you get the exception? Are you sure that the ".Photo" value
represents a Byte array?
 
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
 
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
 
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?
 
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;
 
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
 
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
 
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
 
Back
Top