Display image from a byte array quickly
Posted on 2006-04-26
I am building an application which recieves real-time data from a high speed camera (up to 35000fps) and can preview that data to the user. The data is sent as a raw byte array which consists simply of the black and white image data (no headers). This data is transfered over a TCP connection and is downloaded to the user machine as it is receieved.
Since the data is transfered at an immense speed, I am looking for the fastest possible way to display the image on screen ("preview") with the smallest processing. My current method is the following:
public void ConvertPreview(byte pixels, Bitmap bmp)
// Copy byte into Bitmap to be displayed
BitmapData d = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
Marshal.Copy(pixels, 0, d.Scan0, pixels.Length);
// Display image to user
this.pictureBox1.Image = bmp;
since the image gets processed twice (once to insert it into the bitmap, and then again when it is displayed in the picturebox) I am under the impression there must be a faster way to display the images. In addition, sometimes the methods overlap each other and I get "object in use elsewhere" errors (ie. the picturebox is trying to convert the image to be displayed at the same time that the lockbits is being called on the next image in the queue).
Does anyone know how to do any of the following, and if they will improve performance:
(a) skip the picturebox and draw the image directly on the GUI window?
(b) skip inserting the byte into the Bitmap object and just display the raw data?
(c) speed up the current process?
The ideal method would simply take the raw data and display it on the screen - no pictureboxes, no Bitmaps. I'm not sure if this is possible, but this would be the fastest way I can think of.
The fastest conversion method clearly wins in this instance. Unsafe code is very much accepted.