C # graphics operation

xiaoxiao2021-03-05  65

Like Java, C # provides a complete set of quite rich libraries, methods, and events for developers. C # also introduces GDI , which is evolved from GDI, with more powerful functions than GDI and simplifies programmers. So the developer uses these, it can easily develop applications with powerful graphic image functions. This article, the author will introduce the basic knowledge of graphic programming in C # through some instances.

Simple instance:

First, let us start from example, the following is the simplest instance:

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), 20, 20, 150, 100);

}

Public static void main () {

Application.run (New Hello ());

}

}

In the above example, we used a method: DrawString (), which has 5 parameters. At the same time, we found that before using the DrawString () method, we first created a graphics type object g = E.Graphics, which means that we must create an instantiated object of such a class before using any graphic class. . After the DrawString () method, we use the DrawRectangle () method, in fact, we can also use other methods to draw an elliptical or polygon, and the like. The first instance is still quite easy to understand, isn't it?

Multiple unit of transform graphics:

In graphic programming, the default graphic metric unit is pixel. However, you can modify the measurement unit of the graphic by modifying the PageUnit property, which can be inch or millimeters. The implementation method is as follows:

Graphics g = E.Graphics;

g.pageunit = graphicsUnit.inch

Operation Color Select dialog:

In actual use, in particular graphic image programming, we may often touch the color selection dialog (and the font selection dialogs you will mention). With the Color Select dialog, we allow users to select the system's predetermined colors and user-defined colors. Before using the Color Select dialog box, we must first create a colorDialog type object:

ColorDialog CD = New ColorDialog ();

We can then use the showdialog () method to display the color selection dialog. Thereafter, the related graphic operation can be performed by calling the user's color selection.

Here, I will give you an example. There is a button and a text box in this example. You can adjust the color selection dialog box by clicking the button. You can set the background color of the text box according to the user's 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 b1_click (Object sender, eventargs e) {

CLG.ShowDialog ();

Tb.backcolor = clg.color;

}

Public static void main () {

Application.run (New CLR ());

}

}

Operation Font Selection Dialog:

The font is an important part of graphical programming. By setting different fonts, you can achieve different visual effects in the program. And the above color selection dialog is not much different, you can easily create a font selection dialog box and make users choose the font they need.

A example is given below, this example is similar to the instance of the above, but the font selection dialog is used instead of the original color selection dialog, and finally the font of the text box is set according to the user's font.

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 b1_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 graphic image programming experience, then you must know the concept of brush and painting brush. They have a very wide application in graphical programming. System.drawing.drawing2D Namespace provides considerable power to make developers easy to operate brush and painting objects. For example, you can determine the style of the straight line by setting the DashStyle property of the brush (with Dash, Dashdot, Solid, etc.). Similarly, by using Solidbrush, Hatchbrush, GradientBrush, you can easily modify the appearance of the filled area. For example, you can use Solidbrush to fill a rectangular area in many different thick lines. So, when do we use a brush and painting? As in the example above, usually a graphical profile (using the Drawxxx () method) is implemented with a brush object, and a fill area (using the Fillxxx () method is implemented with a brush object. Use a brush object:

In the following example, we use the system.drawing.drawing2d namespace. In the example, we painted straight lines, ellipses, pie graphics, polygons such as different styles.

Using system;

Using system.windows.forms;

Using system.drawing;

Using system.drawing.drawing2d;

Public Class Drawgra: form {

Public DrawGra () {

THIS.TEXT = "Application of Brush Example";

This.size = New size (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 has Dash, Dashdot, Dashdotdot, Dot, Solid, etc. * /

// draw a straight line in Dash style

Penline.dashstyle = dashstyle.dash;

g.drawline (Penline, 50, 50, 100, 200);

// Draw an ellipse in a Dashdotdot style

PENELLIPSE.DASHSTYLE = dashstyle.dashdotdot;

G. Drawellipse (PENELLIPSE, 15, 15, 50, 50);

// Draw a piece of pie in DOT style

Penpie.dashstyle = dashstyle.dot;

G. Drawpie (Penpie, 90, 80, 140, 40, 120, 100);

/ / Draw a polygon in a Solid style

g.drawpolygon (Penpolygon, New Point [] {

New Point (30, 140),

New Point (270, 250),

New Point (110, 240),

New Point (200, 170),

New Point (70, 350),

NEW POINT (50, 200)});

}

Public static void main () {

Application.run (New DrawGra ());

}

Use a brush object:

The painting object is filled with a specific color, mode or image. There are four types of paintings: Solidbrush (default painting), Hatchbrush, GradientBrush, and TextureDbrush. Below, we give an example to introduce.

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 populate 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 = "Use Hatchbrush Example";

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

/ * HatchStyle of this painting has Diagonalcross,

ForwardDiagonal, Horizontal, Vertical, Solid, etc. * /

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 two kinds of lineargradientbrush and pathgradientbrush. From their names we can know that the former is linear gradient, and the latter is the path gradient, so it can create more complex and perfect effects. Below I will give you all:

1), use lineargradientbrush:

Using system;

Using system.windows.forms;

Using system.drawing;

Using system.drawing.drawing2d; public class lineargradientBRU: form {

Public lineargradientbru () {

THIS.TEXT = "Use the LinearGradientBrush Example";

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 graphics are 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 = "Use the pathGradientBrush example";

This.Paint = new painteventhandler (Fill_Graph);

}

Public void Fill_Graph (Object Sender, Painteventargs E) {

E.Graphics.TextRenderingHint = TextRenderingHint.ntialiased;

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 Point (40, 140),

New Point (275, 200),

New Point (105, 225),

New Point (190, 300),

New Point (50, 350),

New Point (20, 180),

}, new byte [] {

(Byte) PathPointType.start,

(Byte) PathPointType.bezier,

(Byte) PathPointType.bezier,

(Byte) PathPointType.bezier,

(Byte) PathPointType.Line,

(Byte) PathPointType.Line,

});

// Create a PathGradientBrush

PathGradientbrush PGB = New pathgradientbrush (path);

// Set the surrounding color of the painting brush

Pgb.surroundcolors = new color [] {

Color.green,

Color.Yellow,

Color.Red,

Color.Blue,

Color.range,

Color.White,

}

// filled with a painting brush

E.Graphics.FillPath (PGB, PATH);

}

Public static void main () {

Application.run (New pathGradientBru ());

}

}

The resulting graphics are 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 the background of filling the ellipse

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 ());

}

}

Use an image:

The image is often used in graphical programming, which is also very obvious to the user interface. During the previous programming, the details of the operation of the image are quite cumbersome and it is easy to errors. Now, under GDI , you can use C # language to easily complete your desired image programming.

Very simple, you can implement the image programming by just the following steps.

1. Create a bitmap class 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 instances of using images, it is limited to the space, I will no longer be introduced here. I believe that everyone can easily complete an example of using images.

to sum up:

In this article, I mainly used two very core namespaces: one is system.drawing, one is system.drawing.drawing2d. With them, we can easily call the methods, attributes to implement the task that can be done in the past, which can make a lot of tasks that can be completed. This can't be said to be the advantages of GDI . So, master GDI , I believe that your graphic programming ability will be on the first floor.

转载请注明原文地址:https://www.9cbs.com/read-34654.html

New Post(0)