VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • 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;
}



相关教程