将位图和Png图像转换为文本的简单方法,反之亦然 [英] Easy way to convert a Bitmap and Png Image to text and vice versa

查看:110
本文介绍了将位图和Png图像转换为文本的简单方法,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

翻译位图和最简单的方法是什么? Png串并再次返回.我一直在尝试通过内存流等来做一些保存,但是我似乎无法使其正常工作!

what is the easiest way to translate a Bitmap & Png to string AND BACK AGAIN. Ive been trying to do some saves through memory streams and such but i cant seem to get it to work!

显然我并不清楚, 我想要的是能够将带有图像的Bitmap类转换为系统字符串.从那开始,我希望能够将我的字符串丢掉一会儿,然后将其转换回位图以显示在PictureBox中.

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. 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.

推荐答案

基于@peters答案,我最终使用了此方法:

Based on @peters answer I've ended up using this:

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

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

这篇关于将位图和Png图像转换为文本的简单方法,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