03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Windows Forms Printing ❘ 367<br />

Similarly, a Form, PictureBox, or other control can provide a Paint event that includes an<br />

e.Graphics parameter on which a program can draw. The Paint event is raised when a control needs<br />

to redraw some or all of itself.<br />

Note that the Graphics object included in a Paint event handler may clip its drawing methods, so<br />

it redraws only the parts of the control that actually need to be redrawn. That means any graphics<br />

drawn outside of those areas are ignored. You don’t need to do anything special to make this work.<br />

Just be aware that some of your graphics may not actually be drawn.<br />

For example, if you want to redraw an entire PictureBox to cover it with random circles, you need to<br />

refresh the entire PictureBox to ensure that everything you draw appears. (You can refresh a control<br />

by calling its Refresh method.)<br />

The last common way to obtain a Graphics object is to create one that is associated with a<br />

Bitmap. The program can then use the Graphics object to draw on the Bitmap. The following<br />

code demonstrates this technique.<br />

private void Form1_Load(object sender, EventArgs e)<br />

{<br />

// Make a Bitmap.<br />

Bitmap bitmap = new Bitmap(100, 100);<br />

using (Graphics graphics = Graphics.FromImage(bitmap))<br />

{<br />

// Draw an ellipse on it.<br />

graphics.DrawEllipse(Pens.Brown, 0, 0, 99, 99);<br />

}<br />

}<br />

// Display it on the form.<br />

this.BackgroundImage = bitmap;<br />

This code creates a 100 × 100 pixel Bitmap. (Coordinates for all graphics in .NET are in pixels.) It<br />

then uses the Graphics.FromImage method to create a Graphics object associated with the Bitmap.<br />

The Graphics class provides a Dispose method, so the program includes a using statement to<br />

ensure that the method is called when the program is done with the object.<br />

Next, the code uses the Graphics object’s DrawEllipse method to draw a brown ellipse on the<br />

Bitmap. The code finishes by displaying the Bitmap in the form’s BackgroundImage property.<br />

Pens<br />

The Pen object determines how lines are drawn. It determines a line’s color, thickness, dash style,<br />

join style, and end cap style.<br />

A program can explicitly create Pen objects, but often it can simply use one of the more than 280<br />

stock pens that are predefined by the Pens class. For example, the following code draws a rectangle<br />

using a hot pink line that’s one pixel wide.<br />

gr.DrawRectangle(Pens.HotPink, 10, 10, 50, 50)<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!