Hi I'm trying to get a byte array from a database and convert it to something I can use to display the image from the database in my .aspx page. I am using strictly c#.
Here is my code.
SqlCommand picCommand = connection.CreateCommand();
picCommand.CommandText = ("SELECT ItemImage FROM Inventory WHERE ItemName = '" + DropDownList1.SelectedItem.Text + "';");
connection.Open();
object returnPic;
returnPic = picCommand.ExecuteScalar();//value that is read as the byte array or intended to be read as byte array.
connection.Close();
UTF8Encoding utf8 = new UTF8Encoding();
//where i intend to convert the
byte[] image = utf8.GetBytes(returnPic.ToString());
System.Drawing.Image myImage;
using (MemoryStream inStream = new MemoryStream())
{
inStream.Write(image, 0, image.Length);
myImage = Bitmap.FromStream(inStream);
}
this.ItemImageBox.Equals(myImage);
The code compiles and runs but when it gets to the point where it executes the line myImage = Bitmap.FromStream(instream) i get this error System.ArgumentException: Parameter is not valid. I actually got this code from looking at various different sources so maybe someone on here can tell me if I am doing something wrong.
Thanks ahead!
Image.FromStream(new MemoryStream((byte[])picCommand.ExecuteScalar()));