生成高质量缩略图
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
private void CreateThumbnail(string sFileSrcPath, string sFileDstPath, int iSizeLimit)
{
if (File.Exists(sFileSrcPath))
{
System.Drawing.Image image = System.Drawing.Image.FromFile(sFileSrcPath) as System.Drawing.Bitmap;
System.Drawing.SizeF size = new System.Drawing.SizeF(image.Width, image.Height);
while (size.Width > iSizeLimit || size.Height > iSizeLimit)
{
size.Height /= 1.1F;
size.Width /= 1.1F;
}
System.Drawing.Image bitmap = new System.Drawing.Bitmap(Convert.ToInt16(size.Width), Convert.ToInt16(size.Height));
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
g.DrawImage(image, rect, new System.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.GraphicsUnit.Pixel);
ImageCodecInfo myImageCodecInfo;
Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
myImageCodecInfo = ImageCodecInfo.GetImageEncoders()[0];
myEncoder = Encoder.Quality;
myEncoderParameters = new EncoderParameters(1);
myEncoderParameter = new EncoderParameter(myEncoder, 100L); // 0-100
myEncoderParameters.Param[0] = myEncoderParameter;
bitmap.Save(sFileDstPath, myImageCodecInfo, myEncoderParameters);
myEncoderParameter.Dispose();
myEncoderParameters.Dispose();
image.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
{
if (File.Exists(sFileSrcPath))
{
System.Drawing.Image image = System.Drawing.Image.FromFile(sFileSrcPath) as System.Drawing.Bitmap;
System.Drawing.SizeF size = new System.Drawing.SizeF(image.Width, image.Height);
while (size.Width > iSizeLimit || size.Height > iSizeLimit)
{
size.Height /= 1.1F;
size.Width /= 1.1F;
}
System.Drawing.Image bitmap = new System.Drawing.Bitmap(Convert.ToInt16(size.Width), Convert.ToInt16(size.Height));
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
g.DrawImage(image, rect, new System.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.GraphicsUnit.Pixel);
ImageCodecInfo myImageCodecInfo;
Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
myImageCodecInfo = ImageCodecInfo.GetImageEncoders()[0];
myEncoder = Encoder.Quality;
myEncoderParameters = new EncoderParameters(1);
myEncoderParameter = new EncoderParameter(myEncoder, 100L); // 0-100
myEncoderParameters.Param[0] = myEncoderParameter;
bitmap.Save(sFileDstPath, myImageCodecInfo, myEncoderParameters);
myEncoderParameter.Dispose();
myEncoderParameters.Dispose();
image.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
欢迎大家阅读我的极客时间专栏《Java业务开发常见错误100例》【全面避坑+最佳实践=健壮代码】