【手记】解决Graphics.DrawImage带ImageAttributes在XP报内存不足的问题

异常信息:

System.OutOfMemoryException: 内存不足。
   在 System.Drawing.Graphics.CheckErrorStatus(Int32 status)
   在 System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttrs, DrawImageAbort callback, IntPtr callbackData)
   在 System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr, DrawImageAbort callback)
   在 System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr)
   ...

这个问题在外网也有一些讨论,我倾向的说法是,XP的GDI+组件存在问题,对于像素格式PixelFormat有点特别的图像,把它绘制到其它地方的时候处理不好ImageAttributes,于是引发异常。

解决思路,既然同时满足这两者会报错:

g.DrawImage(PixelFormat特别的Image, xxx, imageAttributes)

那么只要错开其一就行,比如这两种就不会报:

g.DrawImage(常规Image, xxx, imageAttributes); //方法一
g.DrawImage(PixelFormat特别的Image, xxx); //方法二,不使用imageAttributes

所以可以用方法二从原图得到一张像素格式正常的新图,然后用方法一把它画出去,就是弄个副本。

 

-END-

posted on 2019-09-03 12:42  ahdung 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/ahdung/p/11452312.html

在C#中,可以通过一些技术来提高图形绘制的速度,例如使用双缓冲、图像压缩、使用硬件加速等。其中,Graphics.DrawImage方法是用于绘制图像的常用方法之一。 以下是一些提高Graphics.DrawImage方法性能的技巧: 1. 双缓冲:在绘制过程中使用双缓冲技术可以减少闪烁。可以使用一个额外的缓冲区来绘制图像,然后将整个缓冲区一次性绘制到屏幕上。 ```csharp // 创建一个内存缓冲区 Bitmap buffer = new Bitmap(this.Width, this.Height); Graphics bufferGraphics = Graphics.FromImage(buffer); // 在缓冲区上进行绘制 bufferGraphics.DrawImage(image, x, y); // 将缓冲区绘制到屏幕上 e.Graphics.DrawImage(buffer, 0, 0); ``` 2. 图像压缩:如果需要在绘制时进行图像缩放,可以考虑使用适当的图像压缩算法来减少图像数据量,从而提高绘制速度。可以使用`Image.GetThumbnailImage`方法进行图像压缩。 ```csharp // 压缩图像 Image thumbnail = image.GetThumbnailImage(width, height, null, IntPtr.Zero); // 绘制压缩后的图像 e.Graphics.DrawImage(thumbnail, x, y); ``` 3. 硬件加速:使用硬件加速可以利用图形处理单元(GPU)来加速图形绘制。在C#中,可以使用一些第三方库如Direct2D或OpenGL来实现硬件加速的图形绘制。 这些技巧可以帮助提高Graphics.DrawImage方法的执行效率和绘制性能。但是请注意,具体的优化策略还需要根据实际场景和需求进行选择和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值