Mahesh819

Hi,

I am trying to display an image from the sql 2000 database on to the picture box control on windows app (VS 2.0).

I am able to pull the image in to byte array. I also am able to put the byte array in to the Memory stream by writing it like stream.Write(image, 0, image.Length)

However, when i try to create a bitmap out of the memory stream it thorows an exception stating "parameter is not valid".

The second approach I tried was to use Image.FromStream(ms1) method, but it also says "parameter is not valid" when it tries to get the image from stream (memory stream).

The third approach i tried was writing to a temporary file stream and then read from file using Image.FromFile(strfn, true). This line gets an error stating "Out of memory".

Nothing seems to be working. Please shed some light if possible. All i want to do is display an image from sql db field(image field) on to picture box in windows application using C# 2.0

Please look at the code below for detail. The line in bold where I am getting my exceptions.


// Put user code to initialize the page here
MemoryStream stream = new MemoryStream();
SqlConnection connection = new SqlConnection(@"my connection string");
try
{
connection.Open();
SqlCommand command = new SqlCommand("select image from images Where EntryDate > '2/15/2007'", connection);
byte[] image = (byte[])command.ExecuteScalar();
stream.Write(image, 0, image.Length);

Bitmap bitmap = new Bitmap(stream);

/////testing 2
MemoryStream ms1 = new MemoryStream(image);
exceptionPictureBox.Image = Image.FromStream(ms1);

///testing 3
string strfn = Convert.ToString(DateTime.Now.ToFileTime());
FileStream fs = new FileStream(strfn, FileMode.CreateNew, FileAccess.Write);

fs.Write(image, 0, image.Length);
fs.Flush();
fs.Close();

exceptionPictureBox.Image = Image.FromFile(strfn, true);
/////testing 3



}
finally
{
connection.Close();
stream.Close();
}

Thanks
Needy



Re: Visual C# Language Display image from SQL DB on to Picturebox In Windows Forms App Using C# 2.0

Shah Sahil

command.ExecuteScalar() will not be at a help here. Instead use a DataReader object to get the byte array for the image. I am writing a sample code, but it is in VB. I hope you could get the main functionality and convert it to C#. Kindly re-post the C# code in case it helps.

Dim cn As New System.Data.OleDb.OleDbConnection("Your Connection String")
Dim cmd As New System.Data.OleDb.OleDbCommand("Your Select Command", cn)
Dim dr As System.Data.OleDb.OleDbDataReader
Dim pictureColumn As Integer = 0
Dim b(Integer.MaxValue - 1) As Byte 'Comment: Max of 2 Gb image can be copied in this array.

cn.Open()
dr = cmd.ExecuteReader()
dr.Read()
dr.GetBytes(pictureColumn, 0, b, 0, b.Length)
dr.Close()
cn.Close()

MessageBox.Show("Bytes read from database: " & b.Length.ToString())

Dim bs As System.IO.Stream = New System.IO.MemoryStream()
bs.Write(b, 0, b.Length)

PictureBox1.Image = Image.FromStream(bs)






Re: Visual C# Language Display image from SQL DB on to Picturebox In Windows Forms App Using C# 2.0

Figo Fei - MSFT

Hi,

You don¡¯t need to post the same issue again. We will focus on one thread.

Thank you for understanding.






Re: Visual C# Language Display image from SQL DB on to Picturebox In Windows Forms App Using C# 2.0

Mahesh819

I figured out the solution.

Here it is in C#. Let me state the reason as well. The picture box control on windows app doesn't take the image from methods like Image.FromStream. It only takes BitMap as an image for some reason. I don't know if someone can explain this behaviour. I had seen the samples on msdn for ms vs 1.1 which took image but for some reason 2.0 only takes bitmap as a image source. Here is the working code in C#

private void viewSnapShotButton_Click(object sender, EventArgs e)

{

this.Cursor = Cursors.WaitCursor;

string connectionString = ConfigurationManager.AppSettings["myCconnectionSstring"];

string queryString = ConfigurationManager.AppSettings["MyQueryString"];

MemoryStream stream = new MemoryStream();

SqlConnection connection = new SqlConnection(connectionString);

try

{

connection.Open();

SqlCommand command = new SqlCommand(queryString, connection);

byte[] image = (byte[])command.ExecuteScalar();

MemoryStream ms1 = new MemoryStream(image);

exceptionPictureBox.Image = Bitmap.FromStream (ms1); //this is how it should be. I was using Image.FromStream and was getting error.

}

finally

{

connection.Close();

stream.Close();

}

this.Cursor = Cursors.Default;

}