简体   繁体   English

将Bitmap和Png图像转换为文本的简便方法,反之亦然

[英]Easy way to convert a Bitmap and Png Image to text and vice versa

what is the easiest way to translate a Bitmap & Png to string AND BACK AGAIN. 什么是将Bitmap和Png转换为字符串AND BACK AGAIN的最简单方法。 Ive been trying to do some saves through memory streams and such but i cant seem to get it to work! 我一直试图通过内存流等进行一些保存,但我似乎无法让它工作!

Appearently i wasnt clear, what i want, is to be able to translate a Bitmap class, with an image in it.. into a system string. 显然我不清楚,我想要的是能够将Bitmap类(带有图像)转换为系统字符串。 from there i want to be able to throw my string around for a bit, and then translate it back into a Bitmap to be displayed in a PictureBox. 从那里我希望能够将我的字符串抛出一点,然后将其转换回Bitmap以显示在PictureBox中。

Based on @peters answer I've ended up using this: 根据@peters的答案,我最终使用了这个:

string bitmapString = null;
using (MemoryStream memoryStream = new MemoryStream())
{
    image.Save(memoryStream, ImageFormat.Png); 
    byte[] bitmapBytes = memoryStream.GetBuffer();
    bitmapString = Convert.ToBase64String(bitmapBytes, Base64FormattingOptions.InsertLineBreaks);
}

and

Image img = null;
byte[] bitmapBytes = Convert.FromBase64String(pictureSourceString);
using (MemoryStream memoryStream = new MemoryStream(bitmapBytes))
{
    img = Image.FromStream(memoryStream);
}

From bitmap to string: 从位图到字符串:

MemoryStream memoryStream = new MemoryStream();
bitmap.Save (memoryStream, ImageFormat.Png);
byte[] bitmapBytes = memoryStream.GetBuffer();
string bitmapString = Convert.ToBase64String(bitmapBytes, Base64FormattingOptions.InsertLineBreaks);

From string to image: 从字符串到图像:

byte[] bitmapBytes = Convert.FromBase64String(bitmapString);
MemoryStream memoryStream = new MemoryStream(bitmapBytes);
Image image = Image.FromStream(memoryStream);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM