0

I am receiving an image from an external system in the form of a sequence of BGR values followed by an empty byte. The sequence looks sort of like...

[B,G,R,0,B,G,R,0,...,B,G,R,0] where each BGR0 is a single pixel in an image.

I need this in a .NET Bitmap so I can perform some manipulations on it and have come up with the following function to do so:

    private Bitmap fillBitmap(byte[] data, int width, int height)
    {
        Bitmap map = new Bitmap(width, height);
        for (int i = 0; i < data.Length; i += 4)
        {
            int y = ((i / 4) / width);
            int x = ((i / 4) - (y * width));
            int b = data[i];
            int g = data[i + 1];
            int r = data[i + 2];
            Color pixel = Color.FromArgb(r, g, b);
            map.SetPixel(x, y, pixel);
        }
        return map;
    }

This would normally be ok except that most of my images are 1920x1200... so I have a loop that's iterating over 2 million times. Even then that wouldn't be so bad as 2 million iterations shouldn't be very taxing on any modern processor.

But for some reason, this loop can take upwards of 5-15 seconds to run on a pretty beefy Xeon on my server. It would be trivial to parallelize the loop but I suspect there is an even better way of going about this. Any help would be greatly appreciated!

Dabloons
  • 1,462
  • 2
  • 17
  • 30
  • SetPixel is very slow, see more information here http://stackoverflow.com/questions/7768711/setpixel-is-too-slow-is-there-a-faster-way-to-draw-to-bitmap – JimmyMcHoover Nov 19 '12 at 22:31

3 Answers3

2

The description of the Bitmap.LockBits Method says,

You can change the color of an image with the SetPixel method, although the LockBits method offers better performance for large-scale changes.

An alternative, I'd guess, might be to use the Bitmap(Stream) Constructor, after you create a Stream which matches the file format of a bitmap.

ChrisW
  • 54,973
  • 13
  • 116
  • 224
0

Check some FastBitmap implementation that will help you set the array from which you will be able to generate the image.

Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33
0

A call to GetPixel and SetPixel makes an interop call to native functions. Each time you call one of the functions the Bitmap image is locked the corresponding pixel/bytes is modified and then the image is finally unlocked. You can imagine performing this repeatedly is highly inefficient.

As others have suggested use the LockBits method, although you will have to use unsafe code I imagine. Now if this is allowed, use it. It allows direct access to the Bitmap's pixels in an unmanaged memory buffer.

Science_Fiction
  • 3,403
  • 23
  • 27