zoukankan      html  css  js  c++  java
  • 生成高质量缩略图

    using System.Drawing;
    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(00, bitmap.Width, bitmap.Height);

                g.DrawImage(image, rect, 
    new System.Drawing.Rectangle(00, 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();
            }
        }
  • 相关阅读:
    马踏棋盘的贪心算法
    Windows Phone开发(13):如何规范用户的输入行为
    Windows Phone开发(16):样式和控件模板
    Windows Phone开发(18):变形金刚第九季——变换
    Windows Phone开发(10):常用控件(上)
    Windows Phone开发(14):数据模板
    Windows Phone开发(12):认识一下独具个性的磁贴
    Windows Phone开发(15):资源
    Windows Phone开发(11):常用控件(下)
    Windows Phone开发(17):URI映射
  • 原文地址:https://www.cnblogs.com/lovecherry/p/433629.html
Copyright © 2011-2022 走看看