C # graph Programming

Source: Internet
Author: User

 

 

 

Like java, C # provides a rich set of class libraries, methods, and events for developers to use. C # It also introduces GDI +, which evolved from GDI and has more powerful functions and simplifies than GDI.ProgramMember programming work. Therefore, developers can easily develop applications with powerful graphic and image functions. In this article, I will introduce the basic knowledge of graph programming in C # through some examples.

 

Simple Example:

 

First, let's start with an example. The following is the simplest example:

 

Using system;

Using system. Windows. forms;

Using system. drawing;

 

Public class Hello: FORM {

Public Hello (){

This. Paint + = new painteventhandler (f1_paint );

}

 

Private void f1_paint (Object sender, painteventargs e ){

Graphics G = E. graphics;

G. drawstring ("Hello, C #! ", New font (" verdana ", 20 ),

New solidbrush (color. Tomato), 40, 40 );

G. drawrectangle (new pen (color. Pink, 3), 150,100 );

 

}

Public static void main (){

Application. Run (New Hello ());

}

 

}

 

In the above example, we used a method: drawstring (), which contains five parameters. At the same time, we found that before using the drawstring () method, we first created a graphics type object G = E. graphics, which indicates that before using any method of the graphic class, we must first create an instantiated object of this class. After the drawstring () method, we used the drawrectangle () method. In fact, we can also use other methods to draw an elliptical or polygon. The first instance is quite easy to understand, isn't it?

 

Measure unit of the transformed graph:

 

In Graphic programming, the default Graph measurement unit is pixel. However, you can modify the unit of measurement of a graph by modifying the pageunit attribute, such as inches or millimeters. The implementation method is as follows:

 

Graphics G = E. graphics;

G. pageunit = graphicsunit. inch

 

Operation color selection dialog box:

 

In practical use, especially in the graphic image programming process, we may often encounter the color selection dialog box (and the font selection dialog box to be mentioned below ). In the color selection dialog box, you can select the expected color and custom color. Before using the color selection dialog box, we must first create a colordialog type object:

 

Colordialog Cd = new colordialog ();

 

Then, we can use the showdialog () method to display the color selection dialog box. Then, you can call the user's color selection to perform related graphic operations.

 

Below, I will give you an instance. There is a button and a text box in this instance. You can click the button to bring up the color selection dialog box. You can set the background color of the text box based on your color selection.

 

Using system;

Using system. drawing;

Using system. Windows. forms;

 

Public class CLR: FORM {

Button b1 = new button ();

Textbox TB = new Textbox ();

Colordialog CLG = new colordialog ();

 

Public CLR (){

B1.click + = new eventhandler (b1_click );

B1.text = "select color ";

TB. Location = new point (50, 50 );

This. Controls. Add (B1 );

This. Controls. Add (TB );

}

 

Public void bid click (Object sender, eventargs e ){

CLG. showdialog ();

TB. backcolor = CLG. color;

}

 

Public static void main (){

Application. Run (New CLR ());

}

 

}

 

 

Operation font selection dialog box:

 

Font is an important part of Graphic programming. By setting different fonts, you can achieve different visual effects in the program. Similar to the above color selection dialog box, you can easily create a font selection dialog box and use it to allow users to select the desired font.

 

The following example is also provided. This example is similar to the above example. The font selection dialog box replaces the original color selection dialog box, And the font of the text box is set based on the user's font selection.

 

Using system;

Using system. drawing;

Using system. Windows. forms;

 

Public class fonts: FORM {

Button b1 = new button ();

Textbox TB = new Textbox ();

Fontdialog flg = new fontdialog ();

 

Public fonts (){

B1.click + = new eventhandler (b1_click );

B1.text = "select font ";

TB. Location = new point (50, 50 );

 

This. Controls. Add (B1 );

This. Controls. Add (TB );

}

 

 

Public void bid click (Object sender, eventargs e ){

CLG. showdialog ();

TB. fontname = flg. Font;

}

 

 

Public static void main (){

Application. Run (new fonts ());

}

 

}

 

Use System. Drawing. drawing2d namespace:

 

