Basic Image Manipulation using GDI+ and C# - Image Manipulation in the Real World
(Page 4 of 4 )
Starting from this part onwards, I will be developing an image manipulation application using C# and GDI+. The basic version that will be developed now will provide the following capabilities:
- Loading an image file.
- Rotating the loaded image 90 degrees.
- Flipping the image along its X-axis.
First the menus must be set up as shown in the figure below.


The names of the menus are:
- mnuLoad - the submenu that will show the Open dialog box and loads the file.
- mnu90rotate - rotates the image by 90 degrees.
- mnuXfilp- flips the image along X-axis.
Here is the handler for loading the image:
private void mnuLoad_Click(object sender,
System.EventArgs e)
{
// Create OpenFileDialog
OpenFileDialog opnDlg = new OpenFileDialog();
// Set a filter for images
opnDlg.Filter =
"All Image files|*.bmp;*.gif;*.jpg;*.ico;"+
"*.emf;,*.wmf|Bitmap Files(*.bmp;*.gif;*.jpg;"+
"*.ico)|*.bmp;*.gif;*.jpg;*.ico|"+
"Meta Files(*.emf;*.wmf;*.png)|*.emf;*.wmf;*.png";
opnDlg.Title = "ImageViewer: Open Image File";
opnDlg.ShowHelp = true;
// If OK, selected
if(opnDlg.ShowDialog() == DialogResult.OK)
{
// Read current selected file name
curFileName = opnDlg.FileName;
// Create the Image object using
// Image.FromFile
try
{
curImage = Image.FromFile(curFileName);
}
catch(Exception exp)
{
MessageBox.Show(exp.Message);
}
}
// Repaint the form, which forces the paint
// event handler
Invalidate();
}
The name of the file returned by the open file dialog is set as the current file name. Then it is used to load the file using the FromFile function of the Image class. The returned image is assigned to the curImage variable, which is of type Image, and its scope is of class level. The Next Invalidate() method is called so that a paint event is fired. Following the code, embedded in the Form's Paint method handler is this piece, which actually draws the image:
private void Form1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
if(curImage != null)
{
// Draw image using the DrawImage method
g.DrawImage(curImage,
AutoScrollPosition.X,
AutoScrollPosition.Y,
curImage.Width,
curImage.Height );
}
}
The logic checks whether the current image is null or not. If it is not null, the DrawImage method of the Graphics class is given the curImage as the Image to be loaded. The Graphics object is obtained from the Graphics property of PaintEventArgs object. Next is the handler for rotating the image; it is embedded in the handler for the mnu90rotate menu item. Here is the code:
// Rotate 90 degrees
private void mnu90rotate_Click(object sender,
System.EventArgs e)
{
if(curImage != null)
{
curImage.RotateFlip(
RotateFlipType.Rotate90FlipNone);
Invalidate();
}
}
The image is rotated using the RotateFlip method of the Image class. Since curImage is of the Image class type, the method can be directly called on the Image object. Next it calls the Invalidate() method to redraw the image.
Last comes the flip logic. It is embedded in the handler for the mnuXflip menu item.
// Flip X
private void mnuXflip_Click(object sender,
System.EventArgs e)
{
if(curImage != null)
{
curImage.RotateFlip(
RotateFlipType.RotateNoneFlipX);
Invalidate();
}
}
That's all for the example. This sets up the stage for image manipulation using GDI+ in C#. However certain aspects of the problem of image manipulation have been left out, including saving the image and how GDI+ works. These will be the topics that will be tackled in the next part of this series. Till then...
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |