//
namespace SafeCampus.Core.Utils;
public static class ImageUtil
{
///
/// 图片转换成base64
///
///
///
public static string ImgToBase64String(SKImage img)
{
try
{
var p = img.Encode(SKEncodedImageFormat.Png, 100);
var arr = p.ToArray();
return Convert.ToBase64String(arr);
}
catch
{
return string.Empty;
}
}
///
/// 图片转换成base64
///
///
///
public static string ImgToBase64String(this SKBitmap bmp)
{
try
{
var img = SKImage.FromBitmap(bmp);
var p = img.Encode(SKEncodedImageFormat.Png, 100);
var arr = p.ToArray();
return Convert.ToBase64String(arr);
}
catch
{
return string.Empty;
}
}
///
/// base64转bitmap
///
///
///
public static Bitmap GetBitmapFromBase64(this string base64String)
{
var b = Convert.FromBase64String(base64String);
var ms = new MemoryStream(b);
var bitmap = new Bitmap(ms);
ms.Close();
return bitmap;
}
///
/// base64转bitmap
///
///
///
public static SKBitmap GetSkBitmapFromBase64(this string base64String)
{
var b = Convert.FromBase64String(base64String);
var bitmap = SKBitmap.Decode(b);
return bitmap;
}
///
/// base64转image格式
///
///
///
public static string ToImageBase64(this string base64String)
{
return "data:image/png;base64," + base64String;
}
///
/// 重新修改尺寸
///
/// 图片
/// 尺寸
///
public static Bitmap ResizeImage(Image imgToResize, Size size)
{
//获取图片宽度
var sourceWidth = imgToResize.Width;
//获取图片高度
var sourceHeight = imgToResize.Height;
float nPercent;
//计算宽度的缩放比例
var nPercentW = size.Width / (float)sourceWidth;
//计算高度的缩放比例
var nPercentH = size.Height / (float)sourceHeight;
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
//期望的宽度
var destWidth = (int)(sourceWidth * nPercent);
//期望的高度
var destHeight = (int)(sourceHeight * nPercent);
var b = new Bitmap(destWidth, destHeight);
var g = Graphics.FromImage(b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//绘制图像
g.DrawImage(imgToResize, 0, 0, destWidth,
destHeight);
g.Dispose();
return b;
}
///
/// Resize图片
///
/// 原始Bitmap
/// 新的宽度
/// 新的高度
/// 处理以后的图片
public static Bitmap ResizeImage(this Bitmap bmp, int newW, int newH)
{
try
{
var b = new Bitmap(newW, newH);
var g = Graphics.FromImage(b);
// 插值算法的质量
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch
{
return null;
}
}
///
/// Resize图片
///
/// 原始Bitmap
/// 新的宽度
/// 新的高度
/// 处理以后的图片
public static SKBitmap ResizeImage(this SKBitmap bmp, int newW, int newH)
{
try
{
var b = new SKBitmap(newW, newH);
var resized = b.Resize(new SKImageInfo(newW, newH), SKFilterQuality.High);
if (resized is null)
{
return null;
}
var image = SKImage.FromBitmap(resized);
return b;
}
catch
{
return null;
}
}
///
/// 获取缩略图
///
///
/// 宽
/// 高
///
public static Image GetPicThumbnail(this Bitmap bmp, int w, int h)
{
try
{
var thumbnail = bmp.GetThumbnailImage(w, h, () => false, IntPtr.Zero);
return thumbnail;
}
catch (Exception ex)
{
return null;
}
}
///
/// 获取缩略图
///
///
/// 宽
/// 高
///
public static SKImage GetPicThumbnail(this SKBitmap bmp, int w, int h)
{
try
{
var resized = bmp.Resize(new SKImageInfo(w, h), SKFilterQuality.Medium);
if (resized is null)
{
return null;
}
var image = SKImage.FromBitmap(resized);
return image;
}
catch (Exception)
{
return null;
}
}
}