开发者

Method incorrectly returning Image object c#.Net

开发者 https://www.devze.com 2023-04-05 22:55 出处:网络
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.

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 obje开发者_开发百科ct 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.


redBmp is getting disposed by your using block, and tout is redBmp cast to the Image type. drop the using block for redBmp.

Menno


by using the using blocks you are disposing the images as soon as you leave the using scope.

try to replace these two lines from the top:

using (Bitmap bmp = new Bitmap(sourceImage))
using (Bitmap redBmp = new Bitmap(sourceImage.Width, sourceImage.Height))

with:

Bitmap bmp = new Bitmap(sourceImage);
Bitmap redBmp = new Bitmap(sourceImage.Width, sourceImage.Height);

now it should work, depending on your program logic you will have to dispose those images manually afterwards.

you could probably dispose bmp also with the using but surely not the redBmp object 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.


You've wrapped redBmp in a using statement so that it's Dispose method is called when the method exits. If you're going to use it outside the method (you've cast it as an Image and are returning it), you shouldn't be disposing of it.

public Image GetRedImage(Image sourceImage)
{
    Bitmap redBmp = null;
    using (Bitmap bmp = new Bitmap(sourceImage))
    {
        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);
            }
        }
    }

    return redBmp as Image;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号