If you have some experience in graphic image programming, you must know the concepts of paint brushes and brushes. They are widely used in graphic programming. The system. Drawing. drawing2d namespace provides quite powerful functions, allowing developers to easily operate on paint brushes and painting objects. For example, you can set the dashstyle attributes of the paint brush (including dash, dashdot, and solid) to determine the straight line style. Similarly, you can easily modify the appearance of a filled area by using a collection of brushes such as solidbrush, hatchbrush, and gradientbrush. For example, you can use a solidbrush to fill a rectangular area with multiple straight lines of different thicknesses. So when can we use paint brushes and paint brushes? As in the preceding example, a graphic profile (using the drawxxx () method) is usually implemented using a paint brush object, and a filled area (using the fillxxx () method) it is implemented by the painter object.

 

Use a paint brush object:

 

In the following example, we use the system. Drawing. drawing2d namespace. In this example, we use a paint brush to draw straight lines, ovans, pie charts, polygon and other images in different styles.

 

Using system;

Using system. Windows. forms;

Using system. drawing;

Using system. Drawing. drawing2d;

 

Public class drawgra: FORM {

Public drawgra (){

This. Text = "Use the paint brush example ";

This. size = newsize (450,400 );

This. Paint + = new painteventhandler (draw_graphics );

}

 

Public void draw_graphics (Object sender, painteventargs e ){

Graphics G = E. graphics;

Pen penline = new pen (color. Red, 5 );

Pen penellipse = new pen (color. Blue, 5 );

Pen penpie = new pen (color. Tomato, 3 );

Pen penpolygon = new pen (color. Maroon, 4 );

 

/* Dashstyle includes dash, dashdot, dashdotdot, dot, and solid */

// Draw a straight line in the dash Style

 

Penline. dashstyle = dashstyle. Dash;

G. drawline (penline, 50, 50, 100,200 );

 

// Draw an ellipse in the dashdotdot Style

 

Penellipse. dashstyle = dashstyle. dashdotdot;

G. drawellipse (penellipse, 15, 15, 50, 50 );

 

 

// Draw a pie chart in the dot Style

Penpie. dashstyle = dashstyle. Dot;

G. drawpie (penpie, 90,80, 140,40, 120,100 );

 

// Draw a polygon in solid Style

 

G. drawpolygon (penpolygon, new point [] {

New Points (30,140 ),

New Points (270,250 ),

New Points (110,240 ),

New Points (200,170 ),

New Points (70,350 ),

New Point (50,200 )});

 

}

 

 

Public static void main (){

Application. Run (New drawgra ());

}

 

} Image painter objects:

 

A painter uses a specific color, pattern, or image to fill an area. There are a total of four types of paint brushes: solidbrush (default paint brush), hatchbrush, gradientbrush, and texturedbrush. Next, we will introduce the instances separately.

 

1. Use solidbrush:

 

Using system;

Using system. Windows. forms;

Using system. drawing;

Using system. Drawing. drawing2d;

 

Public class solidbru: FORM {

Public solidbru (){

This. Text = "use solidbrush example ";

This. Paint + = new painteventhandler (fill_graph );

}

 

 

Public void fill_graph (Object sender, painteventargs e ){

Graphics G = E. graphics;

// Create a solidbrush and use it to fill a rectangular area

Solidbrush sb = new solidbrush (color. Pink );

G. fillrectangle (SB, 50, 50, 150,150 );

 

}

 

 

Public static void main (){

Application. Run (New solidbru ());

}

 

}

 

 

2. Use hatchbrush:

 

Using system;

Using system. Windows. forms;

Using system. drawing;

Using system. Drawing. drawing2d;

Public class hatchbru: FORM {

Public hatchbru (){

This. Text = "example of using hatchbrush ";

This. Paint + = new painteventhandler (fill_graph );

}

 

Public void fill_graph (Object sender, painteventargs e ){

Graphics G = E. graphics;

// Create a hatchbrush and use it to fill a rectangular area

/* The hatchstyle of the paint brush includes diagonalcross and,

Forwarddiagonal, horizontal, vertical, solid, and other different styles */

Hatchstyle HS = hatchstyle. Cross;

Hatchbrush sb = new hatchbrush (HS, color. Blue, color. Red );

G. fillrectangle (SB, 50, 50, 150,150 );

}

 

 

Public static void main (){

Application. Run (New hatchbru ());

}

 

}

3. Use gradientbrush:

 

