It is kind of "I couldn't find the forrest because it is covered by trees."...
But a lesson is to wait some time (In my case a few months :-)) and the look at it again.
The problem is, that I'm doing a series of image operations to scale down a picture and apply rounded corners. One of the operations is:
using (Image img = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap())) {stream = HandleImageFile(img, _width, _height);
}
... blablabla... and then some work with the stream.
The problem is caused by the Bitmap.GetHbitmap() method. I didn't bother to read the documentation when i originally wrote the code... I just looked at intellisence. The problem is that the GetHbitmap returns an IntPtr object, and these objects are not cleaned up by the Garbage Collector (As it is written in the documentation tsk tsk.)
So the solution is:
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
static bool HandleImage()
{IntPtr hBitmap = new IntPtr();
try {hBitmap = bmp.GetHbitmap();
using (Image img = System.Drawing.Image.FromHbitmap(hBitmap)) {stream = HandleImageFile(img, _width, _height);
}
}
catch { throw;}
finally {DeleteObject(hBitmap);
}
}
Check the original docs here:
http://msdn.microsoft.com/en-us/library/1dz311e4(VS.85).aspx
0 comments:
Post a Comment