Several common problems in GDI + (10)

Source: Internet
Author: User

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.

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.