Save bitonal TIFF images with C# and CCITT4
This example shows how to save a bitonal TIFF document with photometric 1 and CCITT4 compression. Some (buggy) image viewers, such as Windows Picture and Fax Viewer used in Windows XP, ignore the photometric interpretation tag of group 3 and group 4 TIFF documents by assuming that black is always encoded with 1, and therefore display them as reversed when the photometric is set to 1.
/**
While saving bitonal TIFF images, Nutrient .NET SDK (formerly GdPicture.NET) (starting V.10) behaves in the following way:
- The photometric is set to 0 if the first entry of the color palette is white and the second entry is black.- The photometric is set to 1 if the first entry of the color palette is black and the second entry is white.- The photometric is set to 3 for other cases.
So if you need to maximize compatibility with 'Windows Picture and Fax Viewer', you have to ensure to save your image using photometric mode 0. For such purpose you have to process like this:
1. Check the intented photometric of the image by obtaining color palette information.2. In case of photometric 1, you have to negative the image pixel and invert the two palette entries.
So a simple code snippet here demonstrates, how to enforce the photometric 0 of an existing tiff image.*/
// We assume that GdPicture has been correctly installed and unlocked.GdPictureImaging oGdPictureImaging = new GdPictureImaging();int ImageID = oGdPictureImaging.CreateGdPictureImageFromFile("c:\\myfile.tif", true);if (oGdPictureImaging.GetStat() == GdPictureStatus.OK){ if ((oGdPictureImaging.GetBitDepth(ImageID) == 1) && (oGdPictureImaging.PaletteGetEntry(ImageID, 0).ToArgb() == Color.Black.ToArgb()) && (oGdPictureImaging.PaletteGetEntry(ImageID, 1).ToArgb() == Color.White.ToArgb())) { if ((oGdPictureImaging.FxNegative(ImageID) == GdPictureStatus.OK) && (oGdPictureImaging.PaletteSet(ImageID, new Color[] { Color.White, Color.Black }) == GdPictureStatus.OK) && (oGdPictureImaging.SaveAsTIFF(ImageID, "c:\\myfile.tif", TiffCompression.TiffCompressionCCITT4) == GdPictureStatus.OK)) MessageBox.Show("Done!", "Bitonal TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Information); else MessageBox.Show("Error: " + oGdPictureImaging.GetStat().ToString(), "Bitonal TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error); } oGdPictureImaging.ReleaseGdPictureImage(ImageID);}else MessageBox.Show("Error: " + oGdPictureImaging.GetStat().ToString(), "Bitonal TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);oGdPictureImaging.Dispose();
This code sample is an example that illustrates how to use our SDK. Please adapt it to your specific use case.