473,387 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Displaying blob data in a picturebox

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
1 11516
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Lindsey Howell | last post by:
Hello, I've encountered a problem when trying to display BLOB images from a SQL database using VB.NET. This is the code which generates the error: Dim mySqlConnection As SqlConnection = New...
3
by: Peter Proost | last post by:
Hi I got this code to read in a file to the database, if use it to save a image from a picturebox to the database it works ok, but now I use it to directly save a file to the database without...
1
by: The Confessor | last post by:
(ellipses in place of code sections which do not deviate from defaults) Private Sub Form_Main_Load(...) Handles MyBase.Load For A = 0 To 16 For B = 0 To 14...
1
by: Hrvoje Voda | last post by:
I'm using this code to get a blob from sql database, and put it into picturebox, but I don't know how to load it into picturebox. Code: sqlConnection.Open(); ...
5
by: Mark | last post by:
I have a VB 6 program that I have saved an audio file to using DAO and the appendchunk as is shown at http://support.microsoft.com/kb/103257/EN-US/. ; I have it working fine in that app (it saves...
0
by: Marcus Kwok | last post by:
I have written a class (Windows Form) in Managed C++ that accepts an array of image filenames and will display them sequentially. All images will be the same size. I got it to work, but I know...
3
by: CD | last post by:
An application is logging faxes sent in SQL2000 image column type. I have found code on the net but what it is doing is prompting to save to local which is fine for single page image. Not good...
2
by: Vinciz | last post by:
hi guys... im new in java and i would love to learn some of these... basically i got a sample code to retrieve the blob from the mysql. however, i dont really know what to do with these...
11
by: Usenet User | last post by:
..NET 1.1/2.0 I have a need to display high-resolution scrollable images in a .NET application (Windows Forms). One well known solution is to create a Panel with AutoScroll set to "true" and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.