public class ControlImage
{
    private ControlImage() { }

    [ThreadStatic]
    private static ImageAttributes disabledImageAttr;

    public static void DrawImageDisabled(Bitmap image, Rectangle imageBounds, bool unscaledImage)
    {
        if (image == null)
        {
            throw new ArgumentNullException("image");
        }
        Size size = image.Size;
        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);
        }
        using (Graphics g = Graphics.FromImage(image))
        {
            if (unscaledImage)
            {
                using (Bitmap bitmap = new Bitmap(size.Width, size.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);
                    }
                    g.DrawImageUnscaled(bitmap, imageBounds);
                    return;
                }
            }
            g.DrawImage(image, imageBounds, 0, 0, size.Width, size.Height, GraphicsUnit.Pixel, disabledImageAttr);
        }
    }

    /// <summary>
    /// 灰色位图。
    /// </summary>
    /// <param name="bitmap"></param>
    /// <param name="iType">0平均值法,1最大值法,其它加权平均值法 </param>
    public static void GrayImage(Bitmap image, int iType)
    {
        if (image == null)
        {
            throw new ArgumentNullException("image");
        }
        int Height = image.Height;
        int Width = image.Width;
        Color pixel;
        for (int x = 0; x < Width; x++)
        {
            for (int y = 0; y < Height; y++)
            {
                pixel = image.GetPixel(x, y);
                int r, g, b, result = 0;
                r = pixel.R;
                g = pixel.G;
                b = pixel.B;
                switch (iType)
                {
                    case 0://平均值法  
                        result = ((r + g + b) / 3);
                        break;
                    case 1://最大值法  
                        result = r > g ? r : g;
                        result = result > b ? result : b;
                        break;
                    default://加权平均值法  
                        result = ((int)(0.7 * r) + (int)(0.2 * g) + (int)(0.1 * b));
                        break;
                }
                image.SetPixel(x, y, Color.FromArgb(result, result, result));
            }
        }
    }
}

posted on 2010-04-21 15:23  yewude15  阅读(203)  评论(0)    收藏  举报