PC Review


Reply
Thread Tools Rate Thread

Displaying blob data in a picturebox

 
 
=?Utf-8?B?d3NjaGxpY2h0bWFu?=
Guest
Posts: n/a
 
      20th Sep 2005
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();
}

 
Reply With Quote
 
 
 
 
Morten Wennevik
Guest
Posts: n/a
 
      20th Sep 2005
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 <(E-Mail Removed)> 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]
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Displaying BLOB data in Access Darrell Childress Microsoft Access Forms 0 7th Dec 2009 08:02 PM
Help Displaying & Stabilizing PictureBox Contents The Confessor Microsoft VB .NET 1 8th Feb 2006 05:05 AM
displaying bmp in PictureBox =?Utf-8?B?Q2hyaXNn?= Microsoft Dot NET Compact Framework 1 10th Dec 2005 12:21 AM
Displaying BLOB data Jeff C. Microsoft Access Queries 3 12th Nov 2003 10:58 PM
error displaying an image in PictureBox rocio Microsoft Dot NET Compact Framework 1 12th Nov 2003 10:53 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:19 AM.