I need to mask image from memory buffers (rectangle area filled with black). So I naively re-use the Bitmap class with ImageFormat.MemoryBmp for my API. This works quite well on my local machine:
public static void MaskBitmap(Bitmap input, Rectangle maskRegion)
{
var bytesPerPixel = Image.GetPixelFormatSize(input.PixelFormat) / 8;
var row = new byte[maskRegion.Width * bytesPerPixel];
var maskData = input.LockBits(maskRegion, ImageLockMode.WriteOnly, input.PixelFormat);
for (var i = 0; i < maskData.Height; ++i)
{
Marshal.Copy(row, 0, maskData.Scan0 + (i * maskData.Stride), row.Length);
}
input.UnlockBits(maskData);
}
However when deploying to production it turns out that the following throw a NotImplementedException
:
var image16 = new Bitmap(512, 512, PixelFormat.Format16bppGrayScale);
I eventually tracked it down to here:
So my question is: is there any existing class in c# that I can re-use to hold images of pixelformat type:
- PixelFormat.Format8bppIndexed:
- PixelFormat.Format16bppGrayScale:
- PixelFormat.Format24bppRgb:
I know GDI+ does not support saving/displaying 16bits image, I simply need a memory structure with image-style access.
Just for reference, I tried the following hack:
var image = new Bitmap(512,512,PixelFormat.Format24bppRgb);
image.Flags = ImageFlags.ColorSpaceGray;
But Flags
is read-only.