I can not solve - A Graphics object cannot be created from an image that has an indexed pixel format error

1.1k Views Asked by At

I am trying this example but I got "Source pixel format is not supported by the filter" error, and to solve it I tried with these answeres but I got titled error, then I tried to solve it with these answers. But I am out of luck and I keep getting this error.

Can anyone give me a solution?

Heres the code:

        // Open your image
        string path = "sample2.jpg"; //taken from first example links initial image.
        Bitmap image = (Bitmap)Bitmap.FromFile(path);

        // The original bitmap with the wrong pixel format. 
        // You can check the pixel format with originalBmp.PixelFormat
        //Bitmap originalBmp = new (Bitmap)Image.FromFile("YourFileName.gif");

        // Create a blank bitmap with the same dimensions
        Bitmap tempBitmap = new Bitmap(image.Width, image.Height);

        // From this bitmap, the graphics can be obtained, because it has the right PixelFormat
        using (Graphics g = Graphics.FromImage(tempBitmap))
        {
            // Draw the original bitmap onto the graphics of the new bitmap
            g.DrawImage(image, 0, 0);
            // Use g to do whatever you like
            //g.DrawLine(...);
        }

        //Bitmap EditableImg = new Bitmap(image);

        Bitmap a = AForge.Imaging.Image.Clone(tempBitmap, PixelFormat.Format8bppIndexed); //currently getting titled error here.
        AForge.Imaging.Image.SetGrayscalePalette(a);

        // create filter
        DifferenceEdgeDetector filter = new DifferenceEdgeDetector();
        // apply the filter
        filter.ApplyInPlace(image);

error image for reference: enter image description here

0

There are 0 best solutions below