110,534
社区成员
发帖
与我相关
我的任务
分享
public static void ChangeTo8bppIndexed(this Bitmap Bmp,PaletteType palettetype= PaletteType.PaletteTypeOptimal,DitherType ditherType = DitherType.DitherTypeErrorDiffusion,int optimalColors =256)
{
int Entries;
// http://msdn.microsoft.com/en-us/library/ms534159(v=vs.85).aspx
switch (palettetype)
{
case PaletteType.PaletteTypeFixedBW:
Entries = 2;
break;
case PaletteType.PaletteTypeFixedHalftone8:
Entries = 16;
break;
case PaletteType.PaletteTypeOptimal:
if (optimalColors <= 0 || optimalColors > 16)
throw new ArgumentOutOfRangeException("调色板数量必须在[1,16]的范围内");
Entries = optimalColors;
break;
default:
throw new ArgumentException ("暂时不支持的调色板项");
}
int[] Pal = new int[2 + Entries];
Pal[0] = (int)PaletteFlags.GrayScale; // Flag标志
Pal[1] = Entries; // Count参数
if (palettetype == PaletteType.PaletteTypeOptimal)
GdipInitializePalette(Pal, palettetype, Entries, 0, Bmp.NativeHandle());
else
GdipInitializePalette(Pal, palettetype, Entries, 0, IntPtr.Zero);
if (palettetype == PaletteType.PaletteTypeOptimal)
if (ditherType!=DitherType.DitherTypeNone && ditherType!=DitherType.DitherTypeSolid && ditherType!=DitherType.DitherTypeErrorDiffusion)
throw new ArgumentException("抖动方式和调色板不匹配.");
GdipBitmapConvertFormat(Bmp.NativeHandle(), Convert.ToInt32(PixelFormat.Format8bppIndexed), ditherType, palettetype, Pal, 50f);
}