该死的代码
三聪的博客
 /// <summary>
    /// 制作等比环切缩缩图
    /// </summary>

    public static bool MakeThumbnail(Image image, string savePath, int toWidth, int toHeight)
    {
        if (File.Exists(savePath))
        {
            File.SetAttributes(savePath, FileAttributes.Normal);
            File.Delete(savePath);
        }

        if (image == null) return false;

        int x = 0;
        int y = 0;
        int ow = image.Width;
        int oh = image.Height;

        if ((double)image.Width / image.Height > (double)toWidth / toHeight)
        {
            oh = image.Height;
            ow = image.Height * toWidth / toHeight;
            x = (image.Width - ow) / 2;
        }
        else
        {
            ow = image.Width;
            oh = image.Width * toHeight / toWidth;
            y = (image.Height - oh) / 2;
        }

        Bitmap bmPhoto = new Bitmap(toWidth, toHeight, PixelFormat.Format24bppRgb);
        Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
        gbmPhoto.InterpolationMode = InterpolationMode.High;
        gbmPhoto.SmoothingMode = SmoothingMode.HighQuality;
        gbmPhoto.Clear(Color.White);
        gbmPhoto.DrawImage(image, new Rectangle(0, 0, toWidth, toHeight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);

        ImageCodecInfo myImageCodecInfo;
        Encoder myEncoder;
        EncoderParameter myEncoderParameter;
        EncoderParameters myEncoderParameters;

        myImageCodecInfo = GetEncoderInfo("image/jpeg");
        myEncoder = Encoder.Quality;
        myEncoderParameters = new EncoderParameters(1);
        myEncoderParameter = new EncoderParameter(myEncoder, 99L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        bmPhoto.Save(savePath, myImageCodecInfo, myEncoderParameters);

        gbmPhoto.Dispose();
        bmPhoto.Dispose();
        return true;
    }

  

posted on 2013-03-01 13:21  三聪  阅读(325)  评论(0编辑  收藏  举报

作者:gateluck
出处:http://gateluck.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。