| re: Displaying blob data in a picturebox
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 <wschlichtman@discussions.microsoft.com> wrote:
[color=blue]
> 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();
> }
>
>[/color]
--
Happy coding!
Morten Wennevik [C# MVP] |