Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphics.

Similar presentations


Presentation on theme: "Graphics."— Presentation transcript:

1 Graphics

2 Parts: 1) Drawing Graphics 2) Working with Images 3) Formatting Text

3 Drawing Graphics

4 System.Drawing Namespace

5 How to Specify the Location and Size of Controls
button1.Location = new Point(10, 10); button1.Left = 10; button1.Top = 10; button1.Size = new Size(30, 30);

6 How to Specify the Color of Controls
button1.ForeColor = Color.Red; button1.BackColor = Color.Blue; button1.ForeColor = Color.FromArgb(10, 200, 200); button1.BackColor = Color.FromArgb(200, 5, 5);

7 How to Draw Lines and Shapes
Create a Graphics object by calling the System.Windows.Forms.Control.CreateGrahics method. 2. Create a Pen object. 3. Call a member of the Graphics class to draw on the control using the Pen.

8 Graphics Class Members

9 private void Form1_Paint(object sender, PaintEventArgs e) { // Create a graphics object from the form Graphics g = this.CreateGraphics(); // Create a pen object with which to draw Pen p = new Pen(Color.Red, 7); Draw the line g.DrawLine(p, 1, 1, 100, 100); }

10 Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Blue, 3); g.DrawPie(p, 1, 1, 100, 100, -30, 60);

11 Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.MediumPurple, 2); //Create an array of points Point[] points = new Point[] {new Point(10, 10), new Point(10, 100), new Point(50, 65), new Point(100, 100), new Point(85, 40)}; //Draw a shape defined by the array of points g.DrawPolygon(p, points);

12 Graphics g = this. CreateGraphics(); Pen p = new Pen(Color. Red, 7); p
Graphics g = this.CreateGraphics(); Pen p = new Pen(Color.Red, 7); p.DashStyle = DashStyle.Dot; g.DrawLine(p, 50, 25, 400, 25); p.DashStyle = DashStyle.Dash; g.DrawLine(p, 50, 50, 400, 50); p.DashStyle = DashStyle.DashDot; g.DrawLine(p, 50, 75, 400, 75); p.DashStyle = DashStyle.DashDotDot; g.DrawLine(p, 50, 100, 400, 100); p.DashStyle = DashStyle.Solid; g.DrawLine(p, 50, 125, 400, 125);

13 Graphics g = this.CreateGraphics(); Pen p = new Pen(Color.Red, 10); p.StartCap = LineCap.ArrowAnchor; p.EndCap = LineCap.DiamondAnchor; g.DrawLine(p, 50, 25, 400, 25); p.StartCap = LineCap.SquareAnchor; g.DrawLine(p, 50, 50, 400, 50); p.StartCap = LineCap.Flat; p.EndCap = LineCap.Round; g.DrawLine(p, 50, 75, 400, 75); p.StartCap = LineCap.RoundAnchor; p.EndCap = LineCap.Square; g.DrawLine(p, 50, 100, 400, 100);

14 How to Fill Shapes ■ System.Drawing.Drawing2D.HatchBrush Defines a rectangular brush with a hatch style, a foreground color, and a background color ■System.Drawing.Drawing2D.LinearGradientBrush Encapsulates a brush with a linear gradient that provides a visually appealing, professional-looking fill. ■ System.Drawing.Drawing2D.PathGradientBrush Provides similar functionality to LinearGradientBrush; however, you can define a complex fill pattern that fades between multiple points. ■ System.Drawing.SolidBrush Defines a brush of a single color ■ System.Drawing.TextureBrush Defines a brush made from an image that can be tiled across a shape, like a wallpaper design.

15 Graphics g = this.CreateGraphics();
Brush b = new SolidBrush(Color.Maroon); Point[] points = new Point[] {new Point(10, 10), new Point(10, 100), new Point(50, 65), new Point(100, 100), new Point(85, 40)}; g.FillPolygon(b, points);

16 Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Maroon, 2); Brush b = new LinearGradientBrush(new Point(1,1), new Point(100,100), Color.White, Color.Red); Point[] points = new Point[] {new Point(10, 10), new Point(10, 100), new Point(50, 65), new Point(100, 100), new Point(85, 40)}; g.FillPolygon(b, points); g.DrawPolygon(p, points);

17 Summary ■ The System.Drawing namespace provides tools for drawing graphics and editing existing images. The most useful classes are Graphics, Image, and Bitmap. ■ Use the Point and Size classes to specify the location and size of controls. ■ The System.Drawing.Color structure provides predefined properties for common colors. ■ To draw lines and shapes, create an instance of the Graphics class, create a Pen object, and then call one of the Graphics member methods to draw a line or a shape using the Pen instance.

