2009-07-01 85 views
3

Windows窗體具有方便的ControlPaint.DrawImageDisabled,以灰色,禁用狀態繪製彩色圖像。有沒有一種方法可以確定,對於給定的顏色,禁用的顏色是什麼(就像它是由DrawImageDisabled繪製的)?如何確定winforms中給定顏色的禁用顏色?

回答

3

反射告訴我們,這是一個創建嘉洛斯是被使用的代碼:

// In class-level declarations in ColorPaint 
private static ImageAttributes disabledImageAttr; 

// In the actual implementation method for DrawImageDisabled 
    if (disabledImageAttr == null) 
    { 
     float[][] newColorMatrix = new float[5][]; 
     newColorMatrix[0] = new float[] { 0.2125f, 0.2125f, 0.2125f, 0f, 0f }; 
     newColorMatrix[1] = new float[] { 0.2577f, 0.2577f, 0.2577f, 0f, 0f }; 
     newColorMatrix[2] = new float[] { 0.0361f, 0.0361f, 0.0361f, 0f, 0f }; 
     float[] numArray2 = new float[5]; 
     numArray2[3] = 1f; 
     newColorMatrix[3] = numArray2; 
     newColorMatrix[4] = new float[] { 0.38f, 0.38f, 0.38f, 0f, 1f }; 
     ColorMatrix matrix = new ColorMatrix(newColorMatrix); 
     disabledImageAttr = new ImageAttributes(); 
     disabledImageAttr.ClearColorKey(); 
     disabledImageAttr.SetColorMatrix(matrix); 
    } 

// To draw the image itself 
using (Bitmap bitmap = new Bitmap(image.Width, image.Height)) 
{ 
    using (Graphics graphics2 = Graphics.FromImage(bitmap)) 
    { 
     graphics2.DrawImage(image, new Rectangle(0, 0, size.Width, size.Height), 0, 0, size.Width, size.Height, GraphicsUnit.Pixel, disabledImageAttr); 
    } 
    graphics.DrawImageUnscaled(bitmap, imageBounds); 
    return; 
} 
+0

謝謝!我沒有想過使用反射器。 – Eric 2009-07-01 14:58:46