[C # Drawing] Recolor an image

Source: Internet
Author: User

Overview:

Recolor is the process of adjusting the color of an image. Recolor includes changing one color to another, adjusting the brightness of one color relative to another, adjusting the brightness or contrast of all colors, and increasing the grayscale of the color.

How to: Transform monochrome using a color matrix

GDI + provides the image and Bitmap classes for storing and manipulating images . The Image and Bitmap objects store the color of each pixel with a 32-digit number: 8 bits of red, green, blue, and Alpha. The values for these four components are 0 to 255, where 0 means no brightness, and 255 indicates the maximum brightness. The alpha component specifies the transparency of the color: 0 is fully transparent and 255 is completely opaque.

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 to achieve maximum brightness.

Another convention for representing colors is to use the number 1 to indicate the maximum brightness. By using this convention, the colors described in the previous paragraph can be represented by vectors (0, 1, 0, 1). GDI + follows the convention of using 1 as the maximum brightness when performing color transformations.

You can apply a linear transformation (rotation, scaling, and so on) to a color vector by multiplying these color vectors with a 4x4 matrix. However, you cannot use a 4x4 matrix for panning (non-linear). 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 and transitions. A transformation consisting of a linear transformation followed by a translation is called an affine transformation.

For example, suppose you want to start with a color (0.2, 0.0, 0.4, 1.0) and apply the following transform:

1. Multiply the red component by 2.

2. Add 0.2 to the red, green, and blue components.

The following matrix multiplication will perform this pair of transformations in the order listed.

The elements of the color matrix are indexed in the order of the preceding columns (starting at 0). For example, the third column of the fifth row of Matrix M is represented by m.

The 5x5 unit matrix (shown in the illustration below) is 1 on the diagonal and 0 anywhere else. If you multiply the color vector by the unit matrix, the color vector does not change. A convenient way to form a color transformation matrix is to start with the unit matrix and then make minor changes to produce the desired transformation.

Examples of programs:

The following example takes an image that uses one color (0.2, 0.0, 0.4, 1.0) and applies the transformation described in the previous paragraph. The following illustration shows the original image on the left and the transformed image on the right.

The code in the following example uses the following steps to recolor:

1. Initializes the ColorMatrix object.

2. Creates a ImageAttributes object and passes the ColorMatrix object to the SetColorMatrix method of the ImageAttributes object.

3. The DrawImage method that passes the ImageAttributes object to the Graphics object.

//get the original and corresponding propertiesImage image =NewBitmap ("inputcolor.bmp");intwidth =image. Width;intHeight =image. Height;//Initialize the color matrixfloat[] colormatrixelements = {    New float[] {2,0,0,0,0},    New float[] {0,1,0,0,0},    New float[] {0,0,1,0,0},    New float[] {0,0,0,1,0},    New float[] {0.2f,0.2f,0.2f,0,1}}; ColorMatrix ColorMatrix=NewColorMatrix (colormatrixelements);//Constructing the image transform toolImageAttributes ImageAttributes =NewImageAttributes (); Imageattributes.setcolormatrix (ColorMatrix, Colormatrixflag.default, COLORADJUSTTYPE.BITMAP);//DrawingE.graphics.drawimage (Image,Ten,Ten); E.graphics.drawimage (Image,NewRectangle ( -,Ten, width, height),0,0, width, height, graphicsunit,pixel, imageattributes);
How to: Convert Image colors

Panning refers to adding values in one or more of these four color components. The following table shows the color matrix items that represent panning.

 The component to translate Matrix Item Red   Green   Blue   Alpha  

Examples of programs:

The following example constructs an Image object from the ColorBars.bmp file. The code then increases the red component of each pixel in the image by 0.75. The original image is drawn next to the transformed image.
The following illustration shows the original image on the left and the transformed image on the right.

HTTPS://MSDN.MICROSOFT.COM/ZH-CN/LIBRARY/A7XW19WH (v=vs.110). aspx

Http://www.cnblogs.com/JohnShao/archive/2011/06/08/2075070.html

Http://www.cnblogs.com/yiyiruohan/archive/2010/08/24/1807533.html

[C # Drawing] Recolor an image

Related Keywords:
Related Article E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD