图片与base64字符串之间的转换

一,图片转base64字符串
        //图片转base64字符串方法
        public static string ImageEncodeToBase64(Image image)
        {
            System.IO.MemoryStream m = new System.IO.MemoryStream();//实例化内存流.因为MemoryStream前面加了System.IO.所以不用在最上方添加引用using System.IO
            image.Save(m, System.Drawing.Imaging.ImageFormat.Gif);//将图片存入内存流
            string base64string = Convert.ToBase64String(m.GetBuffer());//将内存流中的数据转换为base64字符串,Convert方法
            return base64string;//返回字符串
        }
二,base64字符串转图片
        //base64字符串转图片方法
        public static Image Base64DecodeToImage(string base64string)
        {//使用trycatch语句抓取错误
            try
            {
                byte[] bt = Convert.FromBase64String(base64string);//尝试把字符串存入二进制数组,Convert方法
                System.IO.MemoryStream stream = new System.IO.MemoryStream(bt);//将二进制数组存入内存流
                return Image.FromStream(stream);//返回Image类型数据
            }
            catch
            {
                return null;//抓取错误后的处理方法
            }
        }

三,带界面的转换示例程序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WFA练习1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Bitmap bit1;//上提示图片(图片1)
        //private Bitmap bit2;//验证码图片(图片2)
        //图片转base64字符串方法
        public static string ImageEncodeToBase64(Image image)
        {
            System.IO.MemoryStream m = new System.IO.MemoryStream();
            image.Save(m, System.Drawing.Imaging.ImageFormat.Gif);
            string base64string = Convert.ToBase64String(m.GetBuffer());
            return base64string;
        }

        //base64字符串转图片方法
        public static Image Base64DecodeToImage(string base64string)
        {
            try
            {
                byte[] bt = Convert.FromBase64String(base64string);
                System.IO.MemoryStream stream = new System.IO.MemoryStream(bt);
                return Image.FromStream(stream);
            }
            catch
            {
                return null;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string path = this.openFileDialog1.FileName;//获得图片路径
                bit1 = new Bitmap(path);
                this.pictureBox1.Image = bit1;
                textBox1.Text = ImageEncodeToBase64(pictureBox1.Image);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text=="")
            {
                MessageBox.Show("请输入base64字符串");
            }
            else
            {
                pictureBox2.Image = Base64DecodeToImage(textBox1.Text);
            }
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,837评论 18 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,188评论 4 61
  • ① 晴晴问:今天多少号来着? 我答:十四号,白色情人节哦! 晴晴说:嗷,我明天可以看篮球赛了! 我:…… ② 我和...
    橙子欢阅读 271评论 0 1
  • 如果我是那春天的鸿雁 那么你就是那夏日的青鱼 时空的错乱 空间的颠倒 让我们的相遇猝不及防 却又沉醉其中不愿醒来 ...
    壹茶阅读 281评论 0 0
  • 在西伯利亚的冻土中冰封千年之后,这个已经在地球上灭绝了万年的物种~~猛犸象,重回大众视线,这些远古巨兽的象牙如王者...
    福人堂阅读 1,488评论 0 0