在中心调整绘制图像的大小

Resizing Drawn Image at Center

我想使用轨迹栏调整在 .._Paint 事件中绘制的图像的大小。

到目前为止,我已经将文件中的图像加载到名为 original 的位图中。现在我有另一个名为 'cropped' 的位图,它存储原始图像的一部分。

Bitmap original = new Bitmap(spriteSheet);
RectangleF srcRect = new RectangleF(ip.x, ip.y, ip.width, ip.height);
Bitmap cropped = original.Clone(srcRect, original.PixelFormat);

我有一个名为 pb_preview 的图片框,它像这样绘制裁剪后的图像:

e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
RectangleF rc = new RectangleF(pb_preview.Height / 2, pb_preview.Width / 2, zoomSlider.Value * cropped.Width, zoomSlider.Value * cropped.Height);
e.Graphics.DrawImage(cropped, rc);

在我的轨迹栏更改值事件中,我使 pb_preview 无效,因此每次更改值时,pb_preview 都会以更大的值绘制图像。

问题是每次我更改轨迹栏的值时,图像不会在中心调整大小,而是会调整大小,但每次调整大小时它都会向左移动一点,直到最大调整大小的图像超出框架.

我设法找到了另一个关于调整大小的问题并遇到了这个函数:

private Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
{
    Bitmap result = new Bitmap(nWidth, nHeight);
    using (Graphics g = Graphics.FromImage(result))
    {
        g.InterpolationMode = InterpolationMode.NearestNeighbor;
        g.DrawImage(b, 0, 0, nWidth, nHeight);
    }
    return result;
}

在 Trackbar 的值更改事件中,我执行:

pb_preview.Image = ResizeBitmap(cropped, zoomSlider.Value * cropped.Width, zoomSlider.Value * cropped.Height);