• C# Img与Base64转化帮助类


    Base64ImgHelper:

    /**
    *┌──────────────────────────────────────────────────────────────┐
    *│ 描    述:Img与Base64转化的工具类                                                   
    *│ 作    者:执笔小白                                              
    *│ 版    本:2.0                                       
    *│ 创建时间:2020-6-13 15:40:56                            
    *└──────────────────────────────────────────────────────────────┘
    *┌──────────────────────────────────────────────────────────────┐
    *│ 命名空间: ZhibiXiaobai.Uril.ImgHelper                               
    *│ 类    名:Base64ImgHelper                                     
    *└──────────────────────────────────────────────────────────────┘
    */
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    
    namespace ZhibiXiaobai.Uril.ImgHelper
    {
        /// <summary>
        /// Img与Base64转化的帮助类
        /// </summary>
        public class Base64ImgHelper
        {
            /// <summary>
            /// Img转Base64的方法
            /// </summary>
            /// <param name="bitmap">bmp类型的图片数据</param>
            /// <param name="imgFromat">Img原来的格式</param>
            /// <returns></returns>
            public static string ImgToBase64Str(Bitmap bitmap, ImageFormat imgFromat)
            {
                try
                {
                    MemoryStream ms = new MemoryStream();
                    bitmap.Save(ms, imgFromat);
                    byte[] arr = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(arr, 0, (int)ms.Length);
                    ms.Close();
    
                    return Convert.ToBase64String(arr);
                }
                catch
                {
                    return null;
                }
            }
    
            /// <summary>
            /// Base64转Img的方法
            /// img.Save("ImgName.png", ImageFormat.Png);
            /// </summary>
            /// <param name="base64Str">图片源的Base64字节流</param>
            /// <returns></returns>
            public static System.Drawing.Image Base64StrToImg(string base64Str)
            {
                try
                {
                    byte[] arr = Convert.FromBase64String(base64Str);
                    MemoryStream ms = new MemoryStream(arr);
                    return System.Drawing.Image.FromStream(ms);
    
                }
                catch
                {
                    return null;
                }
            }
    
            #region 扩展
            /// <summary>
            /// 根据图片名字的后缀返回图片的System.Drawing.Imaging.ImageFormat格式
            /// </summary>
            /// <param name="imgName">带后缀的图片名字</param>
            /// <returns></returns>
            public static System.Drawing.Imaging.ImageFormat GetImageFormat(string imgName)
            {
                string[] files = imgName.Split('.');
                imgName = files[files.Length-1].Trim().ToLower();  // 获取后缀
                switch (imgName)
                {
                    case "png":
                        return System.Drawing.Imaging.ImageFormat.Png;
                    case "jpg":
                        return System.Drawing.Imaging.ImageFormat.Jpeg;
                    case "jpeg":
                        return System.Drawing.Imaging.ImageFormat.Jpeg;
                    case "gif":
                        return System.Drawing.Imaging.ImageFormat.Gif;
                    case "bmp":
                        return System.Drawing.Imaging.ImageFormat.Bmp;
                    case "icon":
                        return System.Drawing.Imaging.ImageFormat.Icon;
                    case "memoryBmp":  // 不是文件类型
                        return System.Drawing.Imaging.ImageFormat.MemoryBmp;
                    case "tiff":
                        return System.Drawing.Imaging.ImageFormat.Tiff;
                    case "exif":
                        return System.Drawing.Imaging.ImageFormat.Exif;
                    case "wmf":
                        return System.Drawing.Imaging.ImageFormat.Wmf;
                    case "emf":
                        return System.Drawing.Imaging.ImageFormat.Emf;
                    default:  // 默认返回Jpeg
                        return System.Drawing.Imaging.ImageFormat.Jpeg;
                }
            }
            #endregion
        }
    }
  • 相关阅读:
    在IDEA上本地更新同步Git中的更改
    protobuf的序列化和反序列化
    关于Pytorch报警告:Warning: indexing with dtype torch.uint8 is now deprecated, please use a dtype torch.bool instead
    990. 等式方程的可满足性
    死锁
    事务隔离
    Lab-1
    软件测试homework3
    TCP/UDP网络连接的固定写法
    软件测试Homework 2
  • 原文地址:https://www.cnblogs.com/qq2806933146xiaobai/p/15897296.html
Copyright © 2020-2023  润新知