DrawImage调整大小的图像太小 [英] DrawImage resized image too small

查看:1047
本文介绍了DrawImage调整大小的图像太小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 Graphics.DrawImage 绘制图像并以比原始图像更大的尺寸绘制时,它最终会变得太小。您可以在下面的图片中看到它:

When I draw an image using Graphics.DrawImage and draw it at a bigger size than the original image, it ends up being a bit too small. You can see this in the following picture:

绿线不应可见且不是图像的一部分。而是在图像后面绘制它们,并且图像应该覆盖它们。

The green lines shouldn't be visible and are not part of the image. Rather they get drawn behind the image and the image should cover them.

我如何绘制尺寸正确的图像?

How can I draw an image with the exact right size?

编辑:我用传递给 DrawImage 调用的相同矩形绘制绿色部分,其确切尺寸为图像应该很大。因此,我的值没有缺陷(我认为)。

EDIT: I draw the green part with the same rectangle I pass into the DrawImage call, with the exact dimensions of how big the image should be. So no flaw in my values (I think).

编辑2 :我使用 FillRectangle ,因此无需进行笔计算。另外,我记录了传递到矩形中的图像和绿色填充值,这些值是正确的。只是关闭的图像。

EDIT 2: I draw the green rectangle using FillRectangle, so no pen calculations need to be done. Also, I logged the values that I pass into the rectangle for both the image and the green fill, and the values are correct. It's just the image that's off. I will post code later, as I'm not at my computer at the moment.

EDIT 3 :这是我以前使用的代码,我稍后将发布代码。渲染图像:

EDIT 3: This is the code I use to render the images:

// This is for zooming
public readonly float[] SCALES = { 0.05f, 0.1f, 0.125f, 0.25f, 0.333f, 0.5f, 0.667f, 0.75f, 1.0f, 1.25f, 1.5f, 1.75f, 2.0f, 2.5f, 3.0f, 3.5f, 4.0f, 4.5f, 5.0f, 6.0f, 7.0f, 8.0f, 10.0f, 12.0f, 15.0f, 20.0f, 30.0f, 36.0f };
private int scaleIndex = 8;

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    float ScaleFactor = SCALES[scaleIndex];

    e.Graphics.InterpolationMode = ScaleFactor < 1 ? InterpolationMode.Bicubic : InterpolationMode.NearestNeighbor;

    Image im = Properties.Resources.TSprite0;

    for (int y = 0; y < TilesVertical; y++)
    {
        for (int x = 0; x < TilesHorizontal; x++)
        {
            float sx = im.Width * ScaleFactor;
            float sy = im.Height * ScaleFactor;
            Point p = new Point((int)(-scrollPosition.X + sx * x), (int)(-scrollPosition.Y + sy * y));
            Size s = new Size((int)Math.Floor(sx), (int)Math.Floor(sy));

            // The green rectangle in the background should be the same size as the image
            e.Graphics.FillRectangle(Brushes.Lime, new Rectangle(p, s));
            e.Graphics.DrawImage(im, new Rectangle(p, s), 0, 0, 16, 16, GraphicsUnit.Pixel);
        }
    }

    im.Dispose();
}

编辑4 :也请注意图像似乎在左侧和顶部裁剪,而不是调整大小。看一下在Photoshop中放大后的原始图像的比较,然后看GDI +如何渲染它:

EDIT 4: Also note that the image seems to be cropped on the left and top instead of resized. Take a look at this comparison of the original image upscaled in Photoshop and then how GDI+ renders it:

推荐答案

缩放到2倍或更大时会发生此问题。

The issue happens when scaling to 2x or larger.

看起来整个问题都是由错误的默认 PixelOffsetMode

Looks like the whole problem is caused by the wrong default PixelOffsetMode.


通过在渲染过程中偏移像素,可以提高渲染质量
,而以渲染速度为代价。

By offsetting pixels during rendering, you can improve render quality at the cost of render speed.

将其设置为

g.PixelOffsetMode = PixelOffsetMode.Half;

让它消失了。

将其设置为

g.PixelOffsetMode = PixelOffsetMode.HighQuality;

也可以。

默认高速会导致图像有点渲染

Default, None and HighSpeed cause the image to be rendered a little to the left and up.

通常,您还需要设置 InterpolationMode.NearestNeighbor

这篇关于DrawImage调整大小的图像太小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