c#图像调整大小添加额外像素

时间:2016-11-08 01:12:36

标签: c# image resize

以下代码非常奇怪。它在图像的底部添加了一些额外的间距,我看不出原因。代码结果:

enter image description here

这是我正在使用的代码:

    public static Image ReDraw(this Image main, int w, int h,
        CompositingQuality quality = CompositingQuality.Default, //linear?
        SmoothingMode smoothing_mode = SmoothingMode.None,
        InterpolationMode ip_mode = InterpolationMode.NearestNeighbor)
    {
        //size
        double dbl = (double)main.Width / (double)main.Height;

        //preserve size ratio
        if ((int)((double)h * dbl) <= w)
            w = (int)((double)h * dbl);
        else
            h = (int)((double)w / dbl);

        //draw
        Image newImage = new System.Drawing.Bitmap(w, h);
        Graphics thumbGraph = Graphics.FromImage(newImage);
        thumbGraph.CompositingQuality = quality;
        thumbGraph.SmoothingMode = smoothing_mode;
        thumbGraph.InterpolationMode = ip_mode;
        thumbGraph.Clear(Color.Transparent);
        thumbGraph.DrawImage(main, 0, 0, w, h);

        thumbGraph.DrawImage(main,
        new System.Drawing.Rectangle(0, 0, w, h),
        new System.Drawing.Rectangle(0, 0, main.Width, main.Height),
        System.Drawing.GraphicsUnit.Pixel);

        return newImage;
    }

1 个答案:

答案 0 :(得分:-1)

thumbGraph.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;