(精华)2020年6月26日 C#类库 图片操作帮助类
【摘要】
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text.R...
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text.RegularExpressions;
namespace Core.Util
{
/// <summary>
/// 图片操作帮助类
/// </summary>
public class ImgHelper
{
/// <summary>
/// 从文件获取图片
/// </summary>
/// <param name="fileName">文件名</param>
/// <returns></returns>
public static Image GetImgFromFile(string fileName)
{
return Image.FromFile(fileName);
}
/// <summary>
/// 从base64字符串读入图片
/// </summary>
/// <param name="base64">base64字符串</param>
/// <returns></returns>
public static Image GetImgFromBase64(string base64)
{
byte[] bytes = Convert.FromBase64String(base64);
MemoryStream memStream = new MemoryStream(bytes);
Image img = Image.FromStream(memStream);
return img;
}
/// <summary>
/// 从URL格式的Base64图片获取真正的图片
/// 即去掉data:image/jpg;base64,这样的格式
/// </summary>
/// <param name="base64Url">图片Base64的URL形式</param>
/// <returns></returns>
public static Image GetImgFromBase64Url(string base64Url)
{
string base64 = GetBase64String(base64Url);
return GetImgFromBase64(base64);
}
/// <summary>
/// 压缩图片
/// 注:等比压缩
/// </summary>
/// <param name="img">原图片</param>
/// <param name="width">压缩后宽度</param>
/// <returns></returns>
public static Image CompressImg(Image img, int width)
{
return CompressImg(img, width, (int)(((double)width) / img.Width * img.Height));
}
/// <summary>
/// 压缩图片
/// </summary>
/// <param name="img">原图片</param>
/// <param name="width">压缩后宽度</param>
/// <param name="height">压缩后高度</param>
/// <returns></returns>
public static Image CompressImg(Image img, int width, int height)
{
Bitmap bitmap = new Bitmap(img, width, height);
return bitmap;
}
/// <summary>
/// 将图片转为base64字符串
/// 默认使用jpg格式
/// </summary>
/// <param name="img">图片对象</param>
/// <returns></returns>
public static string ToBase64String(Image img)
{
return ToBase64String(img, ImageFormat.Jpeg);
}
/// <summary>
/// 将图片转为base64字符串
/// 使用指定格式
/// </summary>
/// <param name="img">图片对象</param>
/// <param name="imageFormat">指定格式</param>
/// <returns></returns>
public static string ToBase64String(Image img, ImageFormat imageFormat)
{
MemoryStream memStream = new MemoryStream();
img.Save(memStream, imageFormat);
byte[] bytes = memStream.ToArray();
string base64 = Convert.ToBase64String(bytes);
return base64;
}
/// <summary>
/// 将图片转为base64字符串
/// 默认使用jpg格式,并添加data:image/jpg;base64,前缀
/// </summary>
/// <param name="img">图片对象</param>
/// <returns></returns>
public static string ToBase64StringUrl(Image img)
{
return "data:image/jpg;base64," + ToBase64String(img, ImageFormat.Jpeg);
}
/// <summary>
/// 将图片转为base64字符串
/// 使用指定格式,并添加data:image/jpg;base64,前缀
/// </summary>
/// <param name="img">图片对象</param>
/// <param name="imageFormat">指定格式</param>
/// <returns></returns>
public static string ToBase64StringUrl(Image img, ImageFormat imageFormat)
{
string base64 = ToBase64String(img, imageFormat);
return $"data:image/{imageFormat.ToString().ToLower()};base64,{base64}";
}
/// <summary>
/// 获取真正的图片base64数据
/// 即去掉data:image/jpg;base64,这样的格式
/// </summary>
/// <param name="base64UrlStr">带前缀的base64图片字符串</param>
/// <returns></returns>
public static string GetBase64String(string base64UrlStr)
{
string parttern = "^(data:image/.*?;base64,).*?$";
var match = Regex.Match(base64UrlStr, parttern);
if (match.Groups.Count > 1)
base64UrlStr = base64UrlStr.Replace(match.Groups[1].ToString(), "");
return base64UrlStr;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/106969477
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)