Lecture 8 2nd Course C#
Lecture 8 2nd Course C#
1
How to: Create Graphics Objects for Drawing
Namespace: System.Drawing
• The Pen class—Used for drawing lines, outlining shapes, or rendering other
geometric representations.
• The Brush class—Used for filling areas of graphics, such as filled shapes, images, or
text.
• The Font class—Provides a description of what shapes to use when rendering text.
• The Color structure—Represents the different colors to display.
• Work with the appropriate object listed above to draw what you need.
Expand table
To render See
2
How to: Draw a Line on a Windows Form
// Create pen.
Pen blackPen = new Pen(Color.Black, 3);
To draw rectangles, you need a Graphics object and a Pen object. The Graphics object provides
the DrawRectangle method, and the Pen object stores features of the line, such as color and
width.
Example
The following example draws a rectangle with its upper-left corner at (10, 10). The rectangle has
a width of 100 and a height of 50. The second argument passed to the Pen constructor
indicates that the pen width is 5 pixels.
When the rectangle is drawn, the pen is centered on the rectangle's boundary. Because the pen
width is 5, the sides of the rectangle are drawn 5 pixels wide, such that 1 pixel is drawn on the
boundary itself, 2 pixels are drawn on the inside, and 2 pixels are drawn on the outside. For
more details on pen alignment, see How to: Set Pen Width and Alignment.
The following illustration shows the resulting rectangle. The dotted lines show where the
rectangle would have been drawn if the pen width had been one pixel. The enlarged view of the
3
upper-left corner of the rectangle shows that the thick black lines are centered on those dotted
lines.
C#Copy
Pen blackPen = new Pen(Color.FromArgb(255, 0, 0, 0), 5);
e.Graphics.DrawRectangle(blackPen, 10, 10, 100, 50);
C#Copy
myPen.DashStyle = DashStyle.Dash;
myGraphics.DrawLine(myPen, 100, 50, 300, 80);
When you create a Pen, you can supply the pen width as one of the arguments to the
constructor. You can also change the pen width with the Width property of the Pen class.
A theoretical line has a width of 0. When you draw a line that is 1 pixel wide, the pixels are
centered on the theoretical line. If you draw a line that is more than one pixel wide, the pixels
are either centered on the theoretical line or appear to one side of the theoretical line. You can
set the pen alignment property of a Pen to determine how the pixels drawn with that pen will
be positioned relative to theoretical lines.
The values Center, Outset, and Inset that appear in the following code examples are members
of the PenAlignment enumeration.
4
The following code example draws a line twice: once with a black pen of width 1 and once with
a green pen of width 10.
• Set the value of the Alignment property to Center (the default) to specify that pixels
drawn with the green pen will be centered on the theoretical line. The following
illustration shows the resulting line.
The following code example draws a rectangle twice: once with a black pen of
width 1 and once with a green pen of width 10.
C#Copy
Pen blackPen = new Pen(Color.FromArgb(255, 0, 0, 0), 1);
Pen greenPen = new Pen(Color.FromArgb(255, 0, 255, 0), 10);
greenPen.Alignment = PenAlignment.Center;
• Set the value of the Alignment property to Center to specify that the pixels drawn
with the green pen will be centered on the boundary of the rectangle.
C#Copy
Pen blackPen = new Pen(Color.FromArgb(255, 0, 0, 0), 1);
Pen greenPen = new Pen(Color.FromArgb(255, 0, 255, 0), 10);
greenPen.Alignment = PenAlignment.Center;
5
// Draw the rectangle with the wide green pen.
e.Graphics.DrawRectangle(greenPen, 10, 100, 50, 50);
• Change the green pen's alignment by modifying the third statement in the
preceding code example as follows:
C#Copy
greenPen.Alignment = PenAlignment.Inset;
Now the pixels in the wide green line appear on the inside of the rectangle as
shown in the following illustration:
Brushes Class
• Reference
Feedback
Definition
Namespace:
System.Drawing
Assembly:
System.Drawing.Common.dll
Package:
System.Drawing.Common v8.0.0
Source:
Brushes.cs
Brushes for all the standard colors. This class cannot be inherited.
6
C#Copy
public static class Brushes
Inheritance
Object
Brushes
Examples
The following code example demonstrates the how to use a member of the Brushes class to fill
a Rectangle. This example should be used with a Windows Form. Paste this code into a form
and call this method when handling the form's Paint event, passing e as PaintEventArgs.
C#Copy
private void InstanceRectangleIntersection(PaintEventArgs e)
{
e.Graphics.DrawRectangle(Pens.Black, rectangle1);
e.Graphics.DrawRectangle(Pens.Red, rectangle2);
if (rectangle1.IntersectsWith(rectangle2))
{
rectangle1.Intersect(rectangle2);
if (!rectangle1.IsEmpty)
{
e.Graphics.FillRectangle(Brushes.Green, rectangle1);
}
}
}
Listing - 4
1. using System;
2. using System.Windows.Forms;
3. using System.Drawing;
7
4. using System.Drawing.Drawing2D;
5. public class Drawgra: Form {
6. public Drawgra() {
7. this.Text = "Illustrating DrawXXX() methods";
8. this.Size = new Size(450, 400);
9. this.Paint += new PaintEventHandler(Draw_Graphics);
10. }
11. public void Draw_Graphics(object sender, PaintEventArgs e) {
8
Brush class is used to fill the shapes with a given color, pattern or Image. There are four
types of Brushes like SolidBrush (Default Brush), HatchStyleBrush, GradientBrush and
TexturedBrush. The listings given below show the usage of each of these brushes by
applying them in a FillXXX() method:
Using SolidBrush
Listing 5
1. using System;
2. using System.Windows.Forms;
3. using System.Drawing;
4. public class Solidbru: Form {
5. public Solidbru() {
6. this.Text = "Using Solid Brushes";
7. this.Paint += new PaintEventHandler(Fill_Graph);
8. }
9. public void Fill_Graph(object sender, PaintEventArgs e) {
10. Graphics g = e.Graphics;
11. //Creates a SolidBrush and fills the rectangle
12. SolidBrush sb = new SolidBrush(Color.Pink);
13. g.FillRectangle(sb, 50, 50, 150, 150);
14. }
15. public static void Main() {
16. Application.Run(new Solidbru());
17. }
18. // End of class
19. }
9
Example :
using System.Drawing.Drawing2D;
namespace WindowsFormsApp7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
}
10
Home Work
11