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.