[GDI +] ColorMatrix Color Matrix

Source: Internet
Author: User
First of all, the Assembly of the head to give two articles of the friendly reply, as well as netizens fisherman together to discuss ColorMatrix Topic Express Thanks!

ColorMatrix (color matrix) class is located in the System.Drawing.Imaging namespace first 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,0,1,0,0},
New float[]{0,0,0,0,1,0},
New float[]{0,0,0,0,0,1}});
The matrix coefficients form a 5x5 linear transformation, which is used to convert the ARGB monochrome values. For example, the ARGB vector is represented as Alpha, red (red), green (green), Blue (blue), and W, where W is always 1.

So what's W? Why is it defined as a 5x5 matrix?

After looking up MSDN found this article, "using the color matrix to transform monochrome" in this way:

GDI + provides image and Bitmap classes for storing and manipulating images. The Image and Bitmap objects store the color of each pixel as 32 digits: Red, green, blue, and alpha each account for 8 digits. The values for these four components are 0 to 255, where 0 indicates no brightness, and 255 represents the maximum brightness. The alpha component specifies the transparency of the color: 0 indicates full transparency, and 255 indicates total opacity.

Color vectors are in 4-tuple form (red, green, blue, alpha). For example, a color vector (0, 255, 0, 255) represents an opaque color that has no red and blue but green reaches the maximum brightness.

Another practice for representing colors is to use the number 1 to indicate that the brightness is maximized. Using this practice, the colors described in the previous paragraph will be expressed in (0, 1, 0, 1). GDI + uses a 1 representation of the maximum brightness in a color transformation.

You can use the 4x4 matrix to multiply these color vectors to apply the linear transformation (rotation and scaling, etc.) to the color vector. However, you cannot use the 4x4 matrix for translation (nonlinearity). If you add a virtual 5th coordinate (for example, number 1) to each color vector, you can use the 5x5 matrix to apply any combination of linear transformations to the peace shift. A transformation consisting of a linear transformation followed by a translation is called an affine transformation.

I think it can be understood as a virtual vector


Transparent processing of images under Visual C # This is a good piece of advice to read.
Address: http://tech.ccidnet.com/pub/article/c294_a36258_p1.html

The color matrix can be applied to the image using the Imageattributes.setcolormatrix method



[C #]

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 the code after InitializeComponent call
//
}
<summary>
Clean up any 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 to Designer support-do not modify
The contents is 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 is 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.