0

I am trying to convert an 8 bit byte[] array, captured from a machine vision camera, into a png.

Converting a byte array to PNG/JPG I tried following the code from this thread which deals with the same problem but I am getting an error.

This is my code:

  for (int i = 0; i < 1; ++i)
                    {
                        // Wait for an image and then retrieve it. A timeout of 5000 ms is used.
                        IGrabResult grabResult = camera.StreamGrabber.RetrieveResult(5000, TimeoutHandling.ThrowException);
                        using (grabResult)
                        {
                            // Image grabbed successfully?
                            if (grabResult.GrabSucceeded)
                            {
                                // Access the image data.
                                Console.WriteLine("SizeX: {0}", grabResult.Width);
                                Console.WriteLine("SizeY: {0}", grabResult.Height);
                                buffer = grabResult.PixelData as byte[];
                                Console.WriteLine("Gray value of first pixel: {0}", buffer[0]);
                                Console.WriteLine("");

                                // Display the grabbed image.
                                ImageWindow.DisplayImage(0, grabResult);
                            }
                            else
                            {
                                Console.WriteLine("Error: {0} {1}", grabResult.ErrorCode, grabResult.ErrorDescription);
                            }
                        }

                        using (Image image = Image.FromStream(new MemoryStream(buffer)))
                        {
                            image.Save("output.jpg", ImageFormat.Jpeg);  // Or Png
                        }

                        System.Threading.Thread.Sleep(2000);

                    }

This is my error message:

Exception thrown: 'System.ArgumentException' in System.Drawing.dll Exception: Parameter is not valid. enter image description here

palacetrading
  • 71
  • 1
  • 11
  • 1
    Can you please include the complete Stacktrace and Message? And if there is a line mentioned, mark that line in the code? – Fildor Jan 20 '20 at 17:43
  • 2
    Also please clarify if it's .NET Core or .NET Framework – Alexander Goldabin Jan 20 '20 at 17:47
  • @DourHighArch I added my full code above. I used Image because the example I linked started with a byte[] as well. – palacetrading Jan 20 '20 at 17:55
  • @AlexanderGoldabin .NET Framwork, I trigger image capture from the press of a button on a GUI. – palacetrading Jan 20 '20 at 17:57
  • @Fildor The code is failing on this line: using (Image image = Image.FromStream(new MemoryStream(buffer))) - I know because I tried doing Console.Write() before and after that line. I added a picture of the call stack immediately before the breakpoint. Without the breakpoint the call stack is empty. – palacetrading Jan 20 '20 at 18:04
  • I agree with @DourHighArch, are you certain buffer is an image? – LinkedListT Jan 20 '20 at 18:17
  • @LinkedListT Buffer is a byte array. Which is the same as the example I linked AFAIK. – palacetrading Jan 20 '20 at 18:25
  • try this https://stackoverflow.com/a/17668083/9936356 – LinkedListT Jan 20 '20 at 18:32
  • 1
    A few strange things about that code; `using(grabResult)` will dispose `grabResult` before you save it, and you call `image.Save` if `grabResult.GrabSucceeded` is false. Try moving the `image.Save` after `buffer = grabResult.PixelData` and use your debugger to inspect `buffer`. – Dour High Arch Jan 20 '20 at 18:33
  • @DourHighArch What do you mean by false? The code is working fine (showing the image in a pop up window) and is from the camera manufacturer sample kit. I am just stuck on the last part which is saving it out to a png. I tried moving my code into the for loop under the buffer = grabResult.PixelData like you said and I got the same error. – palacetrading Jan 20 '20 at 19:03
  • System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter(); Image img = (Image)converter.ConvertFrom(buffer); img.Save("output.jpg", ImageFormat.Png); // Or Png I added this code and I am getting the same error @LinkedListT – palacetrading Jan 20 '20 at 19:08

0 Answers0