3

In my project after long process, i got a 2 dimensional byte array from the IR camera.

The byte array holds image in it...

How to convert that byte array to image in C#..

I know that by

MemoryStream ms = new MemoryStream(byteArray);
System.drawing.Image im = Image.FromStream(ms);

We can pass 1 dimensional array and convert it into image..

If i pass 2 dimensional array as a single dimensional array.. it shows error..

How to rectify it..???? or else how to convert 2 dimensional byte array to image...???

Thank you!!

2 Answers 2

6

If it's a rectangular array (i.e. a byte[,]) instead of a jagged array (byte[][]) then you may be able to do it pretty simply with some unsafe code.

Have a look at my parallel Mandelbrot set generation code - only the bottom bit is interesting, where it constructs a Bitmap from a palette and a block of data:

byte[] data = query.ToArray();

unsafe
{
    fixed (byte* ptr = data)
    {
        IntPtr scan0 = new IntPtr(ptr);
        Bitmap bitmap = new Bitmap(ImageWidth, ImageHeight, // Image size
                                   ImageWidth, // Scan size
                                   PixelFormat.Format8bppIndexed, scan0);
        ColorPalette palette = bitmap.Palette;
        palette.Entries[0] = Color.Black;
        for (int i=1; i < 256; i++)
        {
            palette.Entries[i] = Color.FromArgb((i*7)%256, (i*7)%256, 255);
        }
        bitmap.Palette = palette;
        // Stuff
    }
}

I don't know whether you can unpin the array after constructing the bitmap - if I were using this for production code I'd look at that more closely.

Sign up to request clarification or add additional context in comments.

3 Comments

Did Jon Skeet get bested, or is there a problem with JaredPar's answer (other than needing LINQ if the asker doesn't have it)?
I'm not necessarily convinced that Jared's solution will work - I'll explain why in a comment in a minute. Also it depends on whether the OP has a byte[][] or a byte[,].
I see now, stackoverflow.com/questions/275073/… if anyone is curious why byte[,] won't work for JaredPar's answer.
3

If you want the byte arrays to be processed inorder, you can do the following

byte[][] doubleArray = GetMyByteArray();
byte[] singleArray = doubleArray.SelectMany(x => x).ToArray();
MemoryStream ms = new MemoryStream(singleArray);
System.drawing.Image im = Image.FromStream(ms);

The SelectMany method essentially takes the arrays of arrays and returns the elements in order. Starting with the first element of the first array, finishing that array and then moving onto the next. This will continue until all elements are processed.

4 Comments

Doesn't this assume that the stream is in some recognised image format, e.g. png, jpg, gif? If the IR camera is giving a 2D byte array, I suspect it's raw data - which is why I suggested my solution. I'm not saying this won't work, but it will depend on exactly what format the camera provides.
@Jon, completely agree. This is very dependent on what the camera actually returns.
If it a multidimensional array, you can just cast it since you know the type. array.Cast<byte>().ToArray()
This method is so horrifically terrible in terms of both speed and memory usage.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.