热心人帮助解答下C#中BMP显示的问题啊
Bitmap b = (Bitmap)Bitmap.FromFile(@"D:\SY\sy3_20120418\0.bmp");
MemoryStream ms = new MemoryStream();
b.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
byte[] myData = new Byte[ms.Length];
ms.Position = 0;
ms.Read(myData, 0, Convert.ToInt32(ms.Length));
File.WriteAllText(@"D:\SY\sy8\wendang.txt", String.Conact(myData).ToArray());
以上是我的代码,我想把存在指定位置的0.bmp图像数据信息写入D:\SY\sy8\wendang.txt,并且在picturebox中直接通过读取该数据信息来显示出来0.bmp,该怎么做啊,麻烦各位大侠解答。
------解决方案--------------------可以使用Base64把字节转成字符串来保存到文本文件中,然后再用base64把文本转回字节数组转回Bitmap。
------解决方案--------------------LS方法可以的,将图片流转换为字符流保存,取的时候再转回来
------解决方案--------------------
//存入文件
using (Image bit = Bitmap.FromFile(@"D:\Document\my\image\1111072009b43b573620a31b06.gif"))
{
using (MemoryStream ms = new MemoryStream())
{
bit.Save(ms, ImageFormat.Gif);
string base64String=Convert.ToBase64String(ms.GetBuffer());
using (StreamWriter sw = new StreamWriter(@"D:\tmp\text.txt"))
{
sw.Write(base64String);
}
}
}
//从文件读取
using (StreamReader sr = new StreamReader(@"D:\tmp\text.txt"))
{
string base64String = sr.ReadToEnd();
byte[] bts = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(bts);
Image bit = Bitmap.FromStream(ms);
this.BackgroundImage = bit;
}