رسم شی با گرافیک رسم چارت با گرافیک به صورت دستی با سی شارپ

رسم اشیا با کمک گرافیک اگر سوالی در مفهوم بود در خدمتم لینک اصلی سایت مرجع در پایان می باشد .

Introduction

The most basic tool you can use is the pen. The GDI+ library provides a pen through the Pen class.

Creating a Pen

To obtain a pen, you can declare a variable of type Pen using one of the constructors of its class.

Characteristics of a Pen

The Color of a Pen

The primary piece of information you must specify about a pen is its color. To do this, you can use the following constructor:

Here is an example:

Instead of first declaring a Color variable, you can directly define the color in the constructor.

If you just want to create a regular pen whose only known characteristic would be its color, the System.Drawing namespace provides the Pens class. The primary, and in fact only, role of the Pens class is to define a simple pen and only specify its color. To make this possible, the Pens class is equipped with only static properties that each represents the name of a color. The names are those of common colors (such as Red, Green, Blue, Yellow, Violet, et), the colors used on web pages (such as LightBlue or DarkGreen), the colors defined in Microsoft Windows (such as ActiveBorder, AppWorkspace, or ButtonFace, etc), and many others. When accessing a Pens property, it produces a Pen object. This means that you can access a pen to initialize a Pen variable. Here is an example:

If you have already created a pen, to change its color, you can assign the desired color name or color value to the Pen.Color property.

The Width of a Pen

The simplest pen is meant to draw a tinny line. Here is an example of a line drawn with a simple pen:

Line

Such a simple pen is said to have a width of 1 pixel. To give you the ability to support or modify the width of a pen, the Pen class is equipped with a Width property.

When creating a pen, to specify is width, you can use the following constructor:

While the first argument represents the color as we saw in the previous section, the second argument specifies the width, which must be a floating-point value. Here is an example:

If you have already defined a pen and you want to change its width, the Pen class provides the Width property. Using this property, to modify the width of a pen, assign a floating-point number to its Width property. Here is an example:

In the same way, you can change, increase, or decrease the width of a pen as often as you want. Here are examples:

We may get the following result:

If a pen has already been defined and you want to know its width, get the value of its Width property.

The Start and End Caps

If you use a pen with a small width, such as 1 pixel, you may not notice how a line drawn with it starts but with a significantly wide pen, you would notice that it starts with a flat shape. An alternative is to have round, square, or triangle start. This is referred to as the start cap.

To support the starting shape of a line, the Pen class is equipped with a property named StartCap. The Pen.StartCap property is of based on the LineCap enumeration whose members are: AnchorMask, ArrowAnchor, Custom, DiamondAnchor, Flat, NoAnchor, Round, RoundAnchor, Square, SquareAnchor, and Triangle. To specify the start cap, assign the desired LineCap member to the StartCap property of the pen. Here are examples:

These may produce the following results:

If none of the available members of the LineCap enumeration suits you, you can define a custom cap using the CustomStartCap class.

You can also control the end cap of a line. To support this, the Pen class is equipped with a property named EndCap that also is based on the LineCap enumeration with the same value. Using a combination of the start and end caps, you can control how a line starts and how it ends. If none of the available members of the LineCap enumeration suits you, you can define a custom cap using the CustomEndCap class.

Regular Shapes

Rectangles and Squares

A rectangle is a geometric figure made of four sides that compose four right angles. To draw a rectangle, you can either specify the Rectangle value that encloses it, or you can define its location and its dimensions. To draw a rectangle that is enclosed in a Rectangle value, you can use the following version of the Graphics.DrawRectangle() method:

Remember that such a rectangle object can be illustrated as follows:

After defining a Rectangle variable, you can pass it to the method. Here is an example:

Remember that you can also define a Pen and/or a Rectangle objects in the parentheses of the method:

This would produce:

A Rectangle Drawn From a Rectangle Value

It is (very) important to remember that the third argument of the Rectangle represents its width (and not its right) value and the fourth argument represents its height (and not its bottom) value. This is a confusing fact for those who have programmed in GDI: GDI+ defines a Rectangle differently than GDI. In fact, to determine the location and dimensions of a rectangle to draw, the Graphics class provides the following versions of the DrawRectangle() method:

This time, the rectangle is represented by its location with a point at (x, y) and its dimensions with the width and height argument. This can be illustrated in a Windows coordinate system as follows:

Rectangle

Based on this, the earlier rectangle can also be drawn with the following:

A square is a rectangle whose four sides are equal.

Practical LearningPractical Learning: Drawing a Rectangle

  1. Start Microsoft Visual C# and create a new Windows Application named WeeklySales1
  2. Design the form as follows:
    Weekly Sales
    Control Name Text
    Label Label Monday
    Label Label Tuesday
    Label Label Wednesday
    Label Label Thursday
    Label Label Friday
    TextBox TextBox txtMonday ۰
    TextBox TextBox txtTuesday ۰
    TextBox TextBox txtWednesday ۰
    TextBox TextBox txtThursday ۰
    TextBox TextBox txtFriday ۰
    Button Button Generate btnGenerate
  3. Double-click an unoccupied area of the form and change the file as follows:
  4. Return to the form and click an empty area on it. In the Properties window, click the Events button Events
  5. Double-click the Paint field and implement its event as follows:
  6. Return to the form and double-click the Generate button
  7. Implement the event as follows:
  8. Execute the application and test the form
  9. After using it, close the form

