TextureBrush内存不足异常 [英] TextureBrush Out of Memory Exception

查看:202
本文介绍了TextureBrush内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在另一张图像的顶部放置水印.这是我的代码:

I'm trying to place a watermark ontop of another image. This is my code:

var imgPhoto = Image.FromFile(filePath);
var grPhoto = Graphics.FromImage(imgPhoto);

var point = new Point(imgPhoto.Width - imgWatermark.Width, imgPhoto.Height - imgWatermark.Height);

var brWatermark = new TextureBrush(imgWatermark, new Rectangle(point.X, point.Y, imgWatermark.Width, imgWatermark.Height));

grPhoto.FillRectangle(brWatermark, new Rectangle(point, imgWatermark.Size));
imgPhoto.Save(outputFolder + @"\" + filename);

但是,发生一个问题,TextureBrush抛出了内存不足异常.我到处搜寻,但找不到真正好的解决方案.据我所知,在TextureBrush尝试执行其工作之前,没有任何处理.

One problem occurs however, the TextureBrush throws an out of memory exception. I've searched around but I couldn't really find a good solution. As far as I can see nothing is disposed before TextureBrush tries to do its job.

推荐答案

我敢打赌filePath处的文件很大.

I am willing to bet that the file at filePath is fairly large.

OutOfMemoryException通常在具有.NET堆碎片的情况下发生,因此在这里您可能试图加载大于运行时中任何可用内存块的文件.

OutOfMemoryException usually happens when you have .NET heap fragmentation, so here you are probably trying to load a file which is larger than any available block of memory in the Runtime.

尝试压缩图像文件,或将其裁剪以使其较小(如果可以接受的话).

Try compressing your image file, or crop it to make it smaller if that is acceptable.

编辑 尝试在图像编辑器(例如MS Paint或Paint.NET)中打开文件.然后重新保存.

EDIT Try opening your file in an image editor such as MS Paint, or Paint.NET. Then re-save it.

如果这不起作用,请尝试以下操作(基本上确保矩形的边界在水印图像内): http://www.owenpellegrin.com/Blog/net/内存不足错误与gdi-texturebrush/

If that doesn't work, try this (basically make sure your rectangle's bounds are inside the watermark image): http://www.owenpellegrin.com/blog/net/out-of-memory-errors-with-gdi-texturebrush/

这篇关于TextureBrush内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