I am using barcode device with OPOS DirectIO commands to switch between modes.
the problem is when i use the device to capture image i get a byte array
(from the device specifications the image i get is "752x480 GrayScale 256,16,2" in JPG format) and i couldn't find a way to convert it to (Image).
I've tried below code
MemoryStream ms = new MemoryStream(scannedByteArray);
pictureBox1.Image = Image.FromStream(ms);
but it didn't work. always throwing "Parameter is not valid" Exception.
Also tried this:
byte[] buffer = scannedByteArray;
var bitmap = new Bitmap(752, 480, PixelFormat.Format24bppRgb);
var bitmap_data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
Marshal.Copy(buffer, 0, bitmap_data.Scan0, buffer.Length);
bitmap.UnlockBits(bitmap_data);
and an alternative way with stride :
Bitmap im = new Bitmap(752, 480, -752,
PixelFormat.Format8bppIndexed,
Marshal.UnsafeAddrOfPinnedArrayElement(scannedByteArray, 0));
and i was getting bad image or corrupted image with wrong colors.
i tried so many solutions (as below ones) but none of them helped
1st: Byte Array to Image Conversion
2nd: How to convert image to byte array (here i was trying to do the opposite solution and it didn't work) I've been spending 3 days on this problem and still no idea how i can display the image or save it.
Note 1: the byte array size is variable (not fixed every time i capture the image i get a different size of byte array)
Note 2: I've tried pixel formats as 24 bits, 16 bits and 8 bits with different values of stride and always get bad or corrupted image displayed.
UPDATE:
I tried to use JpegBitmapDecoder as follow:
JpegBitmapDecoder decoder = new JpegBitmapDecoder(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapsource = decoder.Frames[0];
Bitmap bitmap = new Bitmap(bitmapsource.PixelWidth, bitmapsource.PixelHeight);
Rectangle rec = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
BitmapData bitmapdata = bitmap.LockBits(rec, ImageLockMode.WriteOnly,
(bitmapsource.Format.BitsPerPixel == 24 ? System.Drawing.Imaging.PixelFormat.Format24bppRgb : System.Drawing.Imaging.PixelFormat.Format32bppArgb));
bitmapsource.CopyPixels(System.Windows.Int32Rect.Empty, bitmapdata.Scan0, bitmapdata.Height * bitmapdata.Stride, bitmapdata.Stride);
bitmap.UnlockBits(bitmapdata);
but the Error "No imaging component suitable to complete this operation" appeared at the first line and i am sure the bytes i receive from the device is not corrupted because the device works on its demo without any problems and without any changes in its configurations.
UPDATE 2:
here is sample of bytes i get from the device when capturing image.
https://drive.google.com/file/d/1kBuLDMTe9snwin9voEJ7z6kr9QKizftf/view?usp=sharing