如何将byte[]转换成Image? 如何将byte[]转换成Image 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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; } } 那看你要干什么了。如果要显示到程序里,只有用这个方法。要保存图片的话,直接File.WriteAllBytes()就可以。 就是为了显示用,picturebox1.image=image;picturebox1.refresh(); 如果这个byts是你保存的。直接用内存。image 有个from hbitmap 图片不大,因为是在picturebox上显示的。 可以这么做,假设你的数组为M*N,new一个bitmap,大小为M*N,然后通过Bitmap.LockBits获得BitmapData,然后通过BitmapData.Scan0获得内存首地址,然后将你的byte数组中的值一个个赋值。最后Bitmap.UnlockBits将数据写回图像即可。 说的有点像c/c++,而不是C#,也没有那么复杂。 //获得二进制文件的方法 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(); } } byte[]是2进制流,流可以转换成图片,图片就是流.....学习了 System.IO.MemoryStream ms = new System.IO.MemoryStream(Byte数组);System.Drawing.Bitmap bmp = new Bitmap(ms);pictureBox1.Image = bmp; http://blog.csdn.net/LOVESONGFOREVER/archive/2010/11/26/6037031.aspx ConfigurationSettings.AppSettings 过期 C# 截取字符串 大侠们,如何判断一个是是正整数,负整数,分数? 输出流写入文本的问题 自定义DataSet C# winform datagridview 新增一统计行(顶者有分) 读select语句时用DataReader一个个的读好呢,还是DataAdapter的fill方法好呢? 100分,如何将HTML的BUTTON控件,用代码进行显示/隐藏操作? 如何继承现有类的问题(在线等!) VB6转C#的程序员 请进! jmail发送附件 一维字符串数组如何去重复字符串,相同的字符串元素只保留一个
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;
}
}
picturebox1.image=image;
picturebox1.refresh();
image 有个from hbitmap
假设你的数组为M*N,
new一个bitmap,大小为M*N,然后通过Bitmap.LockBits获得BitmapData,然后通过BitmapData.Scan0获得内存首地址,然后将你的byte数组中的值一个个赋值。最后Bitmap.UnlockBits将数据写回图像即可。
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();
}
}
System.Drawing.Bitmap bmp = new Bitmap(ms);
pictureBox1.Image = bmp;