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?

brandon
  • 73
  • 10
  • https://stackoverflow.com/q/17313285/1070452 – Ňɏssa Pøngjǣrdenlarp Jan 09 '19 at 17:19
  • @WelcomeOverFlow if you read my post you will see I tried that and it did not work for what I am trying to do. When I do that I lose all my zoom in. – brandon Jan 09 '19 at 18:18
  • You're always drawing the image with its original dimensions and location (`videoSourcePlayer.*`). Why would you expect this to be zoomed or panned? – 3Dave Jan 09 '19 at 18:45
  • @brandon Your code is strange; you are painting an image onto itself. Why? Can't you just assign the `videoSourcePlayer.GetCurrentVideoFrame()` to the picture box directly? And why does the function _return_ the bitmap when the job of assigning it to the picture box is already completed? – Nyerguds Jan 09 '19 at 21:16
  • By the way... the reason you can't make a `Graphics` object for it is that the `Graphics` class paints _colours_. The pixel data of an indexed image are _not colours_; they are references to indices on a limited (256 colours or less) colour palette. The two systems are simply incompatible. – Nyerguds Jan 09 '19 at 21:29
  • @Nyerguds, I am taking CurrentVideoFrame and zoom in to it. That is what I cannot just you CurrentVideoFrame. The user can zoom in to it or out or pan. – brandon Jan 09 '19 at 22:54
  • @ Nyerguds, if they are incompatible what should it do? – brandon Jan 09 '19 at 22:56
  • @ 3Dave the zoom and pan are in the g.DrawImage(currentFrame, videoSourcePlayer.imagex, videoSourcePlayer.imagey, videoSourcePlayer.imgWidth, videoSourcePlayer.imgHeight); – brandon Jan 09 '19 at 22:57
  • does anyone know what i should do ? – brandon Jan 10 '19 at 04:57
  • Try simply changing the first line in your original code to `Bitmap currentFrame = new Bitmap(videoSourcePlayer.GetCurrentVideoFrame());`; that should convert it to high-colour. It's still advised to put the graphics handling in a `using` block though; these things _need_ to be disposed afterwards. Likewise, you should dispose the old image that was in pictureBox1.Image after replacing it. – Nyerguds Jan 10 '19 at 15:38

0 Answers0