问题
Here's my problem :
I need to create a TIFF and a PNG with a Palette containing specific colors and alpha.
I am actually able to deal with the PNG but not with the TIFF.
I've search on Internet and found out that TIFF should handle transparency, but that not all software can.
I've tried many way to load the TIFF, but they all lead to a Palette with reseted Alpha values.
So I think that the problem comes from how I save the TIFF, but I can't find any other way.
Just in case : The palette is set with #00FFFFFF, and then with #FFXXXXXX, #FEXXXXXX, etc. Whenever I load the TIFF, the colors are back to : #FFFFFFFF, #FFXXXXXX, #FFXXXXXX, etc
CreateImage
public static BitmapSource CreateImage()
{
int width = ImageGenerator.sizeW;
int height = ImageGenerator.sizeH;
int bytesPerPixel = (format.BitsPerPixel + 7) / 8;
int stride = width * bytesPerPixel;
byte[] pixels = new byte[height * stride];
List<Color> colors = new List<Color>();
PopuplateColors(ref colors);
BitmapPalette myPalette = new BitmapPalette(colors);
PopulatePixels(height, stride, ref pixels);
BitmapSource image = BitmapSource.Create(width, height, 96, 96, format, myPalette, pixels, stride);
if (imageStreamSource != null)
imageStreamSource.Dispose();
return image;
}
CreateTIFF
public static void CreateTIFF()
{
BitmapSource image = CreateImage();
FileStream stream = new FileStream("test.tif", FileMode.Create);
TiffBitmapEncoder encoder = new TiffBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);
stream.Dispose();
}
GetTIFF
public static ImageBrush GetTIFF()
{
/* Another try
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri("test.tif",UriKind.Relative);
bitmap.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
bitmap.EndInit();
imagePalette = bitmap.Palette;
CreateData(bitmap);
return new ImageBrush(bitmap);*/
//The last try i've done, i've looked at the palette through debug
Bitmap bitmap = new Bitmap("test.tif");
System.Drawing.Imaging.ColorPalette cp = bitmap.Palette;
bitmap.MakeTransparent(cp.Entries[0]);
return new ImageBrush();
/* How I normally read the TIFF
TiffBitmapDecoder decoder = new TiffBitmapDecoder(GetImage("test.tif"), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
imagePalette = bitmapSource.Palette;
CreateData(bitmapSource);
return new ImageBrush(bitmapSource);*/
}
回答1:
Palette or indexed color TIFF does not support alpha values in the color map. The entries are 16 bit per color component, but only R, G, B (and no Alpha component) are stored. If you try to store a TIFF with alpha in the color map, the alpha will be lost.
You could probably store the TIFF with a separate "Transparency mask" (single bit mask) though. This will be a separate IFD, meaning some software will probably not merge it with the RGB values to display a transparent image. But if you control the software that reads and writes the TIFF file, I think this will be your best option for transparency.
Another option, if you need the transparency information in the color map (and you control the reading and writing), is to either make the first color in the color map transparent always, or add a custom tag with the transparent index that you can set to transparent. Your image should still display correctly in other software, but without the transparency.
See TIFF tags ColorMap and PhotometricInterpretation (there is also an Indexed
extension tag, but it doesn't change the number of components stored in the color map for an RGB image).
来源:https://stackoverflow.com/questions/48929763/save-tiff-with-transparency-alpha-channel