[GDI +] ColorMatrix Color Matrix

xiaoxiao2021-03-06  96

First give a friendly reply to the two articles in the assembly head, and some netizens Fisherman discussed the topic of ColorMatrix expressed gratitude!

ColorMatrix class is located in System.drawing.Imaging namespace first look at the code below

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 coefficient constitutes a linear conversion of 5x5 for converting the monochrome value of Argb. For example, the Argb vector is represented as alpha, RED (red), Green (green), Blue (blue), and W where W is always 1.

So what is W? Why is it defined as 5x5 matrix? Looking for MSDN found that this article "Transforms using color matrices": GDI provides image and bitmap classes for storing and operating images. Image and Bitmap objects store each pixel color to 32 digits: red, green, blue and alpha each account for 8 bits. The values ​​of these four components are 0 to 255, of which 0 indicates that there is no brightness, 255 represents the maximum brightness. The Alpha component specifies the transparency of the color: 0 indicates full transparency, 255 is completely opaque.

Color vector uses 4 yuan group form (red, green, blue, alpha). For example, color vectors (0, 255, 0, 255) indicate an opaque color without red and blue but green to maximum brightness.

Another convention indicating that the color is that the brightness is maximized with the number 1. With this convention, the colors described in the previous paragraph will be represented by (0, 1, 0, 1). GDI uses 1 to represent the maximum brightness at 1 when performing color transformation.

Linear transformation (rotation and zooming, etc.) can be applied to color vectors by multiplying a 4 × 4 matrix by these color vectors. However, you cannot use a 4 × 4 matrix to pan (nonlinear). If you add a virtual fifth coordinate (for example, numbers 1) in each color vector, a linear transformation and transformation of any combination form can be applied using a 5 × 5 matrix. The rear heel translation of the linear transformation is called an affine transformation. I think it is possible to understand the transparent processing of virtual vectors to implement images in Visual C #, write a good suggestion to see the address: http://tech.ccidnet.com/pub/Article/c294_a36258_p1.html color matrix application to the image ImageAttributes.SetColormatrix method can be used

[C #]

Using system;

Using system.drawing;

Using system.drawing.image;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

Namespace grayshear

{

///

/// summary description for Form1.

///

Public class form1: system.windows.forms.form {

///

/// Required Designer Variable.

///

Private system.componentmodel.Container Components = NULL;

Public Form1 ()

{

//

// Required for Windows Form Designer Support

//

InitializationComponent ();

//

// Todo: Add Any Constructionor Code After InitializationComponent Call

//

}

///

/// Clean Up Any Resources Being Used.

///

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows Form Designer Generated Code

///

/// Required Method for Designer Support - Do Not Modify

/// The contents of this method with the code editor.

///

Private vidinitiRizeComponent ()

{

//

// 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);

}

#ndregion

///

/// The main entry point for the application.

///

[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 Color 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, Img.Width, Img.Height), 0, 0, Img.Width, Img.Height, GraphicsUnit.Pixel, IA);

g.dispose ();

THIS.BACKGROUNDIMAGE = BM;

}

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-100160.html

New Post(0)