Base64String与图像互转

1.参数类

/*************************************************************************************
  * 类 名 称:       PictureParam
  * 命名空间:       Base64StringAndImageConvert
  * 文 件 名:       PictureParam
  * 创建时间:       2019-6-13 下午 4:16:45
  * 作    者:       
  * 说    明:
  * 修改时间:		
  * 修 改 人:
 *************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Base64StringAndImageConvert
{
    public class PictureParam
    {
        public PictureParam(string imageFoldPath,string imageName,string sufName,string base64String="")
        {
            ImageFoldPath = imageFoldPath;
            ImageName = imageName;
            suffixName = sufName;
            Base64String = base64String;
        }
        // base64 string
        public string Base64String;
        // image suffix name
        public string suffixName;
        // folder
        public string ImageFoldPath;
        // image name
        public string ImageName;
        public string FullPath
        {
            get
            {
                return ImageFoldPath+ "\\"+ImageName+ "."+ suffixName; 
            }
        }
    }
}

2.转换操作类

/*************************************************************************************
  * 类 名 称:       Base64StringImageConvert
  * 命名空间:       Base64StringAndImageConvert
  * 文 件 名:       Base64StringImageConvert
  * 创建时间:       2019-6-13 下午 4:17:45
  * 作    者:       
  * 说    明:       Base64 string and image convert  
  * 修改时间:		
  * 修 改 人:
 *************************************************************************************/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Base64StringAndImageConvert
{
    public class Base64StringImageConvert
    {
        public PictureParam PicParam { get; set; }
        public Base64StringImageConvert(PictureParam picParam)
        {
            PicParam = picParam;
        }
        /// 
        /// convert picture to an base64string
        /// 
        /// PictureParam input param
        /// base64string
        public string ImageToBase64String(PictureParam para)
        {
            string strRet = string.Empty;
            do
            {
                string strPath = para.FullPath;  
                if (!File.Exists(strPath))
                {
                    break;
                }
                else
                {
                    Bitmap bp = new Bitmap(strPath);
                    ImageFormat format = para.suffixName == "bmp" ? ImageFormat.Bmp : ImageFormat.Jpeg;
                    using (MemoryStream mm = new MemoryStream())
                    {
                        bp.Save(mm, format);
                        byte[] data = mm.ToArray();
                        strRet = Convert.ToBase64String(data);
                        // out log
                        Console.WriteLine(strRet);
                    }
                }
                                             
            } while (false);
            return strRet;
        }
        /// 
        /// 
        /// 
        /// 
        public void Base64StringToImage(PictureParam picParam)
        {
            if (string.IsNullOrEmpty(picParam.Base64String))
                return;
            byte[] data = Convert.FromBase64String(picParam.Base64String);
            using (MemoryStream mm = new MemoryStream(data))
            {
                Image image = Image.FromStream(mm);
                string strPath = picParam.FullPath;
                ImageFormat format = picParam.suffixName == "bmp" ? ImageFormat.Bmp : ImageFormat.Jpeg;
                image.Save(strPath, format);
            }
        }
    }
}

3.测试

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Base64StringAndImageConvert
{
   

    public class Program
    {
        static void Main(string[] args)
        {
            string foldPath = string.Empty;
            string imageName = "a";
            foldPath = Environment.CurrentDirectory;

            PictureParam bmpParam = new PictureParam(foldPath, imageName, "bmp");
            Base64StringImageConvert convert = new Base64StringImageConvert(bmpParam);
            Console.WriteLine(bmpParam.FullPath);
            bmpParam.Base64String =  convert.ImageToBase64String(bmpParam);

            //convert to image
            bmpParam.ImageName = "b";
            convert.Base64StringToImage(bmpParam);
            Console.ReadKey();
        }
    }
}

4.测试结果

Base64String与图像互转_第1张图片

5.代码地址

https://github.com/evan-zang/CSharpSimpleModel/tree/master/Base64StringAndImageConvert

你可能感兴趣的:(C#)