除非我遗漏了什么位图是量化的(面向像素)。那么当有人尝试以下操作时会发生什么:
public void Foo(Bitmap image)
{
var destinationRect = new Rectangle(0, 0, 50, 50);
var resultingSubimage = new Bitmap(destinationRect.Width, destinationRect.Height, PixelFormat.Format24bppRgb);
using (var graphics = Graphics.FromImage(resultingSubimage))
{
graphics.DrawImage(image, destinationRect, new RectangleF(30.3245F /*x*/, 23.234234F /*y*/, 50F, 50F), GraphicsUnit.Pixel);
// vs graphics.DrawImage(image, destinationRect, new Rectangle(30 /*x*/, 23 /*y*/, 50, 50), GraphicsUnit.Pixel);
}
}
请注意,x 和 y 字段是指向亚像素点的小数。但是,是否存在位图的子像素点这样的东西?幕后发生了什么?抱歉,如果此问题已在其他地方得到解答,但 Graphics.DrawImage() 和底层 p/invoke 函数“GdipDrawImageRectRect”的在线文档对此没有任何说明。
最佳答案
是not well known ,如果你绘制图像,例如:
graphics.DrawImage(image, top, left);
图像将被缩放。这是因为 DrawImage
查看图像的 dpi 设置(例如来自 photoshop 的 72dpi),并缩放图像以匹配目标显示(通常为 96dpi)。
如果你想绘制一个没有任何缩放的图像,你必须明确地给 DrawImage
图像的大小:
graphics.DrawImage(img, top, left, img.Width, img.Height);
通过调用 DrawImage
与原始图像像素大小匹配的目标矩形,您可以避免重新采样/重新缩放。
红利阅读
关于c# - 当图像是位图时,使用 Graphics.DrawImage(Image, RectangleF destRect, RectangleF srcRect, GraphicsUnit srcUnit) 有好处吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36601313/