So I am currently working on an image resizer which works/worked pretty fine but with the exception that it gave me an OutOfMemoryException when I was processing too many images at once crashing the program.
So in order to fix that I have wrapped the methods inside a using statement so the Bitmaps can be disposed correctly.
However I noticed that if I am returning my Bitmap inside the using statement I get this "ArgumentException was unhandled" message
Here my ImageResize method:
public Bitmap ResizeImage(MemoryStream ms, Size size)
{
if (comboBox2.Text == "Pixel")
{
using (Bitmap img = new Bitmap(new Bitmap(ms, true), size.Width, size.Height))
{
var original = new Bitmap(ms, true);
Graphics graphic = Graphics.FromImage(img);
//IRRELEVANT CODE.....
return img;
}
}
else
{
return null;
}
And here when i try to save my image outside the ImageResize method:
private void button1_Click(object sender, EventArgs e)
{
//IRRELEVANT CODE ...
img = ResizeImage(memory, new Size(getX(), getY()));
//IRRELEVANT CODE ...
img.Save(outputFileName, codec, encoderParams); //<-Exception occurs here
}
When I remove the using statement everything works perfectly fine again, however I have to use the using blocks to dispose the Bitmap and therefor to prevent the memory leak. Also when I save the image inside the using statement it works fine too, but that is not a solution in my case.
What am I doing wrong? To me it seems like the Bitmap is not returned correctly.
I appreciate any help and thanks in advance Ravand