从byte []转换为image [英] convert from byte[] to image
本文介绍了从byte []转换为image的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在数据库中有一个byte []格式的图像。我使用execute scalar()method.how检索该图像,以在asp.net图像控件中显示该图像
hi i have an image of byte[] format in database . I retrieve that image using execute scalar() method.how to display that image in asp.net image control
推荐答案
查看此article [ ^ ] :)
-KR
See this article[^] :)
-KR
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
了解更多详情,请参阅以下链接
C#Image to Byte Array和字节数组到图像转换器类 [ ^ ]
尝试这样的事情
使用(SqlConnection conn = ...)
{
conn.Open();
using(SqlCommand cmd = new SqlCommand(SELECT Picture FROM< tablename> WHERE ...,conn)
using(SqlDataReader reader = cmd.ExecuteReader())
{
if(reader.Read())
{
byte [] picData = reader [Picture] as byte [] ?? null;
if(picData!= null)
{
using(MemoryStream ms = new MemoryStream(picData))
{
//从内存流加载图像。你是如何做到的取决于你是否使用Windows窗体或WPF
//
//对于Windows窗体,你可以这样写:
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);
}
}
}
}
}
Try something like this
using (SqlConnection conn = ...)
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT Picture FROM <tablename> WHERE ...", conn)
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
byte[] picData= reader["Picture"] as byte[] ?? null;
if (picData!= null)
{
using (MemoryStream ms = new MemoryStream(picData))
{
// Load the image from the memory stream. How you do it depends
// on whether you're using Windows Forms or WPF.
// For Windows Forms you could write:
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);
}
}
}
}
}
这篇关于从byte []转换为image的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文