/// <summary>
/// 图片处理工具类
/// </summary>
public class ImageUtils
{
/// <summary>
/// 产生缩略图
/// </summary>
public static System.IO.Stream GenerateThumbnail(System.IO.Stream imageStream, int width, int height)
{
System.IO.Stream ms;
using (var originalImage = System.Drawing.Image.FromStream(imageStream))
{
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight))
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);
ms = new System.IO.MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
return ms;
}
/// <summary>
/// 产生缩略图
/// </summary>
public static void GenerateThumbnail(System.IO.Stream imageStream, int width, int height, string outputFilePath)
{
using (var stream = GenerateThumbnail(imageStream, width, height))
using (var fileStream = System.IO.File.Open(outputFilePath, System.IO.FileMode.OpenOrCreate))
{
((System.IO.MemoryStream)stream).WriteTo(fileStream);
}
}
/// <summary>
/// 产生缩略图
/// </summary>
public static System.IO.Stream GenerateThumbnail(byte[] imageBytes, int width, int height)
{
System.IO.Stream ms;
using (var stream = new System.IO.MemoryStream(imageBytes))
{
ms = GenerateThumbnail(stream, width, height);
}
return ms;
}
/// <summary>
/// 产生缩略图
/// </summary>
public static void GenerateThumbnail(byte[] imageBytes, int width, int height, string outputFilePath)
{
using (var stream = GenerateThumbnail(imageBytes, width, height))
using (var fileStream = System.IO.File.Open(outputFilePath, System.IO.FileMode.OpenOrCreate))
{
((System.IO.MemoryStream)stream).WriteTo(fileStream);
}
}
/// <summary>
/// 产生缩略图
/// </summary>
public static System.IO.Stream GenerateThumbnail(string imageFilePath, int width, int height)
{
System.IO.Stream ms;
using (var stream = new System.IO.FileStream(imageFilePath, System.IO.FileMode.Open))
{
ms = GenerateThumbnail(stream, width, height);
}
return ms;
}
/// <summary>
/// 产生缩略图
/// </summary>
public static void GenerateThumbnail(string imageFilePath, int width, int height, string outputFilePath)
{
using (var stream = GenerateThumbnail(imageFilePath, width, height))
using (var fileStream = System.IO.File.Open(outputFilePath, System.IO.FileMode.OpenOrCreate))
{
((System.IO.MemoryStream)stream).WriteTo(fileStream);
}
}