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);
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.
I am willing to bet that the file at filePath
is fairly large.
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.
EDIT Try opening your file in an image editor such as MS Paint, or Paint.NET. Then re-save it.
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/