C#如何实现将图像保存到内存,再读取? C#如何实现将图像保存到内存,再读取?image.saveas();怎么写啊?还有读取的时候怎么弄? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Image的Save方法有重载,可以保存到流,加载可以 Image img = Image.FromStream(stream); 释放流:ns.Close()//不然其他用户无法处理当前图片 要是用 MemoryStream 整个过程应该是怎样的?刚接触,能否实例分析一下啊?谢谢了 理解MemoryStream ,byte[],Image 截取当前窗体工作区图像,保存至内存//截取当前窗体图像Bitmap bitFormImg = new Bitmap(this.Width, this.Height);this.DrawToBitmap(bitFormImg, new Rectangle(0, 0, this.Width, this.Height));int border = (this.Width - this.ClientSize.Width) / 2;//边框宽度int caption = (this.Height - this.ClientSize.Height) - border;//标题栏高度//截取签字区域图像(去边框、标题栏)Bitmap bitSignatureImg = bitFormImg.Clone(new Rectangle(border, caption, this.ClientSize.Width, this.ClientSize.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);//创建内存流MemoryStream Ms = new MemoryStream();//将图像保存至内存流bitSignatureImg.Save(Ms, ImageFormat.Bmp);bitFormImg.Dispose();bitSignatureImg.Dispose();byte[] img = new byte[Ms.Length];Ms.Position = 0;Ms.Read(img, 0, Convert.ToInt32(Ms.Length));Ms.Close();string result = Convert.ToBase64String(img);读取内存中图像/// <summary>/// 将By数组转为Image数据/// </summary>/// <param name="bytes"></param>/// <returns></returns>public Image BytesToImage(byte[] bytes){ MemoryStream ms = new MemoryStream(bytes); Image img = Image.FromStream(ms); //图片加文字水印 //ImageWaterMark(img); return img;}显示在pictureBox中string image = 图片二进制数据字符串。;//第一个方法这里已经把图片二进制数据存储为字符串了。//string result = Convert.ToBase64String(img);byte[] by = Convert.FromBase64String(image);//绑定图片this.pictureBox1.Image = BytesToImage(by); winfrom多线程 .NET如何剪裁图片? FORM1 如何调用 FROM2中的DATETIMEPICKER1? 升级DLL后报错 XP中iis5怎样升级到IIS6.0 在webform中如何实现listview的效果? 有什么办法可以保护自己做的项目不被修改? JS中数字的&操作对应C#的该怎么写? 小白问题:WPF TreeView 中子节点名称太长,显示不完如何处理?在线等 奇怪问题 论坛帖子回复跟在下方 菜单选项及统计执行过几次
要是用 MemoryStream 整个过程应该是怎样的?刚接触,能否实例分析一下啊?谢谢了
Bitmap bitFormImg = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(bitFormImg, new Rectangle(0, 0, this.Width, this.Height));
int border = (this.Width - this.ClientSize.Width) / 2;//边框宽度
int caption = (this.Height - this.ClientSize.Height) - border;//标题栏高度//截取签字区域图像(去边框、标题栏)
Bitmap bitSignatureImg = bitFormImg.Clone(new Rectangle(border, caption, this.ClientSize.Width, this.ClientSize.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
//创建内存流
MemoryStream Ms = new MemoryStream();
//将图像保存至内存流
bitSignatureImg.Save(Ms, ImageFormat.Bmp);
bitFormImg.Dispose();
bitSignatureImg.Dispose();byte[] img = new byte[Ms.Length];
Ms.Position = 0;
Ms.Read(img, 0, Convert.ToInt32(Ms.Length));
Ms.Close();
string result = Convert.ToBase64String(img);
读取内存中图像/// <summary>
/// 将By数组转为Image数据
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public Image BytesToImage(byte[] bytes)
{
MemoryStream ms = new MemoryStream(bytes);
Image img = Image.FromStream(ms);
//图片加文字水印
//ImageWaterMark(img);
return img;
}
显示在pictureBox中
string image = 图片二进制数据字符串。;//第一个方法这里已经把图片二进制数据存储为字符串了。
//string result = Convert.ToBase64String(img);byte[] by = Convert.FromBase64String(image);
//绑定图片
this.pictureBox1.Image = BytesToImage(by);