我正在尝试将图像保存在SQL Server数据库中。
我将图像转换为字节并存储在SQL Server中,但是现在我想将保存的字节转换为图像,并使用c#将其显示在asp.net中的标签上。
我尝试了很多,但没有找到解决方案。
这是代码(将字节转换为图像)
if (fImage.HasFile)
{
if (fImage.PostedFile.ContentType == "image/jpg" || fImage.PostedFile.ContentType == "image/jpeg" || fImage.PostedFile.ContentType == "image/png")
{
int filelenght = fImage.PostedFile.ContentLength;
byte[] imagebytes = new byte[filelenght];
fImage.PostedFile.InputStream.Read(imagebytes, 0, filelenght);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Insert into tbImage(Img) values(@img)";
cmd.Connection = con;
cmd.Parameters.AddWithValue("@img", imagebytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write("Image saved to database");
}
}
这样的事情将转换Byte[]
为Image
:
MemoryStream ms = new MemoryStream(byte);
Image i = Image.FromStream(ms);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句