我已将图像上传到我的数据库中byte[]
,现在我试图将其显示出来。
出现错误 - 用户代码未处理参数异常Parameter is not valid
在这一行
newImage = System.Drawing.Image.FromStream(stream);
这是我的代码
for (int i = 0; i < topRatingList.Count; i++)
{
int commentRating = topRatingList[i].CommentRating;
int drinkID = topRatingList[i].DrinkID;
if (i == 0)
{
DrinkMenuDAO drink = DrinkMenuBLL.getDrinkMenu(drinkID);
LinkButton1.Text = drink.DrinkName;
ImageButton1.ImageUrl = byteArrayToImage(drink.DrinkImage);
}
}
private string byteArrayToImage(byte[] byteArrayIn)
{
System.Drawing.Image newImage;
string strFileName = Server.MapPath("~/Temp/images/") + ".jpg";
if (byteArrayIn != null)
{
using (MemoryStream stream = new MemoryStream(byteArrayIn))
{
newImage = System.Drawing.Image.FromStream(stream);
newImage.Save(strFileName);
}
return strFileName;
}
else
{
return "";
}
}
我用来存储图像的代码
protected void bn_upload_Click(object sender, EventArgs e)
{
lbl_msg.Text = "";
Stream imgStream = FileUpload1.PostedFile.InputStream;
BinaryReader imgBinary = new BinaryReader(imgStream);
bytes = imgBinary.ReadBytes((Int32)imgStream.Length);
imgBinary.Close();
imgStream.Close();
string src = byteArrayToImage(bytes);
if (src.Equals(""))
{
}
else
{
Image1.ImageUrl = "~/Temp/UploadedImage.jpg";
}
}
private string byteArrayToImage(byte[] byteArrayIn)
{
System.Drawing.Image newImage;
string strFileName = Server.MapPath("~/Temp/") + "UploadedImage.jpg";
if (byteArrayIn != null)
{
using (MemoryStream stream = new MemoryStream(byteArrayIn))
{
newImage = System.Drawing.Image.FromStream(stream);
newImage.Save(strFileName);
}
return strFileName;
}
else
{
return "";
}
}
protected void btn_submit_Click(object sender, EventArgs e)
{
DrinkMenuBLL.uploadImg(bytes);
lbl_msg.Text = "uploaded";
}
我varbinary(MAX)
在数据库和我使用的sql中使用
public static void UploadImg(byte[] drinkImage)
{
SqlConnection con = new SqlConnection(Constring.getConString());
string sql = "Update DrinkMenu set drinkImage = (@imgData) where drinkID = 4";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@imgData", SqlDbType.Binary).Value = drinkImage;
try
{
con.Open();
cmd.ExecuteNonQuery();
}
finally
{
con.Close();
cmd.Dispose();
con.Dispose();
}
}