Graphics from an SQL-Server table
Messages   Related Types
This message was discovered on ASPFriends.com 'winforms-cs' list.


Per Erik Lorentzen
I have an SQL -Server table, with a column with DataType=3DImage.

Now, I would like to bind this column, the image that is, to a control's =
property in a WinForm. How???

Any help would be most appreciated!

Med hilsen
Per Erik Lorentzen
SK Data as
Click here to reveal e-mail address

Reply to this message...
 
    
Ryan Trudelle-Schwarz
Not sure this is the best way but I'd do it like so:

System.Byte[]
bytes = command.Parameters("Image").Value; // or however

System.IO.MemoryStream
stream = new System.IO.MemoryStream(bytes);

System.Drawing.Image
image = System.Drawing.Image.FromStream(stream);

stream.Close();

myControl.Image = image;

or some such.

Hth, Ryan

-> -----Original Message-----
-> From: Per Erik Lorentzen [mailto:Click here to reveal e-mail address]
->
-> I have an SQL -Server table, with a column with DataType=Image.
->
-> Now, I would like to bind this column, the image that is, to a control's
-> property in a WinForm. How???
->
-> Any help would be most appreciated!
->
-> Med hilsen
-> Per Erik Lorentzen
-> SK Data as
-> Click here to reveal e-mail address

---
[This E-mail scanned for viruses by Declude Virus]

Reply to this message...
 
    
Erik Brown
The previous post is a good way to extract image data from a known
location and convert it into an image. If you are looking to bind a
series of image entries to a control, you will need to do a bit more.

To summarize, you use the Control.DataBindings property to bind to your
control, for example to a PictureBox:

    Binding myBind = myPBox.DataBindings.Add("Image", myDataTable,
myDataColumn);

This returns the Binding object for the newly created binding. This can
be used to add a Format event for converting the image data into an
image object:

    myBind.Format += new ConvertEventHandler(FormatImageData);

You can look up this method and the associated handler and arguments in
the documentation. Your handler processes the incoming data and
converts it to the desired type. Something like:

    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;
     }
    }

Good luck with it.

Erik Brown
==========
Author of "Windows Forms Programming with C#"
www.manning.com/eebrown

[Original message clipped]

Reply to this message...
 
    
Per Erik Lorentzen
Thanks Erik!

I have tried this approach, but already at the myBind =3D ... where the =
"Image" property should be bound to the column in the table, I get the =
exception:

An unhandled exception of type 'System.FormatException' occurred in =
system.windows.forms.dll

Additional information: Could not format the value to the desired type.

Also, if the field in the table contains nothig, I get this exception:

An unhandled exception of type 'System.InvalidCastException' occurred in =
mscorlib.dll

Additional information: Invalid cast from System.DBNull to =
System.Drawing.Image.

What am I doing wrong here?

Med hilsen
Per Erik Lorentzen
SK Data as
Click here to reveal e-mail address
----- Original Message -----=20
From: "Erik Brown" <Click here to reveal e-mail address>
To: "winforms-cs" <Click here to reveal e-mail address>
Sent: Wednesday, May 29, 2002 3:44 AM
Subject: [winforms-cs] RE: Graphics from an SQL-Server table

[Original message clipped]

Reply to this message...
 
    
Scott Berry
You should handle the case where e.Value =3D=3D null or
e.Value.Equals(DBNull.Value) before grabbing "e.Value as Byte[]" (have
an if block for this, and everything else below goes in the else block).
What you'd want to do in this case, I'm not sure: you could set e.Value
to null, or you could have a special "No Image Available"-type image.
    -Scott

This posting is provided "AS IS" with no warranties, and confers no
rights.

-----Original Message-----
From: Per Erik Lorentzen [mailto:Click here to reveal e-mail address]=20
Sent: Wednesday, May 29, 2002 12:59 AM
To: winforms-cs
Subject: [winforms-cs] RE: Graphics from an SQL-Server table

Thanks Erik!

I have tried this approach, but already at the myBind =3D ... where the
"Image" property should be bound to the column in the table, I get the
exception:

An unhandled exception of type 'System.FormatException' occurred in
system.windows.forms.dll

Additional information: Could not format the value to the desired type.

Also, if the field in the table contains nothig, I get this exception:

An unhandled exception of type 'System.InvalidCastException' occurred in
mscorlib.dll

Additional information: Invalid cast from System.DBNull to
System.Drawing.Image.

What am I doing wrong here?

Med hilsen
Per Erik Lorentzen
SK Data as
Click here to reveal e-mail address
----- Original Message -----=20
From: "Erik Brown" <Click here to reveal e-mail address>
To: "winforms-cs" <Click here to reveal e-mail address>
Sent: Wednesday, May 29, 2002 3:44 AM
Subject: [winforms-cs] RE: Graphics from an SQL-Server table

[Original message clipped]

| [winforms-cs] member Click here to reveal e-mail address =3D YOUR ID
| http://www.asplists.com/asplists/winforms-cs.asp =3D JOIN/QUIT

Reply to this message...
 
    
Per Erik Lorentzen
Thanks again, but no luck.

It seems as if the "Image" type of a field in an SQL Server 2000 table =
does not handle the Byte[] conversion to an image in the C# code. The =
DBNull.Value problem I solved with the same approach you pointed out, =
and that works fine, I just stream an "empty" .GIF file from disk when =
this value appears.

I've tried the same approach with streaming from .BMP and .GIF files, =
and it works just fine. But from the Image field, no luck!