A Series of Rectangles

The DrawRectangle() method is used to draw one rectangle. If you plan to draw many rectangles, you can proceed in one step by using the Graphics.DrawRectangles() method. It comes in two versions whose syntaxes are:

This method requires an array of Rectangle or RectangleF values. When executed, it draws individual rectangles using each member of the array as its own rectangle. Here is an example:

This would produce:

Rectangles

Practical LearningPractical Learning: Drawing a Series of Rectangles

  1. Start a new Windows Application named YearlySales1
  2. Design the form as follows:
    Control Name Text
    GroupBox GroupBox Current Year’s Sales
    Label Label ۱st Qtr
    Label Label ۲nd Qtr
    Label Label ۳rd Qtr
    Label Label ۴th Qtr
    TextBox TextBox txtCurrentQtr1 ۰
    TextBox TextBox txtCurrentQtr2 ۰
    TextBox TextBox txtCurrentQtr3 ۰
    TextBox TextBox txtCurrentQtr4 ۰
    Button Button Close btnClose
    GroupBox GroupBox Previous Year’s Sales
    Label Label ۱st Qtr
    Label Label ۲nd Qtr
    Label Label ۳rd Qtr
    Label Label ۴th Qtr
    TextBox TextBox txtPreviousQtr1 ۰
    TextBox TextBox txtPreviousQtr2 ۰
    TextBox TextBox txtPreviousQtr3 ۰
    TextBox TextBox txtPreviousQtr4 ۰
    Button Button Generate btnGenerate
    Label Label _________ Legend _________
    Label Label lblCurYear This Year’s Sales
    Label Label lblLastYear Last Year’s Sales
  3. Double-click an unoccupied area of the form and change the file as follows:
  4. Return to the form and click an empty area on it. In the Properties window, click the Events button Events
  5. Double-click the Paint field and implement its event as follows:
  6. Return to the form and double-click the Generate button
  7. Implement the event as follows:
  8. Return to the form. Double-click the Close button and implement its Click event as follows:
  9. Execute the application and test the form
  10. After using it, close the form

Ellipses and Circles

An ellipse is a closed continuous line whose points are positioned so that two points exactly opposite each other have the exact same distant from a central point. It can be illustrated as follows:

Ellipse

Because an ellipse can fit in a rectangle, in GDI+ programming, an ellipse is defined with regards to a rectangle it would fit in. To draw an ellipse, you can use the Graphics.DrawEllipse() method that comes in four versions whose syntaxes are:

The arguments of this method play the same roll as those of the Graphics.DrawRectangle() method:

Ellipse 2

Here is an example:

Ellipse

Practical LearningPractical Learning: Drawing a Circle

  1. Start a new Windows Application named RotatingCircles1
  2. Double-click middle of the form and change the file as follows:
  3. From the Components section of the Toolbox, click Timer and click the form
  4. In the Properties window, change the following values:
    Enabled: True
    Interval: 20
  5. Under the form, double-click the timer1 object and make the following changes:
  6. Execute the application to see the result
  7. Close the form

Lines

A Line

A line is a junction of two points. This means that a line has a beginning and an end:

Line Definition

The beginning and the end are two distinct points. Based on this, a line is represented either with two Point values or by four numbers representing its values on the Cartesian axes. To draw a line, the Graphics class is equipped with the following overloaded DrawLine() method:

If the line is represented with natural numbers, its origin can be specified as a Point pt1 and its end would be represented with a Point pt2. If the line is drawn using floating numbers, you can start it at one PointF pt1 and end it at another PointF pt2. Otherwise, you can specify the starting point with coordinates (x1, y1) and the end would be represented with coordinates (x2, y2). The same type of line can be drawn using decimal values from coordinates (x1, y1) to coordinates (x2, y2).

Here is an example that draws three lines:

Line

A Series of Lines

The above DrawLine() method is used to draw one line. If you intend to draw a group of lines at once, you can use the Graphics.DrawLines() method. It is overloaded with two versions as follows:

To use this method, you should first define an array of either Point for natural numbers that represent Cartesian coordinates or PointF for floating numbers. Here is an example:

This would produce:

Drawing Lines

Polygons

A polygon is a series of connected lines with the whole shape being closed. In other words, a polygon is defined a group of lines so that, except for the first line of the group, the starting point of each line is the same as the end point of the previous line and the end point of the last line is connected to the start point of the first line.

To draw a polygon, you can use the Graphics.Polygon() method. It is overloaded with two versions as follows:

To use this method, you can first declare a Point or PointF array and pass it as the second argument to the method. Here is an example:

This would produce:

Polygon

سایت مرجع

شاید این مطالب را هم دوست داشته باشید

پاسخ دهید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *