如何将byte[]转换成Image?

海涵德 2011-01-20 07:18:41
如何将byte[]转换成Image
...全文
1072 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
EdlineHunter 2011-01-22
  • 打赏
  • 举报
回复
byte[]是2进制流,流可以转换成图片,图片就是流.....学习了
海涵德 2011-01-22
  • 打赏
  • 举报
回复
太复杂了,本意是提高效率,这样反而效率更低。
mxxxxxxx 2011-01-22
  • 打赏
  • 举报
回复
System.IO.MemoryStream ms = new System.IO.MemoryStream(Byte数组);
System.Drawing.Bitmap bmp = new Bitmap(ms);
pictureBox1.Image = bmp;
flyerwing 2011-01-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]
public static System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
{
if (byteArrayIn == null)
return null;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArrayIn))
……
[/Quote]
数组构建流,从流构建图然后就OK了.
心灵彩虹 2011-01-21
  • 打赏
  • 举报
回复
//获得二进制文件的方法
protected void SqlConn()
{
string aidSelect = "select emp_sjk_photo from emp_photo where emp_id = '" + id + "'";
DataTable fileTable = new DataTable();
fileTable = DbAccess.GetDS(aidSelect).Tables[0];//DbAccess.GetDS执行数据库公用方法
if (Convert.IsDBNull(fileTable.Rows[0][0]) == true)
{
Response.Write("<script>alert('没有照片!')</script>");
return;
}
byte[] photo = (byte[])fileTable.Rows[0][0];


string strPath = "~/photo/" + jhao + ".jpg";
string x_strPath = "~/photo/" + "x_" + jhao + ".jpg";
string strPhotoPath = Server.MapPath(strPath);
string strPhotoPath_s = Server.MapPath(x_strPath);// 服务器端缩略图路径
//保存图片文件
//this.Response.BinaryWrite(photo);
BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate));
bw.Write(photo);
bw.Close();
MakeThumbnail(strPhotoPath, strPhotoPath_s, 130, 130, "Cut");
//this.imgPhoto.ImageUrl = x_strPath;
//this.imgPhoto.ImageUrl = strPath;
//this.Image1.ImageUrl = "ViewPhoto.aspx?emp_id= '" + id + "' ";
this.Image1.ImageUrl = x_strPath;
}
/**/
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

int towidth = width;
int toheight = height;

int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;

switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}

//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);

//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);

try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
harry330 2011-01-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 madaming 的回复:]
说的有点像c/c++,而不是C#,也没有那么复杂。
[/Quote]
晕。算了,当我没说。
海涵德 2011-01-20
  • 打赏
  • 举报
回复
说的有点像c/c++,而不是C#,也没有那么复杂。
harry330 2011-01-20
  • 打赏
  • 举报
回复
可以这么做,
假设你的数组为M*N,
new一个bitmap,大小为M*N,然后通过Bitmap.LockBits获得BitmapData,然后通过BitmapData.Scan0获得内存首地址,然后将你的byte数组中的值一个个赋值。最后Bitmap.UnlockBits将数据写回图像即可。
海涵德 2011-01-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zanfeng 的回复:]
如果这个byts是你保存的。直接用内存。
image 有个from hbitmap
[/Quote]能否给出具体代码?

海涵德 2011-01-20
  • 打赏
  • 举报
回复
图片不大,因为是在picturebox上显示的。
龍过鸡年 2011-01-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 madaming 的回复:]

谢谢,我使用的也是这个办法,我觉得效率低,是否有更直接的办法。
[/Quote]

图片多大?
足球中国 2011-01-20
  • 打赏
  • 举报
回复
如果这个byts是你保存的。直接用内存。
image 有个from hbitmap
海涵德 2011-01-20
  • 打赏
  • 举报
回复
就是为了显示用,
picturebox1.image=image;
picturebox1.refresh();
机器人 2011-01-20
  • 打赏
  • 举报
回复
那看你要干什么了。如果要显示到程序里,只有用这个方法。

要保存图片的话,直接File.WriteAllBytes()就可以。
海涵德 2011-01-20
  • 打赏
  • 举报
回复
谢谢,我使用的也是这个办法,我觉得效率低,是否有更直接的办法。
wuyq11 2011-01-20
  • 打赏
  • 举报
回复

public static System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
{
if (byteArrayIn == null)
return null;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArrayIn))
{
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
return returnImage;
}
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