c#生成缩略图的实现方法
作者:
c#生成缩略图的实现方法,需要的朋友可以参考一下
复制代码 代码如下:
private void SaveThumbnail(Bitmap originBitmap, int width, int height, string directory, string filename, string extension)
{
var physicalPath = directory + filename + extension;
using (var newImage = new Bitmap(width, height))
{
using (var graphic = GetGraphic(originBitmap, newImage))
{
graphic.DrawImage(originBitmap, 0, 0, width, height);
using (var encoderParameters = new EncoderParameters(1))
{
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
newImage.Save(physicalPath,
ImageCodecInfo.GetImageEncoders()
.Where(x => x.FilenameExtension.Contains(extension.ToUpperInvariant()))
.FirstOrDefault(),
encoderParameters);
}
}
}
}
private Graphics GetGraphic(Image originImage, Bitmap newImage)
{
newImage.SetResolution(originImage.HorizontalResolution, originImage.VerticalResolution);
var graphic = Graphics.FromImage(newImage);
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
return graphic;
}