Handling Animations and Bitmaps Using GDI+ for Image Manipulation - Working With Bitmaps
(Page 3 of 4 )
By definition, a Bitmap is "a data file or structure representing a generally rectangular grid of pixels, or points of color, on a computer monitor, paper, or other display device." In other words, a bitmapped image corresponds bit by bit to the image displayed on the screen. So a BMP (bitmapped) file stores data in pixel format. GDI+ provides functionalities to manipulate BMP through the Bitmap class which is a derivation of the Image class. Hence, it provides all the functionalities provided by the Image class and adds its own functionalities to work with BMP images. The basic functionalities provided by the Bitmap class are:
- Loading the BMP Image
- Viewing the BMP Image
Since the BMP images differ in the way that data is stored (it's pixel based), the way the images are loaded and viewed is also different.
To load a BMP image, an object of the Bitmap class needs to be created. Such an object can be created using a constructor of Bitmap class in the following ways:
- From File:
In this case, a new Bitmap object is created from an existing file. The existing file can be in any of the GDI+ supported formats. For example, the following code instantiates a Bitmap object from a GIF file:
Bitmap curBitmap = new Bitmap("myfile.gif");
- From an object of Image class:
A Bitmap object can also be created from an existing image object. Just as with creating an object from a file, all the supported GDI+ formats based on the Image object can be used. The following code creates a Bitmap object from an Image object which in turn is created from a GIF file:
Image curImage = Image.FromFile("myfile.gif");
Bitmap curBitmap = new Bitmap(curImage);
- From an object of the Image class with a specified size:
This is the specialized form of the previous method. In this case, the object being created can be assigned dimensions (width and height) at the time of instantiation. The following statement illustrates this point:
Bitmap curBitmap3 =new Bitmap(curImage, new Size(200, 100) );
- An empty Bitmap object:
When no Image reference or file name is provided, an empty Bitmap object is created. The following statement does the same:
Bitmap curBitmap = new Bitmap(200, 100);
Apart from the constructor, the Bitmap class provides two static methods that can be used to create BMP from icons (.ico files) and resources (.res files). They are FromHicon and FromResource. Both use handles to icons or resources to create a Bitmap object from icons and resources. That completes the ways to load BMP. Next we'll see what we need to do to view the loaded BMP.
Viewing BMP Image using the Bitmap class is just like viewing any other image using the Image class. Once the Bitmap object is created, it has to be passed to the DrawImage method. The following code illustrates this point:
Graphics g = this.CreateGraphics();
Bitmap bitmap = new Bitmap("myfile.jpg");
g.DrawImage(bitmap, 20, 20);
g.Dispose();
That completes this section. In the next section I will enhance the image viewer application to include a GIF animation control menu.
Next: Image Animation in the Real World >>
More Code Examples Articles
More By A.P.Rajshekhar