将图像保存为Tiff

时间:2012-03-06 14:32:20

标签: c# .net gdi+ tiff

我从库中获取一个字节数组,我希望将其保存为TIFF文件灰度/每像素16位。

我使用这种方法:

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
}

我收到错误&#34; GDI +&#34;中发生了一般错误。 在Vista / 32位和Win7 / 64位上都会出现此问题。 我使用的是.NET 4.0

修改

如果我将ImageFormat.Tiff更改为ImageFormat.Bmp,我就不会有错误。但它仍然是我想要的TIFF图像。

2 个答案:

答案 0 :(得分:4)

这是解决您问题的解决方案 Solving "A generic error occurred in GDI+" exception.

答案 1 :(得分:2)

GDI +不支持PixelFormat.Format16bppGrayScale。它与TIFF无关。此格式尚未实施。