制作等比环切缩缩图

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

  

原文地址:https://www.cnblogs.com/gateluck/p/2938524.html