Im having an issue where I got a method that takes an Image object, processes it to 1 colour channel (the other 2 are solid black) then returns a new image from the process.
Now the issue I have is that when I create the new image in the method and I look at the object during debugging the image object appears perfectly fine. BUT when I return it to an empty Image object the properties inside that Image object all show “System.ArgumentException”
here is the code of that Method:
public Image GetRedImage(Image sourceImage)
{
using (Bitmap bmp = new Bitmap(sourceImage))
using (Bitmap redBmp = new Bitmap(sourceImage.Width, sourceImage.Height))
{
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
Color pxl = bmp.GetPixel(x, y);
Color redPxl = Color.FromArgb((int)pxl.R, 1, 1);
redBmp.SetPixel(x, y, redPxl);
}
}
Image tout = (Image)redBmp;
return tout;
}
}
anyone got any ideas on what the hell is going on?
many thanks.
by using the
usingblocks you are disposing the images as soon as you leave the using scope.try to replace these two lines from the top:
with:
now it should work, depending on your program logic you will have to dispose those images manually afterwards.
you could probably dispose
bmpalso with the using but surely not theredBmpobject as you are basically returning it, so either you clone it and return a clone, or you do not dispose it, or you return a disposed unusable object like what is happening right now.