[GDI +] ColorMatrix color matrix

Source: Internet
Author: User
Tags transparent image

The ColorMatrix (color matrix) class is located in the System. Drawing. Imaging namespace.
Let's take a look at the following code:

ColorMatrix cm = new ColorMatrix (new float [] [] {new
Float [] {0.5f, 0.5f, 0.5f, 0, 0 },
New float [] {0.5f, 0.5f, 0.5f, 0, 0 },
New
Float [] {0.5f, 0.5f, 0.5f, 0, 0 },
New float [] {0, 0, 1, 0, 0 },
New
Float [] {0, 0, 0, 1, 0 },
New float [] {0, 0, 0, 0, 1 }});
Matrix Coefficients form a linear conversion of 5x5 for conversion
The monochrome value of ARGB. For example, the ARGB vectors are Alpha, Red, Green, Blue, and W. Here, W is always
1.

So what is w? Why is the matrix 5x5 defined?

This article was found on MSDN.
In "using color matrix to transform a monochrome", we will talk about the following:

GDI + provides Image and Bitmap classes for storing and operating images. Image and Bitmap
The object stores the color of each pixel as 32 bits: red, green, blue, and alpha each occupy 8 bits. The values of these four components are from 0 to 255, where 0 indicates no brightness, 255
Maximum brightness. The alpha component specifies the transparency of the color. 0 indicates full transparency, and 255 indicates full opacity.

Color vector 4
The format of tuples (red, green, blue, and alpha ). For example, color vector (0,255, 0,255)
Indicates an opaque color with no red or blue but green reaching the maximum brightness.

Another convention for color is to use number 1 to indicate that the brightness reaches the maximum. Using this convention, the color described in the previous section will be used
(0, 1, 0, 1) indicates. GDI + uses 1 to represent the maximum brightness during color conversion.

You can use 4× 4
The matrix is multiplied by these color vectors to apply linear transformations (such as rotation and scaling) to color vectors. However, you cannot use a 4x4 matrix for translation (non-linear ). If you add another virtual 5th in each color vector
Coordinates (for example, number 1) can be 5 × 5
The matrix applies linear transformations and shifts in any combination form. A transformation composed of linear transformations followed by translation is called an affine transformation.

I think it can be understood as a virtual vector.

In the Visual
C # implement transparent image processing. This article is well written and recommended.
Address: http://tech.ccidnet.com/pub/article/c294_a36258_p1.html

You can use ImageAttributes. SetColorMatrix to apply the color matrix to an image.
Method

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace GrayShear
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(296, 269);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter="Image files (*.BMP, *.JPG, *.GIF)|*.bmp;*.jpg;*.gif";
if(dlg.ShowDialog()==DialogResult.OK)
{
Image img = Image.FromFile(dlg.FileName);
Bitmap bm = new Bitmap(img.Width,img.Height);
Graphics g = Graphics.FromImage(bm);


ColorMatrix cm = new ColorMatrix(new float[][]{ new float[]{0.5f,0.5f,0.5f,0,0},
new float[]{0.5f,0.5f,0.5f,0,0},
new float[]{0.5f,0.5f,0.5f,0,0},
new float[]{0,0,0,1,0,0},
new float[]{0,0,0,0,1,0},
new float[]{0,0,0,0,0,1}});

/*
//Gilles Khouzams colour corrected grayscale shear
ColorMatrix cm = new ColorMatrix(new float[][]{ new float[]{0.3f,0.3f,0.3f,0,0},
new float[]{0.59f,0.59f,0.59f,0,0},
new float[]{0.11f,0.11f,0.11f,0,0},
new float[]{0,0,0,1,0,0},
new float[]{0,0,0,0,1,0},
new float[]{0,0,0,0,0,1}});
*/

ImageAttributes ia = new ImageAttributes();
ia.SetColorMatrix(cm);
g.DrawImage(img,new Rectangle(0,0,img.Width,img.Height),0,0,img.Width,img.Height,GraphicsUnit.Pixel,ia);
g.Dispose();
this.BackgroundImage=bm;
}
}
}
}

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.