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;
}
 

좋은 웹페이지 즐겨찾기