图片和文本文件互换

1.将图片保存为文本文件

    var ms = new MemoryStream();
            pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            var buffer = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(buffer, 0, buffer.Length);
            ms.Close();
            File.WriteAllText("demobase64.txt", Convert.ToBase64String(buffer));//保存为文本文件


2. 从文本文件中加载图片

     string contents = File.ReadAllText("demobase64.txt");
            Image image = Image.FromStream(new MemoryStream(Convert.FromBase64String(contents)));
            pictureBox2.Image = image;

posted on 2012-11-05 18:16  HTTP500  阅读(117)  评论(0编辑  收藏  举报