GDI+ Graphics::DrawImage图像缩放问题

使用 graphics.DrawImage(bitmap, 0, 0) 绘制图像时,如果bitmap是 new Bitmap(width, height) 生成的,则绘制是按原分辨率绘制的。如果图片超过 DC 的尺寸,则图片会显示不全。

但如果bitmap是通过 Bitmap::FromFile 从PNG加载的,则 graphics.DrawImage(bitmap, 0, 0) 不是原分辨率,做了缩放。

搜索得到了答案
https://learn.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-improve-performance-by-avoiding-automatic-scaling?view=netframeworkdesktop-4.8

If the resolution used by GDI+ (usually 96 dots per inch) is different from the resolution stored in the Image object, then the DrawImage method will scale the image. For example, suppose an Image object has a width of 216 pixels and a stored horizontal resolution value of 72 dots per inch. Because 216/72 is 3, DrawImage will scale the image so that it has a width of 3 inches at a resolution of 96 dots per inch. That is, DrawImage will display an image that has a width of 96x3 = 288 pixels

获取GDI+的显示分辨率可以用 graphics.GetDpiX()。获取bitmap的分辨率可以用 bitmap.GetHorizontalResolution()。new Bitmap(width, height) 生成的 bitmap 是按默认96dpi生成的

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容