c# - 当图像是位图时,使用 Graphics.DrawImage(Image, RectangleF destRect, RectangleF srcRect, GraphicsUnit srcUnit) 有好处吗?

标签 c# graphics drawimage

除非我遗漏了什么位图是量化的(面向像素)。那么当有人尝试以下操作时会发生什么:

   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/

相关文章:

c# - Enum.GetValues() 的内部工作

c++ - 插值噪声中的伪影

java - 在 Processing 中获取默认的(屏幕上的)PGraphics 上下文

java - 在 BufferedImage 上绘制 Image 对象以供以后使用

html - 在 Chrome 上将 drawImage 与 Canvas 一起使用非常慢

c# - 如何在 ExceptionFilterAttribute 中返回自定义信息?

c# - 绑定(bind)后台时"Must create DependencySource on same Thread as the DependencyObject."

javascript - Blazor:Google map JS API

c# - GetEncoderParameterList 出现意外的 "Bitmap Region is already Locked"异常。有任何想法吗?

java - JPanel 上无法停止闪烁