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!!

unwind
  • 391,730
  • 64
  • 469
  • 606
Manoj
  • 451
  • 1
  • 7
  • 8

2 Answers2

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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)? – Samuel Mar 12 '09 at 14:07
  • 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[,]. – Jon Skeet Mar 12 '09 at 14:09
  • I see now, http://stackoverflow.com/questions/275073/why-does-c-multidimensional-arrays-not-implement-ienumerablet if anyone is curious why byte[,] won't work for JaredPar's answer. – Samuel Mar 12 '09 at 14:20
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.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 1
    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 Skeet Mar 12 '09 at 14:11
  • @Jon, completely agree. This is very dependent on what the camera actually returns. – JaredPar Mar 12 '09 at 14:18
  • If it a multidimensional array, you can just cast it since you know the type. array.Cast().ToArray() – Samuel Mar 12 '09 at 14:27
  • This method is so horrifically terrible in terms of both speed and memory usage. – MikeP May 06 '10 at 05:20