将byte []转换为图像 [英] convert byte[] to image
本文介绍了将byte []转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已将图像作为 byte []
上载到我的数据库中,现在我正试图将其显示出来。
I have uploaded an Image in to my database as byte[]
and now im trying to display it out.
发生错误-用户代码未处理参数异常参数无效
There was an error - Argument Exception was unhandled by user code 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 "";
}
}
我用来存储图像的代码
the code that i used to store the image
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)$ c数据库中的$ c>和我使用的sql
I used varbinary(MAX)
in the database and the sql that i used
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();
}
}
推荐答案
一个问题可能是以下情况:
One problem could be the following one:
bytes = imgBinary.ReadBytes((Int32)imgStream.Length);
如果流的长度大于Int32.MaxValue,因为它是Int64,该怎么办?也许您正在截断图像...使用此方法代替,将流读取到缓冲区:
What if the stream Length is more than Int32.MaxValue as it's a Int64? Maybe you are truncating your image... use this method instead to read the stream to a buffer:
public static Byte[] ReadStream(Stream input)
{
Byte[] buffer = new Byte[(16 * 1024)];
using (MemoryStream stream = new MemoryStream())
{
Int32 read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
stream.Write(buffer, 0, read);
return stream.ToArray();
}
}
protected void bn_upload_Click(object sender, EventArgs e)
{
lbl_msg.Text = "";
Byte[] bytes = ReadStream(FileUpload1.PostedFile.InputStream);
String src = byteArrayToImage(bytes);
if (!src.Equals(""))
Image1.ImageUrl = "~/Temp/UploadedImage.jpg";
}
这篇关于将byte []转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文