Winforms Picturebox control

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

Guest

I have an image data field in a datarow (as in the Northwinds demo sql
database). What is the C# instruction syntax to display it in a Winform
picturebox control.
 
Hi,

You have to create a bitmap first
Save your image to either a temp file or to a memory stream. then use the
correct constructor of Bitmat
 
Hi GSSI,

You need to remove the sql image type header before you try to do anything with the data.
Feed the byte[] to a MemoryStream. Set the position of the MemoryStream to 78 (I think this is the size of the header). Then use Image.FromStream(MemoryStream)
 
Hi,

Morten Wennevik said:
Hi GSSI,

You need to remove the sql image type header before you try to do anything
with the data.
Feed the byte[] to a MemoryStream. Set the position of the MemoryStream
to 78 (I think this is the size of the header). Then use
Image.FromStream(MemoryStream)


I have never deal with a header in the image (or binary data) from SQL , I
store documents in an Image field and I have never need to remove any part
of it.
 
I'm pretty green at this. How do I get the data field into a byte array?
--
GSSI


Morten Wennevik said:
Hi GSSI,

You need to remove the sql image type header before you try to do anything with the data.
Feed the byte[] to a MemoryStream. Set the position of the MemoryStream to 78 (I think this is the size of the header). Then use Image.FromStream(MemoryStream)

I have an image data field in a datarow (as in the Northwinds demo sql
database). What is the C# instruction syntax to display it in a Winform
picturebox control.
 
Ok, usually you don't have to deal with the header, but if you are using the Northwind sample database, you do. If you aren't using the Northwind database, and are unaware of any header info don't adjust the position before loading the image.

Hi,

Morten Wennevik said:
Hi GSSI,

You need to remove the sql image type header before you try to do anything
with the data.
Feed the byte[] to a MemoryStream. Set the position of the MemoryStream
to 78 (I think this is the size of the header). Then use
Image.FromStream(MemoryStream)


I have never deal with a header in the image (or binary data) from SQL , I
store documents in an Image field and I have never need to remove any part
of it.
 
Morten, much appreciative of your help. It's almost working but still getting
an exception when I try to load the image into the control. Code is ...

"byte[] picByte = (byte[])(drUR[0]["Picture"]);
MemoryStream msPic = new MemoryStream(picByte);
msPic.Position=78;
pictureBox1.Image = (Image)Image.FromStream(msPic);"

Any thoughts?
--
GSSI


Morten Wennevik said:
Well, you need to query the database. This example might be helpful.

[How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET]
http://support.microsoft.com/default.aspx?scid=kb;en-us;309158

Following the sample you will end up with

MyData = (byte[])myRow["imgField"];


Continue with this


using(MemoryStream ms = new MemoryStream(MyData))
{
Image img = Image.FromStream(ms);
}


I'm pretty green at this. How do I get the data field into a byte array?
 
Ignacio:

drUR is a datarow[].
All variables seem to be initializing correctly. Moreover, that data seems
to be formatted correctly. (I can verify this by using a simple Access form
with a PictureBox control connected via an Access Project setup.)

I don't get an explicit error message because of a surrounding try/catch
construct.
 
Don't set the position if you aren't using the Northwind database.

Morten, much appreciative of your help. It's almost working but still getting
an exception when I try to load the image into the control. Code is ...

"byte[] picByte = (byte[])(drUR[0]["Picture"]);
MemoryStream msPic = new MemoryStream(picByte);
msPic.Position=78;
pictureBox1.Image = (Image)Image.FromStream(msPic);"

Any thoughts?
 
Hi,


Well, what are you getting in the catch ?????

Unless you post the error message I have no idea why you are having this
problem.
Also see if the InnerException is populated

BTW, did you try to save the image to disk and read the file back?


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



GSSI said:
Ignacio:

drUR is a datarow[].
All variables seem to be initializing correctly. Moreover, that data seems
to be formatted correctly. (I can verify this by using a simple Access
form
with a PictureBox control connected via an Access Project setup.)

I don't get an explicit error message because of a surrounding try/catch
construct.
--
GSSI


Ignacio Machin ( .NET/ C# MVP ) said:
Hi,


is dr a datatable?

What is the error you are getting?
 
Ignacio:

The Error Message is "INVALID PARAMETER USED.". The Inner Exception is null.

Yes, I have copied the field to disk and have been able to display it in
Paint.

Sorry for being so clutsy!
--
GSSI


Ignacio Machin ( .NET/ C# MVP ) said:
Hi,


Well, what are you getting in the catch ?????

Unless you post the error message I have no idea why you are having this
problem.
Also see if the InnerException is populated

BTW, did you try to save the image to disk and read the file back?


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



GSSI said:
Ignacio:

drUR is a datarow[].
All variables seem to be initializing correctly. Moreover, that data seems
to be formatted correctly. (I can verify this by using a simple Access
form
with a PictureBox control connected via an Access Project setup.)

I don't get an explicit error message because of a surrounding try/catch
construct.
--
GSSI


Ignacio Machin ( .NET/ C# MVP ) said:
Hi,


is dr a datatable?

What is the error you are getting?
 
Morten: I AM using the Northwind database!

That's why I am so frustrated. In MS Access Project, it is a simple matter
to simply point a PictureBox control at an image and it will display it. I
don't expect C# and Winforms to be that much more complicated.
--
GSSI


Morten Wennevik said:
Don't set the position if you aren't using the Northwind database.

Morten, much appreciative of your help. It's almost working but still getting
an exception when I try to load the image into the control. Code is ...

"byte[] picByte = (byte[])(drUR[0]["Picture"]);
MemoryStream msPic = new MemoryStream(picByte);
msPic.Position=78;
pictureBox1.Image = (Image)Image.FromStream(msPic);"

Any thoughts?
 
Hi,
The Error Message is "INVALID PARAMETER USED.". The Inner Exception is
null.

Yes, I have copied the field to disk and have been able to display it in
Paint.

Sorry for being so clutsy!

What if you keep a reference to the MemoryStream in a member variable, IIRC
you need to have the stream available always.
 
Back
Top