生成缩略图 c#生成缩略图的实现方法
人气:0想了解c#生成缩略图的实现方法的相关内容吗,在本文为您仔细讲解生成缩略图的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:生成缩略图,下面大家一起来学习吧。
复制代码 代码如下:
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;
}
加载全部内容