aihot  2017-05-18 00:52:19  图像处理 |   查看评论   
 palette;
}


/// <summary>
/// 根据颜色深度,创建对应的调色板
/// </summary>
/// <param name="depth">颜色深度,即表示颜色所用的位数</param>
/// <returns>返回调色板</returns>
private ColorPalette CreateColorPalette(int depth)
{
    
//根据颜色数,决定使用什么样的调色板
    PixelFormat pixelFormat = PixelFormat.Format1bppIndexed;
    
if (depth > 2)
        pixelFormat 
= PixelFormat.Format4bppIndexed;
    
if (depth > 16)
        pixelFormat 
= PixelFormat.Format8bppIndexed;
    
return CreateColorPalette(pixelFormat);
}


/// <summary>
/// 创建256级灰度调色板
/// </summary>
/// <returns>返回调色板</returns>
private ColorPalette CreateGrayscalePalette()
{
    ColorPalette palette 
= CreateColorPalette(PixelFormat.Format8bppIndexed);
    
for (int i = 0; i < palette.Entries.Length; i++)
        palette.Entries[i] 
= Color.FromArgb(255, i, i, i);
    
return palette;
}
复制代码

    使用上述方法创建好调色板,按需要修改调色板的颜色之后,用它来替换图像原有的调色板就可以了。

    3.为什么不能直接修改调色板?

    今天中午无聊,想看看为什么不能直接修改调色板。于是用Relector查看Image.Palette属性的设置部分 ,相关代码如下:

[lianlun]1[/lianlun]