aihot  2017-05-18 00:52:19  图像处理 |   查看评论   
1.Image.Palette属性的set部分
public void set_Palette(ColorPalette value)
{
    
this._SetColorPalette(value);
}


2.设置调色板
private void _SetColorPalette(ColorPalette palette)
{
    
//将调色板对象转换成内存块
    IntPtr ptr = palette.ConvertToMemory();
    
//调用API函数设置图像的调色板
    int status = SafeNativeMethods.Gdip.GdipSetImagePalette(new HandleRef(thisthis.nativeImage), ptr);
    
if (ptr != IntPtr.Zero)
    {
        Marshal.FreeHGlobal(ptr);
    }
    
if (status != 0)
    {
        
throw SafeNativeMethods.Gdip.StatusException(status);
    }
}
复制代码

    通过这里我们可以发现,GDI+中的Image是对API的封装,直接修改调色板中的颜色并不会调用到GdipSetImagePalette这个API函数,当然不会生效了。

    我们的任务就是要让GdipSetImagePalette运行一次,使修改过的调色板生效。

解决问题

    我们可以这么处理:(1)将调色板保存到临时变量palette内;(2)修改调色板中的颜色;(3)将palette赋值给Image.Palette以调用GdipSetImagePalette。这么处理之后,效果跟方法2一样,但是不用在内存中创建临时图像,效率更高。代码如下:

复制代码
//创建灰度图像
Bitmap bCustom = new Bitmap(22, PixelFormat.Format8bppIndexed);
//修改调色板为256级灰色
ColorPalette palette = bCustom.Palette;    //用临时变量保存调色板
for (int i = 0; i < palette.Entries.Length; i++)
    palette.Entries[i] 
= Color.FromArgb(255, i, i, i);
//让修改过的调色板生效
bCustom.Palette = palette;
//
 

除特别注明外,本站所有文章均为 赢咖4注册 原创,转载请注明出处来自自定义(手动调整).net中图像的调色板(How to adjust (customize) image's palet

留言与评论(共有 0 条评论)
   
验证码:
[lianlun]1[/lianlun]