18 Working with Images

19 The Image and Bitmap Classes

20 How to Display Pictures
Image i = fishing.bmp"); pictureBox1.BackgroundImage = i; Bitmap b = new fishing.bmp"); pictureBox1.BackgroundImage = b; Bitmap bm = new Graphics g = this.CreateGraphics(); g.DrawImage(bm, 1, 1, this.Width, this.Height);

21 How to Create and Save Pictures
Bitmap bm = new Bitmap(600, 600); Graphics g = Graphics.FromImage(bm); Brush b = new LinearGradientBrush(new Point(1, 1), new Point(600, 600), Color.White, Color.Red); Point[] points = new Point[] {new Point(10, 10), new Point(77, 500), new Point(590, 100), new Point(250, 590), new Point(300, 410)}; g.FillPolygon(b, points); bm.Save("bm.jpg", ImageFormat.Jpeg);

22 How to Use Icons Graphics g = this.CreateGraphics(); g.DrawIcon(SystemIcons.Question, 40, 40);

23 Summary ■ The Image and Bitmap classes enable you to edit or create pictures, and save the results as a file. ■ To display a picture in a Windows Forms assembly, load the picture into an instance of the Image or Bitmap class, create an instance of the PictureBox control, and then use the Image or Bitmap object to define the PictureBox.BackgroundImage property. ■ To create and save a picture, create a Bitmap object, edit it using a Graphics object, and then call the Bitmap.Save method. ■ To display an icon, call the Graphics.DrawIcon or Graphics.DrawIconUnstretched methods using one of the properties of the SystemIcons class.

24 Formatting Text

25 How to Add Text to Graphics
1. Create a Graphics object. 2. Create a Font object. 3. Optionally, create a Brush object. 4. Call Graphics.DrawString and specify the location for the text.

26 How to Create a Font Object
Font f = new Font("Arial", 12, FontStyle.Bold); FontFamily ff = new FontFamily("Arial"); Font f = new Font(ff, 12); FontConverter converter = new FontConverter(); Font f = (Font)converter.ConvertFromString("Arial, 12pt");

27 How to Write Text Graphics g = this.CreateGraphics(); Font f = new Font("Arial", 40, FontStyle.Bold); g.DrawString("Hello, World!", f, Brushes.Blue, 10, 10);

28 How to Control the Formatting of Text
StringFormat class

29 Graphics g = this.CreateGraphics();
// Construct a new Rectangle. Rectangle r = new Rectangle(new Point(40, 40), new Size(80, 80)); // Construct 2 new StringFormat objects StringFormat f1 = new StringFormat(StringFormatFlags.NoClip); StringFormat f2 = new StringFormat(f1); // Set the LineAlignment and Alignment properties for // both StringFormat objects to different values. f1.LineAlignment = StringAlignment.Near; f1.Alignment = StringAlignment.Center; f2.LineAlignment = StringAlignment.Center; f2.Alignment = StringAlignment.Far; f2.FormatFlags = StringFormatFlags.DirectionVertical; // Draw the bounding rectangle and a string for each // StringFormat object. g.DrawRectangle(Pens.Black, r); g.DrawString("Format1", this.Font, Brushes.Red, (RectangleF)r, f1); g.DrawString("Format2", this.Font, Brushes.Red, (RectangleF)r, f2);

30 Summary ■ To add text to graphics, create a Graphics object, create a Font object, optionally create a Brush object, and then call the Graphics.DrawString method. ■ To create a Font object, pass the font family name, font size, and font style. ■ Write text by calling the Graphics.DrawString method. The DrawString method requires a Font object, a Brush object that specifies the color of the text, and the location to draw the text. ■ Use the StringFormat class to control the formatting of text. You can use this class to change the direction of the text, or to change the alignment of text.

31 Your Key Competences ■ Describe the members of the System.Drawing namespace. ■ Control the location, size, and color of controls. ■ Draw lines, empty shapes, and solid shapes. ■ Customize pens and brushes to enhance graphics. ■ Describe the purpose of the Image and Bitmap classes. ■ Display pictures in forms or PictureBox objects. ■ Create a new picture, add lines and shapes to the picture, and save it as a file. ■ Describe the process of creating the objects required to add text to images. ■ Create Font objects to meet your requirements for type, size, and style. ■ Use Graphics.DrawString to annotate images with text. ■ Control the formatting of text.

32 Key Terms ■ Bitmap ■ Brush ■ Graphics ■ Pen


Download ppt "Graphics."

Similar presentations


Ads by Google