I am looking at a small Image Cropping and Resizing library written in C#. It consists of a single static class with static methods for resize and crop funcions.
For example the crop method:
public static Image Crop(Image img, Rectangle cropArea)
{
var bmpImage = new Bitmap(img);
var bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
return bmpCrop;
}
I know that typically, Bitmap objects need to be disposed of to prevent memory leaks, but Is this a different case because of the class being static?
I dont want to implement this library and run into memory problems down the road
First, when using Bitmaps I would warn against using them from a static class if at any time you wish to use more than one thread. No bitmap can be used by more than one thread at a time. So for example if you next did this in one thread:
and on another
you will get an invalid parameter exception.
Next, in your example, I see three image instances…
Right now you aren’t disposing bmpImage in this function and would need to.
After this call it would be the callers responsibility at some point to dispose of img and bmpCrop