嗨我试图从数据库中获取一个字节数组,并将其转换为我可以用来从我的.aspx页面中的数据库显示图像的东西。我严格使用C#。字节数组到图像显示
这是我的代码。
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);
的代码可以编译和运行,但是当它到达的地方执行行 MYIMAGE = Bitmap.FromStream(插播广告)我得到这个错误System.ArgumentException点:参数是无效的。实际上,我通过查看各种不同的来源获得了这些代码,所以也许有人在这里可以告诉我,如果我做错了什么。
谢谢!
请在[参数化查询(HTTP读了起来:// www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html) –
http://bobby-tables.com/ –
这就是你需要的:'Image .FromStream(new MemoryStream((byte [])picCommand.ExecuteScalar()));' – Magnus