This message was discovered on ASPFriends.com 'winforms-cs' list.
| Per Erik Lorentzen |
I've implemented the following code:
In the Form_Load: Binding imageBind =3D new Binding("Image", = DataClass._dataSet.Tables[DataClass.TableName], "Drawing"); // Column = "Drawing" contains the images
imageBind.Format +=3D new ConvertEventHandler(FormatImageData);
this.pictureBox1.DataBindings.Add(imageBind);
private void FormatImageData(object sender, ConvertEventArgs e)
{
if (e.DesiredType =3D=3D typeof(Image))
{
byte[] bytes =3D e.Value as byte[];
System.Drawing.Image image =3D = System.Drawing.Image.FromFile("empty.gif"); // Empty image
if (bytes !=3D null)
{
try
{
System.IO.MemoryStream stream =3D new System.IO.MemoryStream(bytes);
image =3D System.Drawing.Image.FromStream(stream);
stream.Close();
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString(), "Warning!");
}
}
// Re-assign formatted data
e.Value =3D image;
}
}
The exception I get is:
System.ArgumentException : Invalid parameter used.
This is the "stream" parameter to the FromStream method.
The database column "Drawing" is defined as "Image", so I can't see = where I'm in error here...
Med hilsen Per Erik Lorentzen SK Data as Click here to reveal e-mail address
|
|
| |
| |
| Erik Brown |
As you appear to have discovered, you need to create the Binding instance separately and add the Format handler before establishing the binding (as opposed to what I did in my original post):
myBind = new Binding(...); myBind.Format += ...; pBox.DataBindings.Add(myBind);
In the method used in my original post, the framework attempts to bind immediately and gets the FormatException error you originally received. The other error you mentioned in your prior post may occur if the value is DBNull.
As to your current error (described below), I have not actually made this work, so I'm not certain what the problem might be (I'm fairly certain this is the right approach, however). My suspicion would be the byte[] array and the cast of the Value to this array.
Can you convert a byte array (in memory or from a file, or based on your empty image) into an Image successfully (without the database)? If so, are you certain that your database holds the complete set of data for the image.
Any other ideas out there?
Erik Brown ============ Author of "Windows Forms Programming with C#" http://www.amazon.com/exec/obidos/ASIN/1930110286
[Original message clipped]
|
|
| |
|
| |
| Scott Berry |
Do you know what type of data you actually have in the Drawing column? The SQL type "Image" and the .NET class "Image" aren't the same -- for instance, the "Image" column from Northwind's Employees table is a bitmap that was copied from an Access database. Access throws a header on a bitmap, so before you can grab the bitmap, you have to hack off the offending header characters. Another possibility is that the type of file that was stored isn't supported as an Image (Bitmap and Metafile). -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 5:21 AM To: winforms-cs Subject: [winforms-cs] More Graphics from an SQL-Server table column
I've implemented the following code:
In the Form_Load: Binding imageBind =3D new Binding("Image", DataClass._dataSet.Tables[DataClass.TableName], "Drawing"); // Column "Drawing" contains the images
imageBind.Format +=3D new ConvertEventHandler(FormatImageData);
this.pictureBox1.DataBindings.Add(imageBind);
private void FormatImageData(object sender, ConvertEventArgs e)
{
if (e.DesiredType =3D=3D typeof(Image))
{
byte[] bytes =3D e.Value as byte[];
System.Drawing.Image image =3D = System.Drawing.Image.FromFile("empty.gif"); // Empty image
if (bytes !=3D null)
{
try
{
System.IO.MemoryStream stream =3D new System.IO.MemoryStream(bytes);
image =3D System.Drawing.Image.FromStream(stream);
stream.Close();
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString(), "Warning!");
}
}
// Re-assign formatted data
e.Value =3D image;
}
}
The exception I get is:
System.ArgumentException : Invalid parameter used.
This is the "stream" parameter to the FromStream method.
The database column "Drawing" is defined as "Image", so I can't see where I'm in error here...
Med hilsen Per Erik Lorentzen SK Data as Click here to reveal e-mail address
| [winforms-cs] member Click here to reveal e-mail address =3D YOUR ID | http://www.asplists.com/asplists/winforms-cs.asp =3D JOIN/QUIT
|
|
| |
| |
| Can Ozturhan |
Hi,
If you have a byte[] that represents an image (ie:image column type in SQL Server) try this to output the image to a ASP.NET response stream:
Response.Clear(); Response.ContentType = "image/gif"; //Put the MimeType here BinaryFormatter formatter = new BinaryFormatter(); MemoryStream stream = new MemoryStream(byteArray); System.Drawing.Image image = (System.Drawing.Image)formatter.Deserialize(stream); image.Save(Response.OutputStream, image.RawFormat);
Good luck! Can Ozturhan
|
|
| |
|
|
|
|
|
|