生成高质量缩略图

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();
        }
    }
posted @ 2006-06-23 11:54  lovecherry  阅读(2811)  评论(3编辑  收藏  举报