Mirror

How to copy a 2D array with picture greylevels to an image (2) (Views: 100)


Problem/Question/Abstract:

I have been developing a program to display and manipulate medical images which consist of 2D arrays of greyscale values as described. As was observed, the Pixels property is way too slow. Here's what I discovered. I think you'll find it a big improvement.

Answer:

Assuming your data is stored in an array of bytes named TestArray, for example:


TestArray: array[0..127, 0..127] of byte { ... }

ArrayPtr := addr(TestArray); {ArrayPtr: pointer}


In this case we are going to display on the bitmap of a TImage component that has been dropped on the canvas and named Image1.


Image1.Picture.Bitmap.Width := 128;
Image1.Picture.Bitmap.Height := 128;


This is a Windows API function that will copy the bits in TestArray, pointed to by ArrayPtr, into an HBitmap structure, in this case Image1.Picture.Bitmap.Handle.


SetBitmapBits(Image1.Picture.Bitmap.Handle, sizeof(TestArray), ArrayPtr);
Image1.Refresh; {must refresh before changes are displayed}


You still have to deal with the palette, but this technique works great for me.

<< Back to main page