By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,581 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Displaying blob data in a picturebox

P: n/a
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();
}

Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.
On Tue, 20 Sep 2005 02:23:02 +0200, wschlichtman <ws**********@discussions.microsoft.com> wrote:
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();
}


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.