Create an ImageAttributes object and set its color matrix : ImageAttributes « 2D Graphics « C# / C Sharp






Create an ImageAttributes object and set its color matrix

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {

    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        Bitmap bmp = new Bitmap("rama.jpg");
        g.FillRectangle(Brushes.White, this.ClientRectangle);

        for (int i = 1; i <= 7; ++i) {
            Rectangle r = new Rectangle(i * 40 - 15, 0, 15, this.ClientRectangle.Height);
            g.FillRectangle(Brushes.Gray, r);
        }

        float[][] matrixItems = {
                            new float[] {1, 0, 0, 03, 0},
                            new float[] {0, 1, 0, 0.1f, 0},
                            new float[] {0, 0, 1, 0, 0},
                            new float[] {0, 0, 0, 0.6f, 0}, 
                            new float[] {0, 0, 0, 0, 1}};
        ColorMatrix colorMatrix = new ColorMatrix(matrixItems);

        ImageAttributes imageAtt = new ImageAttributes();
        imageAtt.SetColorMatrix(
            colorMatrix,
            ColorMatrixFlag.Default,
            ColorAdjustType.Bitmap);

        g.DrawImage(
            bmp,
            this.ClientRectangle,  // destination rectangle
            0.0f,             // source rectangle x 
            0.0f,             // source rectangle y
            bmp.Width,        // source rectangle width
            bmp.Height,       // source rectangle height
            GraphicsUnit.Pixel,
            imageAtt);

        imageAtt.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

 








Related examples in the same category