0% found this document useful (0 votes)
13 views11 pages

Lecture 8 2nd Course C#

This document is a lecture on creating graphics objects using C#, focusing on the Graphics object and its associated classes such as Pen, Brush, Font, and Color. It provides examples of how to draw lines, shapes, and images, as well as manipulate their properties like width and alignment. The document also includes code snippets demonstrating the use of various drawing methods and brushes in a Windows Forms application.

Uploaded by

yousif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

Lecture 8 2nd Course C#

This document is a lecture on creating graphics objects using C#, focusing on the Graphics object and its associated classes such as Pen, Brush, Font, and Color. It provides examples of how to draw lines, shapes, and images, as well as manipulate their properties like width and alignment. The document also includes code snippets demonstrating the use of various drawing methods and brushes in a Windows Forms application.

Uploaded by

yousif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

‫س‬ ‫ح‬‫ل‬‫ا‬ ‫ل‬ ‫ع‬ ‫س‬‫ق‬ ‫م‬ ‫ل‬ ‫مع‬‫كل عل الح س تكن ل جي ال‬

‫ م وم ا وب‬/ ‫ية وم ا وب و و و ا و اب‬


3rd Class – 2nd Course

Subject: Programming Language II (Advance C#)

Lecturer: Dr. Yousif A. Hamad


Lecture No 8

1
How to: Create Graphics Objects for Drawing
Namespace: System.Drawing

Drawing and Manipulating Shapes and Images


After it is created, a Graphics object may be used to draw lines and shapes, render text, or
display and manipulate images. The principal objects that are used with the Graphics object are:

• 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.

To use the Graphics object you have created

• Work with the appropriate object listed above to draw what you need.

For more information, see the following topics:

Expand table

To render See

Lines How to: Draw a Line on a Windows Form

Shapes How to: Draw an Outlined Shape

Text How to: Draw Text on a Windows Form

Images How to: Render Images with GDI+

2
How to: Draw a Line on a Windows Form

Pen pen = new Pen(Color.FromArgb(255, 0, 0, 0));


e.Graphics.DrawLine(pen, 20, 10, 300, 100);

public void DrawLineInt(PaintEventArgs e)


{

// Create pen.
Pen blackPen = new Pen(Color.Black, 3);

// Create coordinates of points that define line.


int x1 = 100;
int y1 = 100;
int x2 = 500;
int y2 = 100;

// Draw line to screen.


e.Graphics.DrawLine(blackPen, x1, y1, x2, y2);
}

How to: Use a Pen to Draw Rectangles

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

Dashed Lines and Line Caps


The Pen object also exposes properties, such as DashStyle, that you can use to specify features
of the line. The following example draws a dashed line from (100, 50) to (300, 80):

C#Copy
myPen.DashStyle = DashStyle.Dash;
myGraphics.DrawLine(myPen, 100, 50, 300, 80);

How to: Set Pen Width and Alignment


• Article
• 02/06/2023
• 2 contributors
Feedback

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.

To vary the width of a pen

• 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;

// Draw the line with the wide green pen.


e.Graphics.DrawLine(greenPen, 10, 100, 100, 50);

// Draw the line with the thin black pen.


e.Graphics.DrawLine(blackPen, 10, 100, 100, 50);

To change the alignment of a pen

• 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.

The following illustration shows the resulting 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);

// Draw the rectangle with the thin black pen.


e.Graphics.DrawRectangle(blackPen, 10, 100, 50, 50);

To create an inset pen

• 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)
{

Rectangle rectangle1 = new Rectangle(50, 50, 200, 100);


Rectangle rectangle2 = new Rectangle(70, 20, 100, 200);

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

Working with Pen objects


Listing - 4 given below examines the usage of various DrawXXX() methods by making use
of some properties in the System.Drawing.Drawing2D namespace:

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) {

12. Graphics g = e.Graphics;


13. Pen penline = new Pen(Color.Red, 5);
14. Pen penellipse = new Pen(Color.Blue, 5);
15. Pen penpie = new Pen(Color.Tomato, 3);
16. Pen penpolygon = new Pen(Color.Maroon, 4);
17. /*DashStyle Enumeration values are Dash,
18. DashDot,DashDotDot,Dot,Solid etc*/
19. penline.DashStyle = DashStyle.Dash;
20. g.DrawLine(penline, 50, 50, 100, 200);
21. //Draws an Ellipse
22. penellipse.DashStyle = DashStyle.DashDotDot;
23. g.DrawEllipse(penellipse, 15, 15, 50, 50);
24. //Draws a Pie
25. penpie.DashStyle = DashStyle.Dot;
26. g.DrawPie(penpie, 90, 80, 140, 40, 120, 100);
27. //Draws a Polygon
28. g.DrawPolygon(penpolygon, new Point[] {
29. new Point(30, 140),
30. new Point(270, 250),
31. new Point(110, 240),
32. new Point(200, 170),
33. new Point(70, 350),
34. new Point(50, 200)
35. });
36. }
37. public static void Main() {
38. Application.Run(new Drawgra());
39. }
40. // End of class
41. }

Working with Brush objects

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

private void Button1_Click(object sender, EventArgs e)


{
Graphics G = this.CreateGraphics();

Pen p1 = new Pen(Color.Blue,6);


Pen p2 = new Pen(Color.Red
, 6);
SolidBrush sb = new SolidBrush(Color.DarkRed);

G.DrawLine(p1, 50, 50, 300, 50);


G.DrawRectangle(p2, 30, 30, 320, 200);
G.FillEllipse(sb, 150, 100, 100, 100);
//G.FillPolygon(sb, new Point[ ] { new Point(20, 20),new Point(100,100),new
Point(100,200) });

}
}
}

10
Home Work

Write a program to Drow the following Diagram using Graphics

11

You might also like