您可以使用
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);
}