DrawImage 调整后的图像太小
DrawImage resized image too small
当我使用 Graphics.DrawImage
绘制图像并以比原始图像更大的尺寸绘制时,它最终有点太小了。您可以在下图中看到这一点:
绿线不应该是可见的并且不是图像的一部分。相反,它们被绘制在图像后面,图像应该覆盖它们。
如何绘制尺寸准确的图像?
EDIT:我用传入 DrawImage
调用的相同矩形绘制绿色部分,图像的确切尺寸应该有多大。所以我的价值观没有缺陷(我认为)。
编辑 2:我使用 FillRectangle
绘制绿色矩形,因此不需要进行笔计算。此外,我记录了传递到矩形中的图像和绿色填充值,这些值是正确的。这只是关闭的图像。我稍后会 post 编码,因为我现在不在电脑前。
编辑 3:这是我用来渲染图像的代码:
// 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+ 如何渲染它的比较:
缩放到 2 倍或更大时会出现此问题。
看起来整个问题都是由错误的默认设置引起的 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
和 HighSpeed
导致图像向左上方稍微渲染。
通常您还想设置 InterpolationMode.NearestNeighbor
。
当我使用 Graphics.DrawImage
绘制图像并以比原始图像更大的尺寸绘制时,它最终有点太小了。您可以在下图中看到这一点:
绿线不应该是可见的并且不是图像的一部分。相反,它们被绘制在图像后面,图像应该覆盖它们。
如何绘制尺寸准确的图像?
EDIT:我用传入 DrawImage
调用的相同矩形绘制绿色部分,图像的确切尺寸应该有多大。所以我的价值观没有缺陷(我认为)。
编辑 2:我使用 FillRectangle
绘制绿色矩形,因此不需要进行笔计算。此外,我记录了传递到矩形中的图像和绿色填充值,这些值是正确的。这只是关闭的图像。我稍后会 post 编码,因为我现在不在电脑前。
编辑 3:这是我用来渲染图像的代码:
// 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+ 如何渲染它的比较:
缩放到 2 倍或更大时会出现此问题。
看起来整个问题都是由错误的默认设置引起的 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
和 HighSpeed
导致图像向左上方稍微渲染。
通常您还想设置 InterpolationMode.NearestNeighbor
。