欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 图片与Base64的相互转化

程序员文章站 2022-06-11 12:52:51
public ActionResult UploadSignature2(string src_data) { Class1.Base64StrToImage(src_data, "C:\\Users\\45448\\Desktop\\1\\"+ DateTime.Now.ToString("yyy ......
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;
            }

        }