GDI+ Resources: Bitmaps |
|
A bitmap is a graphic object used for displaying pictures on windows. It is the primary type of graphic used for various occasions. For example, a bitmap can be used as a background for a window. That is the case for the Pinball game that ships with some versions of Microsoft Windows: A bitmap can also be used for an aesthetic purpose to decorate a dialog box. That’s how it is used on some of the installation wizard boxes such as the graphic on the left section of the WordPerfect 2002 installer: Probably the most regular use of bitmaps is as small graphics on toolbars:
To create a bitmap, you can use any graphics application, including the Paint program that is installed with Microsoft Windows. In Visual Studio .NET, to create a bitmap, on the main menu, you would click Project -> Add New Item... In the Add New Item dialog box, click Bitmap File, accept the suggested name of the file or specify your own, and click Open: A new file with an extension of .bmp would be added to your project. You can then design it as you see fit. Here is an example:
To support bitmaps, the .NET Framework provides the Bitmap class. The Bitmap class is based on the abstract Image class. If you have created a bitmap and stored it as a file, you can pass the path of that file to the following constructor of the class: public Bitmap(string filename); (If you create a picture using the Add New Item dialog box, Visual C++ and Visual C# have different ideas when saving the file. For this reason, in Visual C#, you may have to either manually move the file or provide the complete path to the file). Once the picture is ready, to present it to the user, you can call the Graphics.DrawImage() method that is overloaded with as many different versions as you can possibly need. One of the versions of this method has the following syntax: public void DrawImage(Image img, Point pt); The first argument can be a bitmap that you can have previously initialized. The second argument specifies the location where the picture will be drawn, which will be the top-left corner of the picture with regards to its parent. Here is an example:
|
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Bitmap bmpFood = new Bitmap("C:\\Programs\\MSVCS\\WindowsApplication1\\FoodBasket.bmp"); Graphics graph = this.CreateGraphics(); graph.DrawImage(bmpFood, 0, 0); } |
Practical Learning: Displaying a Bitmap |
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Bitmap Butterfly = new Bitmap("Butterfly.bmp"); e.Graphics.DrawImage(Butterfly, 10, 10); } |
|
||
Home | Copyright © 2004-2009 FunctionX, Inc. | |
|