• C# 图片与Base64的相互转化


    public ActionResult UploadSignature2(string src_data)
            {
                Class1.Base64StrToImage(src_data, "C:\Users\45448\Desktop\1\"+ DateTime.Now.ToString("yyyyMMddHHss") + ".png");
                return Json(1, JsonRequestBehavior.AllowGet);
            }
    
    
            /// <summary>
            /// 将Base64字符串转换为图片并保存到本地
            /// </summary>
            /// <param name="base64Str">base64字符串</param>
            /// <param name="savePath">图片保存地址,如:/Content/Images/10000.png</param>
            /// <returns></returns>
            public static bool Base64StrToImage(string base64Str, string savePath)
            {
                var ret = true;
                try
                {
                    base64Str = base64Str.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "")
                        .Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", ""); //将base64头部信息替换
                    var bitmap = Base64StrToImage(base64Str);
                    if (bitmap != null)
                    {
                        //创建文件夹
                        var folderPath = savePath.Substring(0, savePath.LastIndexOf('\'));
                        ////FileHelper.CreateDir(folderPath);
                        if (!Directory.Exists(folderPath))
                        {
                            Directory.CreateDirectory(folderPath);
                        }
                        //图片后缀格式
                        var suffix = savePath.Substring(savePath.LastIndexOf('.') + 1,
                            savePath.Length - savePath.LastIndexOf('.') - 1).ToLower();
                        var suffixName = suffix == "png"
                            ? ImageFormat.Png
                            : suffix == "jpg" || suffix == "jpeg"
                                ? ImageFormat.Jpeg
                                : suffix == "bmp"
                                    ? ImageFormat.Bmp
                                    : suffix == "gif"
                                        ? ImageFormat.Gif
                                        : ImageFormat.Jpeg;
    
                        //这里复制一份对图像进行保存,否则会出现“GDI+ 中发生一般性错误”的错误提示
                        var bmpNew = new Bitmap(bitmap);
                        bmpNew.Save(savePath, suffixName);
                        bmpNew.Dispose();
                        bitmap.Dispose();
                    }
                    else
                    {
                        ret = false;
                    }
                }
                catch (Exception ex)
                {
                    ret = false;
                }
                return ret;
       
            }
    复制代码
    复制代码
     /// <summary>
            /// Image 转成 base64
            /// </summary>
            /// <param name="fileFullName"></param>
            public static string ImageToBase64(string fileFullName)
            {
                try
                {
                    Bitmap bmp = new Bitmap(fileFullName);
                    MemoryStream ms = new MemoryStream();
                    var suffix = fileFullName.Substring(fileFullName.LastIndexOf('.') + 1,
                        fileFullName.Length - fileFullName.LastIndexOf('.') - 1).ToLower();
                    var suffixName = suffix == "png"
                        ? ImageFormat.Png
                        : suffix == "jpg" || suffix == "jpeg"
                            ? ImageFormat.Jpeg
                            : suffix == "bmp"
                                ? ImageFormat.Bmp
                                : suffix == "gif"
                                    ? ImageFormat.Gif
                                    : ImageFormat.Jpeg;
    
                    bmp.Save(ms, suffixName);
                    byte[] arr = new byte[ms.Length]; ms.Position = 0;
                    ms.Read(arr, 0, (int)ms.Length); ms.Close();
                    return Convert.ToBase64String(arr);
                }
                catch (Exception ex)
                {
                    return null;
                }
    
            }
  • 相关阅读:
    动手学深度学习
    实用机器学习
    双向注意力流模型
    深度学习进阶自然语言处理
    整体二分的升华
    2022百度之星 复赛 T3 最大值
    树的难题 BJOI2017 点分治 单调队列
    CF 818 div2
    2022 CCPC 热身赛
    AOJ 完全背包 数量少体积大价值小版本 dp+贪心
  • 原文地址:https://www.cnblogs.com/sdxlz666/p/12447506.html
Copyright © 2020-2023  润新知