Several attributes of graphics.
Today I'm going to talk about some of the attributes of graphics in DrawImage.
Graphics is GDI + inside the Montana, can be used to draw lines, draw a rectangle, and even can be used to draw a variety of materials. Through a variety of pen,brush to achieve. The specific use of the method is all want to use GDI + students base, I do not elaborate, the specific reference can be msdn:http://msdn.microsoft.com/en-us/library/haxsc50a (vs.80). aspx. I mainly talk about 2 attributes that you don't really notice.
A.graphics.compositingmode
This is an enumeration property that can fetch 2 kinds of values, one is Sourceover, the other is sourcecopy. This defines how the graphics will synthesize the current color and background. If it is sourcecopy, then the color is not combined with the current background. If it is sourceover, then the background color will be mixed with the current color, the algorithm is as follows:
Display color = source color xalpha/255 + background color x (255-alpha)/255
The transparent component of the new color is 255, which is opaque. Let's take a look at the following code:
private int compositecolor (int color, int alpha, int backgroudcolor)
{
//Display color = source color xalpha/255 + background color x (255-alpha)/255
Return color * alpha/255 + backgroudcolor * (255-alpha)/ 255;
}
private void Draw (object sender, EventArgs e)
{
this. BackColor = Color.FromArgb (255, 255, 255);
Graphics g = this. CreateGraphics ();
G.compositingmode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
G.fillrectangle (New SolidBrush (Color.FromArgb (127, 255, 0, 0)), new Rectangle (0, 0, 200, 200));
G.compositingmode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
G.fillrectangle (New SolidBrush (Color.FromArgb (127, 255, 0, 0)), new Rectangle (200, 0, 200, 200));
G.compositingmode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
G.fillrEctangle (New SolidBrush (Color.FromArgb
255,
Compositecolor (255, 127, 255),
Compositecolor (0, 127, 255),
Compositecolor (0, 127, 255))
, new Rec Tangle (0, 200, 200, 200));
G.dispose ();
}
The 1th color block and the 2nd color block are mixed and not mixed, if we do not mix the way we want to get the effect of mixing, then we should use the third color block. We can see the result very clearly from the image below.