|
|
|
@ -0,0 +1,53 @@
|
|
|
|
|
using System;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Drawing2D;
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
public static class ImageHelper
|
|
|
|
|
{
|
|
|
|
|
public static Bitmap ClipImage(this Image image, int renderWidth, int renderHeight, int rectWidth, int rectHeight)
|
|
|
|
|
{
|
|
|
|
|
var width = rectWidth * image.Width / renderWidth;
|
|
|
|
|
var height = rectHeight * width / rectWidth;
|
|
|
|
|
var bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
|
|
|
|
|
bitmap.SetResolution(350, 350);
|
|
|
|
|
using (var g = Graphics.FromImage(bitmap))
|
|
|
|
|
{
|
|
|
|
|
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
|
|
|
g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle((image.Width - bitmap.Width) / 2, (image.Height - bitmap.Height) / 2, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
|
|
|
|
|
}
|
|
|
|
|
return bitmap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Bitmap ResizeImage(this Image image, int width, int height)
|
|
|
|
|
{
|
|
|
|
|
var bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
|
|
|
|
|
bitmap.SetResolution(350, 350);
|
|
|
|
|
using (var g = Graphics.FromImage(bitmap))
|
|
|
|
|
{
|
|
|
|
|
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
|
|
|
g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
|
|
|
|
|
}
|
|
|
|
|
return bitmap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ToBase64(this Image image)
|
|
|
|
|
{
|
|
|
|
|
var info = ImageCodecInfo.GetImageEncoders().First(o => o.FormatID == ImageFormat.Jpeg.Guid);
|
|
|
|
|
using (var encoderParams = new EncoderParameters(1))
|
|
|
|
|
{
|
|
|
|
|
using (var param = new EncoderParameter(Encoder.Quality, 70L))
|
|
|
|
|
{
|
|
|
|
|
encoderParams.Param[0] = param;
|
|
|
|
|
//image.Save("temp.jpg", info, encoderParams);
|
|
|
|
|
using (var ms = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
image.Save(ms, info, encoderParams);
|
|
|
|
|
return Convert.ToBase64String(ms.ToArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|