这是我的问题:
我需要创建一个 TIFF 和一个 PNG,其中调色板包含特定颜色和 Alpha。
我实际上能够处理 PNG,但不能处理 TIFF。
我在互联网上搜索并发现 TIFF 应该处理透明度,但并非所有软件都可以。
我尝试了很多方法来加载 TIFF,但它们都会导致调色板具有重置的 Alpha 值。
所以我认为问题出在我如何保存 TIFF,但我找不到任何其他方法。
以防万一:调色板设置为 #00FFFFFF,然后设置为 #FFXXXXXX、#FEXXXXXX 等。每当我加载 TIFF 时,颜色都会恢复为:#FFFFFFFF、#FFXXXXXX、#FFXXXXXX 等
创建图像
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;
}
创建TIFF
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);*/
}