使用resize方法在白色背景上覆盖位图

时间:2014-01-09 16:56:42

标签: c# graphics bitmap

我有一个方法,它接受一个名为sourceBMPwidthheight的值,并根据这些规范调整图像大小。

result变量是新BMP的指定尺寸。这个方法的作用是sourceBMP,它在点0, 0上呈现,并使用NearestNeighbor进行插值(因为这些是正在插值的像素,NearestNeighbor达到理想的宽度和高度是最佳选择。

    private Bitmap ResizeBitmap(Bitmap sourceBMP, int width, int height) {
        Bitmap result = new Bitmap(width, height);
        using (Graphics g = Graphics.FromImage(result)) {
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
            g.DrawImage(sourceBMP, 0, 0, width, height);
        }
        return result;
    }

据说 - 如何更改它,以便将此图重叠在白色背景上(所以我可以在图表上添加x和y轴标签)? enter image description here尝试自行更改参数,例如在高于0, 0的位置指定起点(如result.Width/6)只会使图像离开屏幕。

编辑:例如:

    private Bitmap overlayBitmap(Bitmap sourceBMP, int width, int height) {
        Bitmap result = new Bitmap(width + (width/3), height + (height/3));
        using (Graphics g = Graphics.FromImage(result)) {
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
            g.DrawImage(sourceBMP, width / 6, height / 6, width + (width / 3), height + (height / 3));
        }
        return result;
    }

上面的代码将图形推离屏幕而不是在尺寸内部。

1 个答案:

答案 0 :(得分:0)

   private Bitmap overlayBitmap(Bitmap sourceBMP, int width, int height) {
        Bitmap result = new Bitmap(width + (width/3), height + (height/3));
        using (Graphics g = Graphics.FromImage(result)) {
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
            g.DrawImage(sourceBMP, width / 6, height / 6, width, height);
        }
        return result;
    }