0

i am trying to get image from fingerprint scanner through c# application, but when transferring through UART, to quicken speed, only the upper 4 bits of the pixel is transferred (that is 16 grey degrees). And two adjacent pixels of the same row will form a byte before the transferring.

So converting that byte stream back to image i am using following code:-

MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
pictureBox2.Image = returnImage;

But it is giving me error as

"Parameter not valid"

for :

Image returnImage = Image.FromStream(ms);

I am getting around 500 bytes of data. Could any one please provide any solution so that image when uploaded to PC, the 16-grey-degree image can be extended to 256-grey-degree format i.e. 8-bit BMP format.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
sami manchnda
  • 41
  • 1
  • 1
  • 2
  • Well what format is the data in? – Jon Skeet Aug 06 '13 at 09:07
  • `Image.FromStream` or one of its siblings that deal with other sources actually expect a fully formatted image *file*, ie. a full Jpeg or PNG file, not just the raw pixel values. There's a lot more to an image *file format* than just the pixels. Now, decoding nibbles (4 bits) to bytes is one problem, but getting it into a `Image` object is another. Which one do you require help with? Both? – Lasse V. Karlsen Aug 06 '13 at 09:09
  • Additionally, around 500 bytes of data, consisting of 2 4-bit nibbles each, translates to around 1000 pixels. How big is the image? 40*25? – Lasse V. Karlsen Aug 06 '13 at 09:10

1 Answers1

1

You could transform the nibbles into bytes and then use the transformed data, like so:

public byte[] NibblesToBytes(byte[] data)
{
    byte[] result = new byte[data.Length*2];
    int i = 0;

    foreach (var bits in data)
    {
        // You may need to reverse these two lines, depending on data format:
        result[i++] = (byte)((bits & 0x0F) << 4);
        result[i++] = (byte)(bits & 0xF0);
    }

    return result;
}

Those bytes will be greyscale values. To convert each value to an RGB value, you would need to set each of the R, G and B values to the greyscale value (I expect that you already know that, but I mention it just in case).

But you will need to do more than just this. You will need header information and so on.

These threads might help with that:

Create Bitmap from a byte array of pixel data

Convert byte Array or File Storage to Bitmap Image

Community
  • 1
  • 1
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276