Basic Image Manipulation using GDI+ and C# - Image Manipulation Using GDI : Step By Step
(Page 3 of 4 )
Now let's have a look at the steps involved in using the GDI+ for image manipulation. There are four main steps:
- Overriding the Paint event of Form.
- Obtaining the Graphics object.
- Obtaining the Image object.
- Apply manipulation methods.
The first two steps are required whenever one has to work with GDI+. However, the last two come into the picture (pun not intended) only when image manipulation has to be done. So here are the details:
Overriding the Paint event of form
In .Net applications, drawing is done on a Form's surface. This is same for both web and desktop applications. The function where the actual drawing code can be hooked in is the Paint method. So, the first step is to override this method to get a foothold into the draw process within the application. This can be achieved either by supplying a Paint handler to the Form's paint method or by overriding the OnPaint method. It is better to provide a Paint handler.
Obtaining the Graphics object
The next step is to obtain the object of the Graphics class, because it is only through the Graphics object that the drawing methods can be called. To show an image it has to be drawn onto the Form. That can only be done using the DrawImage method of an object of the Graphics class. An object of the Graphics class is associated with a form. So it can be obtained by one of three methods:
- Using PaintEventArgs argument passed into the Paint handler.
- Using PaintArgs argument passed into the OnPaint method.
- Using CreateGraphics method of a Form.
An example of the first method would be thus:
private void form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
}
where form1_Paint is the handler for Paint event of Form form1. The Graphics property of the PaintEventArgs returns a Graphics object associated with the Form object. The second way can be exemplified as:
protected override void OnPaint(PaintEvent
{
Graphics g = e.Graphics;
}
The main difference between the first and second examples is that in the second way, the OnPaint method is overridden. The example for the third way would be:
Graphics g = this.CreateGraphics();
Where this refers to the Form in which this statement has been embedded. The CreateGraphics() method returns a method object of the Form. The only drawback of this approach is that the Graphics object would have to be disposed of manually using the dispose method of the Graphics object. I prefer the first method; therefore I will be using it henceforth.
Obtaining the Image object
The image object can be obtained in three different ways using the static methods provided by the Image class:
- The FromFile method returns an Image from the file specified as an argument.
- FromHbitmap creates and returns an Image object from a window handle to a bitmap.
- FromStream creates an Image object from a stream of bytes (in a file or a database).
Among these, the first method is the most commonly used if the image is available as a file, whereas the third method comes handy in web applications where the images would be stored as a column value of a table. The following statement creates an object of the Image class using the first method:
Image curImage = Image.FromFile("flower.bmp");
Apply manipulation methods
For the sole purpose of imaging, the Image class provides many methods. The most common in imaging are Flip and Rotate. The Image class provides for these by the following method - RotateFlip. The parameter to this method defines whether flipping as well as rotating both have to be done or only a flip or rotation must be applied. It also defines through what degrees flipping or rotating must be done. The following table provides the most common arguments to the RotateFlip method as well as their descriptions. The arguments being passed are the members of the RotateFlipType.
|
Member | Description |
Rotate180FlipNone | 180-degree rotation without flipping |
Rotate180FlipX | 180-degree rotation with a horizontal flip |
Rotate180FlipXY | 180-degree rotation with horizontal and vertical flips |
Rotate180FlipY | 180-degree rotation with a vertical flip |
Rotate270FlipNone | 270-degree rotation without flipping |
Rotate270FlipX | 270-degree rotation with a horizontal flip |
Rotate270FlipXY | 270-degree rotation with horizontal and vertical flips |
Rotate270FlipY | 270-degree rotation with a vertical flip |
Rotate90FlipNone | 90-degree rotation without flipping |
Rotate90FlipX | 90-degree rotation with a horizontal flip |
Rotate90FlipXY | 90-degree rotation with horizontal and vertical flips |
Rotate90FlipY | 90-degree rotation with a vertical flip |
RotateNoneFlipNone | No rotation and no flipping |
RotateNoneFlipX | No rotation, with a horizontal flip |
RotateNoneFlipXY | No rotation, with horizontal and vertical flips |
RotateNoneFlipY | No rotation, with a vertical flip |
For example, to flip and rotate an image through 90 degrees on both the X- and Y- axes the statement would be:
curImage.RotateFlip(RotateFlipType.Rotate90FlipXY);
where curImage is an object of the type Image and the argument tells the RotateFlip method that the image has to be rotated through 90 degrees and needs to be flipped both vertically and horizontally.
That brings us to the end of this section. In the next section I will develop an application that will load, flip and rotate the loaded image.
Next: Image Manipulation in the Real World >>
More C# Articles
More By A.P.Rajshekhar