Displaying blob data in a picturebox

G

Guest

I'm attempting to retrieve a bitmap from an image field in SQL Server 2005
using Visual Studio 2005 C#. I then want to load the bitmap into a
picturebox. When I run the following code, I get the error message:

Parameter is no valid.

Debugging shows that the Image.FromStream(ms) statement is the culprit.

Anyone know what I'm doing wrong?

-----------------------------------------

using (SqlConnection sqlConn = new SqlConnection(builder.ConnectionString))
{
sqlConn.Open();
SqlDataAdapter sql = new SqlDataAdapter(
"SELECT Picture FROM Pictures WHERE Pictures.CardID=5", sqlConn);
DataSet ds1 = new DataSet();
sql.Fill(ds1, "Pictures");
DataRow dr = ds1.Tables["Pictures"].Rows[0];

byte[] result = (byte[])dr["Picture"];
int ArraySize = result.GetUpperBound(0);

MemoryStream ms = new MemoryStream(result, 0, ArraySize);
pictureBox1.Image = Image.FromStream(ms);
sqlConn.Close();
}
 
M

Morten Wennevik

Hi wsclichtman,

I'm guessing SQL Server 2005 uses the same image header as 2000, so you need to remove the first 80(?) bytes from the stream before creating a Bitmap out of it.


I'm attempting to retrieve a bitmap from an image field in SQL Server 2005
using Visual Studio 2005 C#. I then want to load the bitmap into a
picturebox. When I run the following code, I get the error message:

Parameter is no valid.

Debugging shows that the Image.FromStream(ms) statement is the culprit.

Anyone know what I'm doing wrong?

-----------------------------------------

using (SqlConnection sqlConn = new SqlConnection(builder.ConnectionString))
{
sqlConn.Open();
SqlDataAdapter sql = new SqlDataAdapter(
"SELECT Picture FROM Pictures WHERE Pictures.CardID=5", sqlConn);
DataSet ds1 = new DataSet();
sql.Fill(ds1, "Pictures");
DataRow dr = ds1.Tables["Pictures"].Rows[0];

byte[] result = (byte[])dr["Picture"];
int ArraySize = result.GetUpperBound(0);

MemoryStream ms = new MemoryStream(result, 0, ArraySize);
pictureBox1.Image = Image.FromStream(ms);
sqlConn.Close();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top