Home  >  Article  >  Backend Development  >  C# GDI+ Programming (3)

C# GDI+ Programming (3)

高洛峰
高洛峰Original
2016-12-17 10:14:241584browse

Color’s Alpha value

Color can also set the Alpha value, which is the transparency. Such as Color.FromArgb(120,255,255,255). FromArgb has four parameters, the first one specifies the Alpha value.
The last three are color values ​​RGB.
The alpha value range is 0~255, 0 means completely transparent, and 255 means opaque.
Look at an example of a semi-transparent brush:
                                                                      using use using use using using             through use through out ’s ’ s through out using off ‐   ‐ ‐‐‐‐‐ and
to be a semi-transparent brush.
Rectangle rect = new Rectangle(0, 0, 100 , 100); Path.AddRectangle(rect);径 // Create road drawing brush
PATHGRADIENTBRUSH BRUSH = New Pathgradientbrush (PATH);
// central point color
brush.centercolor = color.fromargb (120,255,255); ) The color on the upper u Brush.Surroundcolors = new Color[] { Color.FromArgb(120,0,0,0)};

If the color of the brush If the Alpha (transparent) value is not set, the displayed string cannot be seen.

Anti-aliasing

Eliminating the aliasing of lines is accomplished by setting the SmoothingMode attribute member in the Graphics class, which is an enumeration type.

Example statement to set anti-aliasing: e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

After eliminating aliasing, the lines look much smoother.

The corresponding code is:

                                                                                                                                                                                                                                                                                  lipse(pen, rect1);

//Eliminate aliasing

            e.Graphics.SmoothingMode =  SmoothingMode.AntiAlias;

                                                    using use use with using using             through using ’ ’ s ’ through using ’s way through ’ through ‐ to ‐‐ ‐‐‐ , � ;

                  e.Graphics.DrawEllipse(pen, rect2) ;

The SmoothingMode enumeration also has some other members, which can set different levels of anti-aliasing or not.

Text anti-aliasing
To anti-alias the displayed text, just set TextRenderingHint.
Sample code:
private void formPaint(Object sender, PaintEventArgs e)
ing("ABCDEFGHIJKL", new Font("宋体", 15f), brush, 0, 20);
                                                                                     new Font("宋体", 15f), brush, 0, 50);
}

area

It’s best to understand these functions, because C# can also call these APIs. And many of the concepts there are similar. For example, window handle, area handle.

For example, the GetHrgn function in the Region class can get the region handle.

Also let’s talk about the relationship between area and path, so that you can better understand what the area is. The relationship between area and path is like the relationship between filled graphics and graphics.

You can use paths and rectangles to create a area. Through the Region's constructor.

The following example creates two areas with rectangles and fills them. V Private void Formpaaint on2 = New Region (New Rectangle (50,50,100,100));

                                                                                                                       Graphics.Fillregion (brush1, region1);

// Create a painting brush 2, and fill the area 2

solidbrush brush2 = new solidbrush (color.fromargb (125, 0, 255));

E. Graphics. FillRegion (brush2 , region2);

        }


In addition, two regions can also be merged. Merging regions has also been discussed in the 95th function CombineRgn. You can refer to that function.

CombineRgn The last parameter nCombineMode of the API function specifies how to merge two regions, so the merge in the Region class should also have a similar function.

Indicate how to merge, but the Region class is not specified with parameters. Region directly calls different functions.

For example, the Region.Intersect function is RGN_AND, and Region.Union is the RGN_OR method of merging. . region1.Exclude is RGN_DIFF,

Region. ;>>>>>>>>>>>>>>>>>From API CombineRgn function>>>>>> ;>>>>>>>>>>>>>>>>>>>>>>>>> ;>>>>>>>>>>>>>>>>>>>

RGN_AND The overlapping portion of the two regions

RGN_OR Combine two regions

RGN_DIFF The non-overlapping part of hSrcRgn1

RGN_XOR The non-overlapping part of hSrcRgn1 and hSrcRgn2

<<<<<<<<<<<<<<<<<<<<<<<< ;<<<<<<<<<<<<<<<<<<<<<<<< ;<<<<<<<<<<<<<<<<<<<<<<<< ;<<<<<<<<<<<<<<<<<<<<<<<< ;<<<

There is also a function Complement in the Region class. The area obtained by merging with this function is the non-overlapping part of area 2.

Look at the example:

private void formPaint(Object sender, PaintEventArgs e)
0,0,100,100));
  Region region2 = New Region (New Rectangle (50,50,100,100));
// Use the XOR function merging area to obtain the unpacking parts of the two areas.
        region1.
          }

Settings window Display area:

Just specify the Region attribute member in the Form class, for example, display a circular window:

       public Form1()

                                  InitializeComponent();                         // Create a circular path

            GraphicsPath path = new GraphicsPath ( );

           this.Region  =  region;

                                     

Region.IsVisible determines whether a point (or rectangle) is within the region.
Look at the example. In the example, a circular area is created. As soon as the mouse enters the circular area, the area will display red. P PARIC PARTIAL CLASS FORM1: Form
{
// Public Solidbrush Norbrush = New Solidbrush (COLOR.GREEN); c solidBrush Hovbrush = New solidbrush (color.red);
// indicate whether the mouse is in the region
public Bool Hovflag = False; ecomponent ();
// Create a circular path
                  GraphicsPath   path =   new                                                                                                                         using   using using using using         using using ‐                       through       through through off ‐         through ’ ‐ ‐ ‐‐ ‐‐ ‐ ‐ ‐ ‐ ‐ ‐ ‐ ‐ ‐ ‐                                 to  new Region(path);
件 // Add event processing

This.paint+= Formpaaint;
// Mouse Mobile Event
This.Mousemove+= Frommousemove;
}}


targs e)
{
if (hovflag)
{
                      e.Graphics.FillRegion(hovBrush, region);                                                                                                                  . gion(norBrush, region);
                                                                                                                    gion; , MouseEventArgs e)
                                                        Graphics                                                                                                                                    Graphics                                                                                     . hovFlag != true)
                                                                                                                                  hovFlag                                                     . FillRegion (HOVBRUSH, Region);
}
// The mouse leaves the area
else if (region.isvisible (e.x, e.y)! = True && Hovflag) {
hovflag = false;
.FillRegion (norbrush, region) ;
                                                                                                                                                                   ​ 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# GDI+ Programming (4)Next article:C# GDI+ Programming (4)