2
votes

I'm getting a byte array from a library and I want to save it as a Tiff file grayscale / 16-bit per pixel.

I'm using this method to do so:

private static void CreateBitmapFromBytes(byte[] pixelValues)
{
  Bitmap pic = new Bitmap(1024, 1024, PixelFormat.Format16bppGrayScale);

  BitmapData picData = pic.LockBits
   ( new Rectangle(0, 0, pic.Width, pic.Height)
   , ImageLockMode.ReadWrite
   , pic.PixelFormat
   );
  IntPtr pixelStartAddress = picData.Scan0;

  Marshal.Copy(pixelValues, 0, pixelStartAddress, pixelValues.Length);

  pic.UnlockBits(picData);
  pic.Save("grid.tif", ImageFormat.Tiff); //< HERE IS THE ERROR
}

And I get the error "A generic error occurred in GDI+". The problem occurs both on Vista/32-bit and Win7/64-bit. I'm using .NET 4.0

EDIT:

If I change ImageFormat.Tiff to ImageFormat.Bmp I don't have the error. But it's still a TIFF image that I want.

2
At what line do you receive the error? Can you supply some pixel value bytes? - CodingBarfield
one of reasons - you dont have permission to write to directory - Renatas M.
Bytes are... bytes. Taken 2 by 2 they're coding the grayscale on ushorts. - gregseth
Which line you getting the error ? and how about using a free tiff lib: bitmiracle.com/libtiff/help/create-16-bit-grayscale-tiff.aspx | bitmiracle.com/libtiff - Lost_In_Library
Why not use an external lib, but isn't it supposed to work natively? - gregseth

2 Answers

4
votes
2
votes

PixelFormat.Format16bppGrayScale is unsupported in GDI+. It has nothing to do with TIFF. This format is not yet implemented.