PictureBox执行不必要的缩放c#

本文关键字:缩放 不必要 执行 PictureBox | 更新日期: 2023-09-27 18:16:36

我目前有一个windows窗体应用程序,在它中间有一个pictureBox,我也在绘制各种图像。这些图像都画得很好,除了它们都被按比例放大了25%。我还应该补充一点,我正在绘制一个Paint方法中的所有内容,使用PaintEventArgs来获取图形设备。

我确保SizeMode设置为Normal,我反复检查了图形对象的比例因子为1,并且我传递给paint方法的所有图像对象都具有应有的大小,但是当它们被绘制时,它们的大小不同。

到目前为止,我一直在调用g.d drawimage (image, Rectangle)并将图像的宽度和高度传递为矩形的宽度和高度,以便它们被迫以正确的大小绘制,但我觉得这应该是一个短期的修复,我忽略了一些简单的东西。

任何帮助都将是伟大的,提前感谢。

代码如下(只包含重要的部分):

public class Level : PictureBox
{
    ...
    private Image image;
    ...
    public Level(TabPage parent, Panel propertiesPanel, ItemManager items, string levelName)
    {
        ...
        image = Image.FromFile(@"Levels/" + levelName);
        Size = image.Size;
        SizeMode = PictureBoxSizeMode.Normal;
        MouseClick += new MouseEventHandler(level_MouseClick);
        MouseMove += new MouseEventHandler(level_MouseMove);
        Paint += new PaintEventHandler(level_Paint);
        Invalidate();
    }
    private void level_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        //With the rectangle fix (drawing to correct size)
        g.DrawImage(image, new Rectangle(0, 0, image.Size.Width, image.Size.Height));
        ////Without the fix (as i thought it should be be this is where it scales it)
        //g.DrawImage(image, new Point(0, 0));
        drawPlacedItems(g);
        drawItemPreview(g);
    }

PictureBox执行不必要的缩放c#

这听起来像你的图像的HorizontalResolutionVerticalResolution属性被应用时,你不希望他们,修改你的代码按照Jeremy的链接到图像大小问题在位图,确保HorizontalResolutionVerticalResolution被重置或忽略之前调用DrawImage