Game Development of .Nettrix: GDI+ and Collision Detection - Performing Graphic Operations with a Graphics Object
(Page 2 of 22 )
When using GDI+, the very first step always is to create a Graphics object, which will help you to perform graphics operations. The Graphics class provides methods for drawing in a specific device context.
There are four ways to attain the correct Graphics object: with the e parameter received in the Paint event, from a window handle, from an image, or from a specified handle to a device context. There’s no real difference among these different approaches; you’ll use each one depending on your program needs. For example, if you are coding your drawing functions on the Paint event of the form, you’ll use the e parameter; but if you are coding a class to draw on a form, you’ll probably want to use a window handle to create the Graphics object. We discuss each method in the sections that follow.
Creating a Graphics Object with the PaintEventArgs Parameter In this case, all drawing code must be associated with the Paint event of the destination image object. The following code shows how to draw a simple red rectangle at the 10, 20 position (in pixels) on the screen, 7 pixels high and 13 pixels long:
private PicSourcePaint(Object sender, Windows.Forms.PaintEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Red), 10, 20, 13, 7);
}
Note: In these first few lines of code, you can see the event-handling features of .NET, as described here: Every event handler in C# receives at least two parameters, the sender object, which is the object that generates the event, and an object related to the event (the EventArgs object). The event handler procedure is now associated with the object by associating the method to the event, typically in the InitializeComponent method. The association is done with the += operator like this: this.PicSource.Paint += new System.Windows.Forms.PaintEventHandler(this.picBackgroundPaint); The e parameter is of the type Windows.Forms.PaintEventArgs. You will notice that everything in .NET languages is organized into managed units of code, called namespaces. In this case, you use the System.Windows.Forms namespace, which contains classes for creat ing Windows-based applications using the features of the Windows operating system. Inside this namespace, you use the PaintEventArgs class, which basically gives the Paint event access to the rectangle structure that needs to be updated (ClipRectangle property), and the Graphics object used to update it. The Graphics and SolidBrush classes are defined in the System.Drawing namespace. This namespace has several classes that provide all the functionality you need to work with 2-D draw ings, imaging control, and typography. In the code sample, you create a SolidBrush object with red color (using the Color structure) to draw a filled rectangle using the FillRectangle method of the Graphics object. |
Creating Graphics Objects from a Window Handle
In order to create any graphical images in GDI+, you must ask for a “handle” to the drawable part of a window. This handle, which is a Graphics object, can be obtained by the Graphics.FromHwnd method (Hwnd means “Handle from a window”). In the code shown here, Graphics.FromHwnd is a shortcut for the System.Drawing.Graphics.FromHwnd method, which creates a Graphics object used to draw in a specific window or control, given its handle. This code references a pictureBox control named picSource:
Graphics graph = new Graphics();
graph = Graphics.FromHwnd(picSource.Handle);
graph.FillRectangle(new SolidBrush(Color.Red), 10, 20, 13, 7);
Creating Graphics Objects from an Image The FromImage method shown here creates a Graphics object from the specified image:
Graphics graph = new Graphics();
graph = Graphics.FromHwnd(picSource.image);
graph.FillRectangle(new SolidBrush(Color.Red), 10, 20, 13, 7);
Note that the previous code sample will work only if you have a valid bitmap image loaded on the pictureBox control. If you try to execute it against an empty picture box or using a picture box with an indexed pixel format image loaded (such as a JPEG image), you’ll get an error and the Graphics object won’t be created.
Creating a Graphics Object from a Specified Handle to a Device Context Similar to the previously mentioned methods, the Graphics.FromHdc method creates a Graphics object that allows the program to draw over a specific device context, given its handle. You can acquire the device handle from another Graphics object, using the GetHdc method, as shown in the next code snippet:
public void FromHdc(PaintEventArgs e) {
// Get handle to device context.
IntPtr hdc = e.Graphics.GetHdc();
// Create new graphics object using handle to device context.
Graphics newGraphics = Graphics.FromHdc(hdc);
newGraphics. FillRectangle(new SolidBrush(Color.Red), 10, 20, 13, 7);
// Release handle to device context.
e.Graphics.ReleaseHdc(hdc);
This chapter is from Beginning .NET Game Programming in C#, by David Weller, et al., (Apress, 2004, ISBN: 1590593197). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Creating Gradients >>
More .NET Articles
More By Apress Publishing