I am trying to write to a 16 bit grayscale image in C# and everything is fine, but when I try to save it, it gives me a generic GDI+ error.
here is the code:
byte [] data = new byte [200 * 100 * 2];
Random end = new Random();
for (int i = 0; i < data.Length; i++)
data[i] = (byte)end.Next(0, 255);
Bitmap bmp = new Bitmap(200, 100, PixelFormat.Format16bppGrayScale);
BitmapData bd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.WriteOnly, PixelFormat.Format16bppGrayScale);
Marshal.Copy(data, 0, bd.Scan0, data.Length);
bmp.UnlockBits(bd);
// on this line I get a "parameter not valid exceptioin"
// and if I remove it, I cannot save the original image (GDI+ generic error occurs)
Bitmap noError = new Bitmap(bmp);
bmp.Dispose();
bmp.Save("D:\\Tes.bmp", ImageFormat.Bmp);
I have tried everything from cloning the original image to a different image and everything. Nothing works.
PS. When the pixel forma is 8BppIndexed, it works just fine.