c#图片base64去转义字符_c# 图片 与 BASE64 字符串 互相转换

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-1 19:23   11   0

using System;

using System.Collections.Generic;

using System.Drawing;

using System.IO;

using System.Linq;

using System.Web;

using System.Drawing.Imaging;

namespace Html5Image.Tools

{

public class ImageTool

{

//图片 转为 base64编码的文本

public static string ImgToBase64String(Bitmap bmp)

{

//Bitmap bmp = new Bitmap(Imagefilename);

//this.pictureBox1.Image = bmp;

//FileStream fs = new FileStream(Imagefilename + ".txt", FileMode.Create);

//StreamWriter sw = new StreamWriter(fs);

MemoryStream ms = new MemoryStream();

bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

byte[] arr = new byte[ms.Length];

ms.Position = 0;

ms.Read(arr, 0, (int)ms.Length);

ms.Close();

String strbaser64 = Convert.ToBase64String(arr);

return strbaser64;

}

public static Bitmap Base64StringToImage(string base64Img)

{

byte[] bytes = Convert.FromBase64String(base64Img);

MemoryStream ms = new MemoryStream();

ms.Write(bytes, 0, bytes.Length);

Bitmap bmp = new Bitmap(ms);

return bmp;

}

///

/// 保存图片

///

///

///

/// EX: System.Drawing.Imaging.Jpeg

public static void SaveFile(string base64Img, string imgPath, ImageFormat imgFormat )

{

string dir = Path.GetDirectoryName(imgPath);

if (!Directory.Exists(dir))

{

Directory.CreateDirectory(dir);

}

var bitmap = Base64StringToImage(base64Img);

bitmap.Save(imgPath, imgFormat);

}

}

}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP