I'm writing a .dll in which I'm using the following method:
public static Bitmap Merge(Bitmap largeBmp, Bitmap smallBmp)
{
Bitmap bitmap = new Bitmap(largeBmp.Width + smallBmp.Width, Math.Max(largeBmp.Height, smallBmp.Height));
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(largeBmp, 0, 0);
g.DrawImage(smallBmp, largeBmp.Width, 0);
}
return bitmap;
}
I want know if calling this method many times (let's say 10000 times) will cause memory problems. If yes, how can I avoid it? Do I have to call dispose
on the bitmap
objects?
EDIT: I'm calling that method in this way inside of a while
statement:
writerOut.WriteVideoFrame(Merge(lastFrame, lastColorFrame));
How can I dispose the Bitmap
that is returned from the Merge
method (after I have used it)?
As you are returning the bitmap to be used by the caller - then it is the callers responsibility to dispose the object when they're finished with it.
By using the 'using' statement you've already done all you can to ensure that the graphics object is properly disposed. In theory you should be fine - I wouldn;t worry about that unless it becomes a specific problem - then come back and ask how to solve it.
With regard to your edit - unless 'writerOut.WriteVideoFrame' disposes of the bimtap when its finished with it then this will cause a problem.
you could either do :
var bm = Merge( ... );
writerOut.WriteVideoFrame(bm);
bm.Dispose();
or
using(var bm=merge(...))
writerOut.WriteVideoFrame(bm)