事實證明,我能夠打造出一個非索引位圖和使用轉換PngBitmapEncoder像這樣:
byte[] ConvertTo8bpp(Bitmap sourceBitmap)
{
// generate a custom palette for the bitmap (I already had a list of colors
// from a previous operation
Dictionary<System.Drawing.Color, byte> colorDict = new Dictionary<System.Drawing.Color, byte>(); // lookup table for conversion to indexed color
List<System.Windows.Media.Color> colorList = new List<System.Windows.Media.Color>(); // list for palette creation
byte index = 0;
unchecked
{
foreach (var cc in ColorsFromPreviousOperation)
{
colorDict[cc] = index++;
colorList.Add(cc.ToMediaColor());
}
}
System.Windows.Media.Imaging.BitmapPalette bmpPal = new System.Windows.Media.Imaging.BitmapPalette(colorList);
// create the byte array of raw image data
int width = sourceBitmap.Width;
int height = sourceBitmap.Height;
int stride = sourceBitmap.Width;
byte[] imageData = new byte[width * height];
for (int x = 0; x < width; ++x)
for (int y = 0; y < height; ++y)
{
var pixelColor = sourceBitmap.GetPixel(x, y);
imageData[x + (stride * y)] = colorDict[pixelColor];
}
// generate the image source
var bsource = BitmapSource.Create(width, height, 96, 96, PixelFormats.Indexed8, bmpPal, imageData, stride);
// encode the image
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Interlace = PngInterlaceOption.Off;
encoder.Frames.Add(BitmapFrame.Create(bsource));
MemoryStream outputStream = new MemoryStream();
encoder.Save(outputStream);
return outputStream.ToArray();
}
加上輔助擴展方法:
public static System.Windows.Media.Color ToMediaColor(this System.Drawing.Color color)
{
return new System.Windows.Media.Color()
{
A = color.A,
R = color.R,
G = color.G,
B = color.B
};
}
注意的警惕:PngBitmapEncoder實際上似乎可以將bpp計數從8減少到4。例如,當我使用6種顏色進行測試時,輸出PNG只有4位。當我使用更色彩豐富的圖像時,它是8位的。看起來像一個功能到目前爲止...雖然它會很好,如果我有明確的控制權。
儘管專門提到GIF,但相同的顏色表主要可能適用於... http://support.microsoft.com/kb/319061只有在做一些非常相似的事情時纔會嘗試使用代碼。 – 2010-12-05 03:08:05