Graphics with indexed pixel format run time error

0

ok so I have two video cameras that my software is using. The 1st is a computer webcam. when I connect to and run this code:

        Bitmap currentFrame = videoSourcePlayer.GetCurrentVideoFrame();
        Graphics g = Graphics.FromImage(currentFrame);

        g.DrawImage(currentFrame, videoSourcePlayer.imagex, videoSourcePlayer.imagey, videoSourcePlayer.imgWidth, videoSourcePlayer.imgHeight);
        pictureBox1.Image = currentFrame;
        return currentFrame;

the webcam works fine. I am able to copy my zoom and pan, just fine.

but when I connect to the other cam and run the same code, I get this error : HResult=0x80131500 Message=A Graphics object cannot be created from an image that has an indexed pixel format. Source=System.Drawing StackTrace: at System.Drawing.Graphics.FromImage(Image image)

so I tried this code :

private Bitmap CaptureScreen()
    {
        Bitmap originalBmp = videoSourcePlayer.GetCurrentVideoFrame();

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

        // From this bitmap, the graphics can be obtained, because it has the right PixelFormat
        using (Graphics g = Graphics.FromImage(tempBitmap))
        {

            g.DrawImage(originalBmp, videoSourcePlayer.imagex, videoSourcePlayer.imagey, videoSourcePlayer.imgWidth, videoSourcePlayer.imgHeight);
            pictureBox1.Image = originalBmp;
            return originalBmp;
        }
    }

that code fix the runtime error, I get but is get rid of my zoom and pan. I do not know why? but I do have a zoom and pan function that works with the 1st code block but not with the second.

how can I fix the A Graphics object cannot be created from an image that has an indexed pixel format runtime error but keep my zoom and pan?

c#
aforge
asked on Stack Overflow Jan 9, 2019 by brandon

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0