C# 使用调整大小方法将位图覆盖在白色背景上

C# 使用调整大小方法将位图覆盖在白色背景上,c#,graphics,bitmap,C#,Graphics,Bitmap,我有一个方法,它接受一个名为sourceBMP、width和height的值,并根据这些规范调整图像的大小 result变量是新BMP的指定尺寸。此方法所做的是将源BMP,在点0,0上渲染,并使用最近邻点(因为这些是正在插值的像素,最近邻点是最佳选择)插值到所需的宽度和高度 private Bitmap ResizeBitmap(Bitmap sourceBMP, int width, int height) { Bitmap result = new Bitmap(wi

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

result
变量是新BMP的指定尺寸。此方法所做的是将
源BMP
,在点
0,0
上渲染,并使用
最近邻点
(因为这些是正在插值的像素,
最近邻点
是最佳选择)插值到所需的宽度和高度

    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轴标签)?我自己尝试更改参数,比如在高于
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;
    }

上述代码将图形推离屏幕,而不是放在尺寸内。

这是一个基本错误,您正在调整位图的大小。使用g.DrawImage(sourceBMP,width/6,height/6,width,height);令人惊叹的。。非常感谢你。介意你把它作为一个答案,这样我就可以检查它作为答案吗?我想不出一个方法,这将永远有助于任何其他人。这只是一个简单的错误,只是一个微不足道的解决办法。请删除你的问题。我不会删除它。我已经在谷歌上搜索了好几次这个问题,并在提问之前查看了MSDN论坛。如果它对我有帮助,我想不出为什么它对某人没有帮助。只要发布你自己的答案并接受它。
   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;
    }