Is there any other field type in an SQL Server 2000 table that would =
accomodate this feature? Otherwise it seems I've got to go back to =
storing filenames, and streaming the images in .GIF format from disk...

Med hilsen
Per Erik Lorentzen
SK Data as
Click here to reveal e-mail address

----- Original Message -----=20
From: "Scott Berry" <Click here to reveal e-mail address>
To: "winforms-cs" <Click here to reveal e-mail address>
Sent: Wednesday, June 05, 2002 9:55 PM
Subject: [winforms-cs] RE: Graphics from an SQL-Server table

[Original message clipped]

Reply to this message...
 
    
Scott Berry
Hmmm...Image should work here. I have a class that does this, but I
left it in my other outfit (it's at home) -- I'll try to remember to
grab it tonight & send to you tomorrow.
    -Scott

-----Original Message-----
From: Per Erik Lorentzen [mailto:Click here to reveal e-mail address]=20
Sent: Wednesday, June 05, 2002 3:07 PM
To: winforms-cs
Subject: [winforms-cs] RE: Graphics from an SQL-Server table

Thanks again, but no luck.

It seems as if the "Image" type of a field in an SQL Server 2000 table
does not handle the Byte[] conversion to an image in the C# code. The
DBNull.Value problem I solved with the same approach you pointed out,
and that works fine, I just stream an "empty" .GIF file from disk when
this value appears.

I've tried the same approach with streaming from .BMP and .GIF files,
and it works just fine. But from the Image field, no luck!

Is there any other field type in an SQL Server 2000 table that would
accomodate this feature? Otherwise it seems I've got to go back to
storing filenames, and streaming the images in .GIF format from disk...

Med hilsen
Per Erik Lorentzen
SK Data as
Click here to reveal e-mail address

----- Original Message -----=20
From: "Scott Berry" <Click here to reveal e-mail address>
To: "winforms-cs" <Click here to reveal e-mail address>
Sent: Wednesday, June 05, 2002 9:55 PM
Subject: [winforms-cs] RE: Graphics from an SQL-Server table

[Original message clipped]

| [winforms-cs] member Click here to reveal e-mail address =3D YOUR ID
| http://www.asplists.com/asplists/winforms-cs.asp =3D JOIN/QUIT

Reply to this message...
 
    
Scott Berry
This works for me. dataSet21.Tables[0] is Northwind.Employees: it's
stored in a funky Access format, and the actual Bitmap starts with
characters "BM".

            Byte[] b =3D
(Byte[])dataSet21.Tables[0].Rows[0]["Photo"];
            int i;
            for (i=3D0; i<b.Length - 2; i++) {
                if ((Char)b[i] =3D=3D 'B' && (Char)b[i+1] =3D=3D
'M')
                    break;
            }
            if (i >=3D b.Length - 2)
                MessageBox.Show("No BITMAP!");
            try {
                MemoryStream ms =3D new MemoryStream(b, i,
b.Length - i);
                Image bm =3D Image.FromStream(ms);
                this.BackgroundImage =3D bm;
            }
            catch (Exception ex) {
                MessageBox.Show(ex.ToString());
            }

    -Scott

    This posting is provided "AS IS" with no warranties, and confers
no rights.
-----Original Message-----
From: Scott Berry=20
Sent: Thursday, June 06, 2002 10:51 AM
To: 'winforms-cs'
Subject: RE: [winforms-cs] RE: Graphics from an SQL-Server table

Hmmm...Image should work here. I have a class that does this, but I
left it in my other outfit (it's at home) -- I'll try to remember to
grab it tonight & send to you tomorrow.
    -Scott

-----Original Message-----
From: Per Erik Lorentzen [mailto:Click here to reveal e-mail address]=20
Sent: Wednesday, June 05, 2002 3:07 PM
To: winforms-cs
Subject: [winforms-cs] RE: Graphics from an SQL-Server table

Thanks again, but no luck.

It seems as if the "Image" type of a field in an SQL Server 2000 table
does not handle the Byte[] conversion to an image in the C# code. The
DBNull.Value problem I solved with the same approach you pointed out,
and that works fine, I just stream an "empty" .GIF file from disk when
this value appears.

I've tried the same approach with streaming from .BMP and .GIF files,
and it works just fine. But from the Image field, no luck!

Is there any other field type in an SQL Server 2000 table that would
accomodate this feature? Otherwise it seems I've got to go back to
storing filenames, and streaming the images in .GIF format from disk...

Med hilsen
Per Erik Lorentzen
SK Data as
Click here to reveal e-mail address

----- Original Message -----=20
From: "Scott Berry" <Click here to reveal e-mail address>
To: "winforms-cs" <Click here to reveal e-mail address>
Sent: Wednesday, June 05, 2002 9:55 PM
Subject: [winforms-cs] RE: Graphics from an SQL-Server table

[Original message clipped]

| [winforms-cs] member Click here to reveal e-mail address =3D YOUR ID
| http://www.asplists.com/asplists/winforms-cs.asp =3D JOIN/QUIT

Reply to this message...
 
 
System.Byte
System.DBNull
System.Drawing.Image
System.FormatException
System.InvalidCastException
System.IO.MemoryStream
System.Web.UI.Control
System.Web.UI.MobileControls.Image
System.Windows.Forms.ConvertEventArgs
System.Windows.Forms.ConvertEventHandler
System.Windows.Forms.MessageBox
System.Windows.Forms.PictureBox




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com