当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现字符串与图片的Base64编码转换操作示例

C#实现字符串与图片的Base64编码转换操作示例

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例讲述了c#实现字符串与图片的base64编码转换操作。分享给大家供大家参考,具体如下: using system; using system.colle

本文实例讲述了c#实现字符串与图片的base64编码转换操作。分享给大家供大家参考,具体如下:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
using system.io;
using system.drawing.imaging;
namespace base64_img
{
  public partial class form1 : form
  {
    public form1()
    {
      initializecomponent();
    }
    //图片 转为  base64编码的文本
    private void button1_click(object sender, eventargs e)
    {
      openfiledialog dlg = new openfiledialog();
      dlg.title = "选择要转换的图片";
      dlg.filter = "image files (*.jpg;*.bmp;*.gif)|*.jpg*.jpeg;*.gif;*.bmp|allfiles (*.*)|*.*";
      if (dialogresult.ok == dlg.showdialog())
      {
        imgtobase64string(dlg.filename);
      }
    }
    //图片 转为  base64编码的文本
    private void imgtobase64string(string imagefilename)
    {
      try
      {
        bitmap bmp = new bitmap(imagefilename);
        this.picturebox1.image = bmp;
        filestream fs = new filestream(imagefilename + ".txt", filemode.create);
        streamwriter sw = new streamwriter(fs);
        memorystream ms = new memorystream();
        bmp.save(ms, system.drawing.imaging.imageformat.jpeg);
        byte[] arr = new byte[ms.length];
        ms.position = 0;
        ms.read(arr, 0, (int)ms.length);
        ms.close();
        string strbaser64 = convert.tobase64string(arr);
        sw.write(strbaser64);
        sw.close();
        fs.close();
        messagebox.show("转换成功!");
      }
      catch (exception ex)
      {
        messagebox.show("imgtobase64string 转换失败/nexception:" + ex.message);
      }
    }
    //base64编码的文本 转为  图片
    private void button2_click(object sender, eventargs e)
    {
      openfiledialog dlg = new openfiledialog();
      dlg.title = "选择要转换的base64编码的文本";
      dlg.filter = "txt files|*.txt";
      if (dialogresult.ok == dlg.showdialog())
      {
        base64stringtoimage(dlg.filename);
      }
    }
    //base64编码的文本 转为  图片
    private void base64stringtoimage(string txtfilename)
    {
      try
      {
        filestream ifs = new filestream(txtfilename, filemode.open, fileaccess.read);
        streamreader sr = new streamreader(ifs);
        string inputstr = sr.readtoend();
        byte[] arr = convert.frombase64string(inputstr);
        memorystream ms = new memorystream(arr);
        bitmap bmp = new bitmap(ms);
        bmp.save(txtfilename + ".jpg", system.drawing.imaging.imageformat.jpeg);
        //bmp.save(txtfilename + ".bmp", imageformat.bmp);
        //bmp.save(txtfilename + ".gif", imageformat.gif);
        //bmp.save(txtfilename + ".png", imageformat.png);
        ms.close();
        sr.close();
        ifs.close();
        this.picturebox1.image = bmp;
        messagebox.show("转换成功!");
      }
      catch (exception ex)
      {
        messagebox.show("base64stringtoimage 转换失败/nexception:"+ex.message);
      }
    }
  }
}

ps:这里再为大家提供几款比较实用的base64在线编码解码工具供大家使用:

base64编码解码工具:

在线图片转换base64工具:

base64在线编码解码 utf-8版:

base64在线编码解码 gb2312版:

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#编码操作技巧总结》、《c#中xml文件操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结

希望本文所述对大家c#程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网