首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

image转string解决办法

2012-08-14 
image转string1、将image转成string;C# codeusing (MemoryStream stream new MemoryStream()){PictureBox

image转string
1、将image转成string;

C# code
using (MemoryStream stream = new MemoryStream())         {             PictureBox pb = c as PictureBox;             pb.Image.Save(stream, ImageFormat.Jpeg);             byte[] by = stream.ToArray();             string s = System.Convert.ToBase64String(by);          }

2、将string保存至外部
3、读取外部string转成image,pictruebox也能显示图片
C# code
PictureBox pb = new PictureBox();byte[] b = System.Convert.FromBase64String(xnChild.Attributes["Image"].InnerText);using (MemoryStream ms = new MemoryStream(b)){    Image myimge = Image.FromStream(ms);    pb.Image = myimge;    pb.BorderStyle = BorderStyle.FixedSingle;    pb.SizeMode = PictureBoxSizeMode.StretchImage;}

4、再执行第1步,提示出错“GDI+ 中发生一般性错误。”

请问为什么?

[解决办法]
using (MemoryStream ms = new MemoryStream(b))
请勿滥用using,第三步中的using必须去掉,那个ms不能释放,一旦释放,图片将一起被释放。Image.FromXXX方法的参数必须始终保持而不释放。

热点排行