從你的字節操作部的說明中,它出現您正在從8位轉換的圖像數據以1位正確。 如果是這種情況,並且您沒有具體的原因使用您自己的代碼從頭開始,則可以使用System.Drawing.Bitmap和System.Drawing.Imaging.ImageCodecInfo來簡化創建有效TIFF文件的任務。這使您可以使用不同類型的壓縮保存未壓縮的1位TIFF或壓縮文件。代碼如下:
// first convert from byte[] to pointer
IntPtr pData = Marshal.AllocHGlobal(imgData.Length);
Marshal.Copy(imgData, 0, pData, imgData.Length);
int bytesPerLine = (imgWidth + 31)/32 * 4; //stride must be a multiple of 4. Make sure the byte array already has enough padding for each scan line if needed
System.Drawing.Bitmap img = new Bitmap(imgWidth, imgHeight, bytesPerLine, PixelFormat.Format1bppIndexed, pData);
ImageCodecInfo TiffCodec = null;
foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders())
if (codec.MimeType == "image/tiff")
{
TiffCodec = codec;
break;
}
EncoderParameters parameters = new EncoderParameters(2);
parameters.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionLZW);
parameters.Param[1] = new EncoderParameter(Encoder.ColorDepth, (long)1);
img.Save("OnebitLzw.tif", TiffCodec, parameters);
parameters.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
img.Save("OnebitFaxGroup4.tif", TiffCodec, parameters);
parameters.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionNone);
img.Save("OnebitUncompressed.tif", TiffCodec, parameters);
img.Dispose();
Marshal.FreeHGlobal(pData); //important to not get memory leaks
首先,您可以嘗試查看例如當你在那裏做相同的步驟時,Photoshop會生成。 – Joey 2014-11-14 13:12:39
規格:https://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf – 2014-11-14 13:14:43
您可能指定了錯誤的BITSPERSAMPLE和/或SAMPLESPERPIXEL值。嘗試使用AsTiffTagViewer實用程序打開您的圖像並查看它將顯示的內容。 – Bobrovsky 2014-11-14 19:02:08