views:

29

answers:

1

I'm trying to render a small Bitmap in memory with .NET that needs to be 16 bit Grayscale. The bitmap's format is set to PixelFormat.Format16bppGrayScale. However, Bitmap.SetPixel takes a Color argument. Color in turn takes one byte for each of R, B, and G (and optionally A).

How do I specify a 16-bit gray scale value rather than an 8 bit value when drawing to my bitmap?

+1  A: 

Regardless of the image format, SetPixel() is brutally slow. I never use it in practice.

You can set pixels much faster using the LockBits method, which allows you to quickly marshal managed data to the unmanaged bitmap bytes.

Here is an example of how that might look:

Bitmap bitmap = // ...

// Lock the unmanaged bits for efficient writing.
var data = bitmap.LockBits(
    new Rectangle(0, 0, bitmap.Width, bitmap.Height),
    ImageLockMode.ReadWrite,
    bitmap.PixelFormat);

// Bulk copy pixel data from a byte array:
Marshal.Copy(byteArray, 0, data.Scan0, byteArray.Length);

// Or, for one pixel at a time:
Marshal.WriteInt16(data.Scan0, offsetInBytes, shortValue);

// When finished, unlock the unmanaged bits
bitmap.UnlockBits(data);

Note that 16bpp grayscale appears to be unsupported by GDI+, meaning that .NET doesn't help out with saving a 16bpp grayscale bitmap to a file or a stream. See http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/10252c05-c4b6-49dc-b2a3-4c1396e2c3ab

kbrimington
Thanks. I originally went with SetPixel since I was only writing a 512x512 bitmap to the file system (creating a heightmap of a solid created by rotating a Gaussian distribution around the vertical axis for use in another program). Good point about SetPixel being too slow for most applications. I ultimately went with another heightmap format due to the limitations you reference, but your answer is correct and complete so accepting.
Eric J.