Gradientbrush can be divided into lineargradientbrush and pathgradientbrush. From their names, we can know that the former is a linear gradient, while the latter is a path gradient, therefore, it can create more complex and perfect results. The following is an example:

 

1) Use lineargradientbrush:

 

Using system;

Using system. Windows. forms;

Using system. drawing;

Using system. Drawing. drawing2d;

 

Public class lineargradientbru: FORM {

Public lineargradientbru (){

This. Text = "example of using lineargradientbrush ";

This. Paint + = new painteventhandler (fill_graph );

}

 

 

Public void fill_graph (Object sender, painteventargs e ){

Rectangle r = new rectangle (500,300,100,100 );

Lineargradientbrush lB = new lineargradientbrush (R, color. Red, color. Yellow,

Lineargradientmode. backwarddiagonal );

E. Graphics. fillrectangle (LB, R );

}

 

 

Public static void main (){

Application. Run (New lineargradientbru ());

}

}

 

 

The resulting image is as follows:

 

 

 

2) use pathgradientbrush:

Using system;

Using system. Windows. forms;

Using system. drawing;

Using system. Drawing. drawing2d;

 

Public class pathgradientbru: FORM {

Public pathgradientbru (){

This. Text = "example of using pathgradientbrush ";

This. Paint + = new painteventhandler (fill_graph );

}

 

Public void fill_graph (Object sender, painteventargs e ){

E. Graphics. textrenderinghint = textrenderinghint. antialiased;

E. Graphics. fillrectangle (backgroundbrush, clientrectangle );

E. Graphics. fillrectangle (New solidbrush (color. fromargb (180, color. White), clientrectangle );

 

// Set a path first

Graphicspath Path = new graphicspath (new point [] {

New Points (40,140 ),

New Points (275,200 ),

New Points (105,225 ),

New Points (190,300 ),

New Points (50,350 ),

New Points (20,180 ),

}, New byte [] {

 

(Byte) pathpointtype. Start,

(Byte) pathpointtype. bezr,

(Byte) pathpointtype. bezr,

(Byte) pathpointtype. bezr,

(Byte) pathpointtype. Line,

(Byte) pathpointtype. Line,

});

 

// Create a pathgradientbrush

 

Pathgradientbrush pgb = new pathgradientbrush (PATH );

 

// Set the surrounding color of the paint brush

 

Pgb. surroundcolors = new color [] {

Color. Green,

Color. Yellow,

Color. Red,

Color. Blue,

Color. Orange,

Color. White,

 

};

 

// Fill the image with a paint brush

E. Graphics. fillpath (pgb, PATH );

}

 

 

Public static void main (){

Application. Run (New pathgradientbru ());

}

 

}

 

The resulting image is as follows:

 

 

4. Use texturedbrush:

 

Using system;

Using system. Windows. forms;

Using system. drawing;

Using system. Drawing. drawing2d;

 

Public class texturedbru: FORM {

Brush bgbrush;

 

Public texturedbru (){

 

// Create an image for filling the elliptical background

Image bgimage = new Bitmap ("dotnet.gif ");

Bgbrush = new texturebrush (bgimage );

This. Paint + = new painteventhandler (text_bru );

 

}

 

Public void text_bru (Object sender, painteventargs e ){

Graphics G = E. graphics;

G. fillellipse (bgbrush, 50, 50, 500,300 );

 

}

 

Public static void main (){

Application. Run (New texturedbru ());

}

}

 

Images used:

 

Images are often used in graphic programming, and their functions on user interfaces are also very obvious. In the previous programming process, the image operation details were quite tedious and error-prone. Now, under GDI +, you can use the C # language to easily complete the image programming you want.

 

You can program images by following these steps.

 

1. Create a bitmap object as follows:

 

Image IMG = new Bitmap ("image.bmp ");

 

2. Use the above object in the drawimage () method:

 

G. drawimage (IMG, 20,20, 100,90 );

 

As for examples of using images, I will not introduce them here. I believe that you can easily complete an example of using images.

 

Summary:

 

In this articleArticleI mainly used two very core namespaces: system. Drawing and system. Drawing. drawing2d. With them, we can easily call the methods and attributes in it to implement the tasks that previously had to be completed at a great cost for Graphic programming, this is not to mention the advantages that GDI + brings to us. Therefore, if you have mastered GDI +, I believe that your graphic programming capability will be improved.

Related Article

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.