叠加两个相同大小的图像时,一个被偏移
When overlaying two identically-sized images, one is offset
我正在尝试通过将一个图像叠加在另一个图像之上来创建图像。代码有效,但我覆盖的图像似乎有点拉伸,我不知道为什么。
所以代码只是创建了一个空白的红色 24x24 矩形,然后我覆盖了一个 24x24 的 png 文件,如下所示:
我期待的是:
但我实际上明白了:
Using backGround As New Bitmap(24, 24, Imaging.PixelFormat.Format32bppArgb)
Using g = Graphics.FromImage(backGround)
Using brush1 As New SolidBrush(Color.Red)
g.FillRectangle(brush1, 0, 0, 24, 24)
Using topimage = Image.FromFile("C:\Scratch\ManNoRecords24.png")
g.DrawImage(topimage, New Point(0, 0))
End Using
End Using
End Using
backGround.Save("C:\Scratch\Emp.png", Imaging.ImageFormat.Png)
End Using
显示 topImage 属性的调试器:
您可以使用
g.DrawImageUnscaledAndClipped(topimage, New Rectangle(0, 0, 24, 24))
而是为了避免在绘制源图像时对源图像进行任何缩放。这行得通,但实际上我不太确定您的解决方案有什么问题。
从 Reference Source 开始,DrawImageUnscaledAndClipped
似乎使用 Pixel
作为图像大小的默认单位,因此忽略了源图像的 DPI 设置:
/// <include file='doc\Graphics.uex' path='docs/doc[@for="Graphics.DrawImageUnscaledAndClipped"]/*' />
/// <devdoc>
/// </devdoc>
public void DrawImageUnscaledAndClipped(Image image, Rectangle rect) {
if(image == null) {
throw new ArgumentNullException("image");
}
int width = Math.Min(rect.Width, image.Width);
int height = Math.Min(rect.Height, image.Height);
//We could put centering logic here too for the case when the image is smaller than the rect
DrawImage(image, rect, 0, 0, width, height, GraphicsUnit.Pixel);
}
DrawImage
和 DrawImageUnscaled
不会,然后可能会根据其内部 DPI 设置重新缩放图像,Matt 发现该设置小于默认的 96,这会导致图像拉伸:
/// <include file='doc\Graphics.uex' path='docs/doc[@for="Graphics.DrawImageUnscaled"]/*' />
/// <devdoc>
/// </devdoc>
public void DrawImageUnscaled(Image image, Point point) {
DrawImage(image, point.X, point.Y);
}
我正在尝试通过将一个图像叠加在另一个图像之上来创建图像。代码有效,但我覆盖的图像似乎有点拉伸,我不知道为什么。
所以代码只是创建了一个空白的红色 24x24 矩形,然后我覆盖了一个 24x24 的 png 文件,如下所示:
我期待的是:
但我实际上明白了:
Using backGround As New Bitmap(24, 24, Imaging.PixelFormat.Format32bppArgb)
Using g = Graphics.FromImage(backGround)
Using brush1 As New SolidBrush(Color.Red)
g.FillRectangle(brush1, 0, 0, 24, 24)
Using topimage = Image.FromFile("C:\Scratch\ManNoRecords24.png")
g.DrawImage(topimage, New Point(0, 0))
End Using
End Using
End Using
backGround.Save("C:\Scratch\Emp.png", Imaging.ImageFormat.Png)
End Using
显示 topImage 属性的调试器:
您可以使用
g.DrawImageUnscaledAndClipped(topimage, New Rectangle(0, 0, 24, 24))
而是为了避免在绘制源图像时对源图像进行任何缩放。这行得通,但实际上我不太确定您的解决方案有什么问题。
从 Reference Source 开始,DrawImageUnscaledAndClipped
似乎使用 Pixel
作为图像大小的默认单位,因此忽略了源图像的 DPI 设置:
/// <include file='doc\Graphics.uex' path='docs/doc[@for="Graphics.DrawImageUnscaledAndClipped"]/*' />
/// <devdoc>
/// </devdoc>
public void DrawImageUnscaledAndClipped(Image image, Rectangle rect) {
if(image == null) {
throw new ArgumentNullException("image");
}
int width = Math.Min(rect.Width, image.Width);
int height = Math.Min(rect.Height, image.Height);
//We could put centering logic here too for the case when the image is smaller than the rect
DrawImage(image, rect, 0, 0, width, height, GraphicsUnit.Pixel);
}
DrawImage
和 DrawImageUnscaled
不会,然后可能会根据其内部 DPI 设置重新缩放图像,Matt 发现该设置小于默认的 96,这会导致图像拉伸:
/// <include file='doc\Graphics.uex' path='docs/doc[@for="Graphics.DrawImageUnscaled"]/*' />
/// <devdoc>
/// </devdoc>
public void DrawImageUnscaled(Image image, Point point) {
DrawImage(image, point.X, point.Y);
}