Sunday, March 25, 2012

Bind Image Data to PictureBox

Hi,
I'm just starting w/ VB.NET and want to find out how to bind an image from a
field in a SQL Server database table to a PictureBox control. I am able to
get data from the database into text boxes, etc., and to navigate and query
to different records, but coming from the Access world, I'm not sure how to
get image data displayed. We want to scan a document (single page), and sav
e
it with it's related record and just be able to view it in a Windows Form.
I've tried a few things that haven't worked and would really appreciate some
help!
--
PaulJS(This problem should be posted in a .NET Group)
For the example of o picturebox you should go along like this (picturebox).
Binding myBind = myPBox.DataBindings.Add("Image", myDataTable,
myDataColumn);
myBind.Format += new ConvertEventHandler(FormatImageData);
private void FormatImageData(object sender, ConvertEventArgs e)
{
if (e.DesiredType == typeof(Image))
{
System.Byte[] bytes = e.Value as Byte[];
System.IO.MemoryStream stream
= new System.IO.MemoryStream(bytes);
System.Drawing.Image image
= System.Drawing.Image.FromStream(stream);
stream.Close();
// Re-assign formatted data
e.Value = image;
}
}
HTH, Jens Suessmeyer.

No comments:

Post a Comment