/// <summary>
/// 把图片转换到文本信息
/// </summary>
/// <param name="imagePath">图片的路径</param>
/// <param name="savePath">文本存储路径</param>
public void GetImageToString(string imagePath,string savePath)
{
Stream s = File.Open(imagePath, FileMode.Open);
int leng = 0;
if (s.Length < Int32.MaxValue) //防止图片太大了
leng = (int)s.Length;
byte[] by = new byte[leng]; //声明字节数组
MemoryStream s = new MemoryStream(by); //内存中声明一个流
s.Read(by, 0, (int)s.Length);//把图片读到字节数组中
s.Close();
string str = Convert.ToBase64String(by);//把字节数组转换成字符串
StreamWriter sw = File.CreateText(savePath);//存入savePath文件
sw.Write(str);
sw.Close();
sw.Dispose();
}
/// <summary>
/// 把字符串还原成图片
/// </summary>
/// <param name="path"></param>
/// <returns>图片</returns>
public Image GetImageFromString(string stringPath)
{
StreamReader sr = new StreamReader(stringPath);
string s = sr.ReadToEnd();
sr.Close();
byte[] buf = Convert.FromBase64String(s);//把字符串读到字节数组中
MemoryStream ms = new MemoryStream(buf);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
img.Save("12.gif", System.Drawing.Imaging.ImageFormat.Gif); //保存成图片
ms.Close();
ms.Dispose();
return img;
}