Thumbnails and Zooming with GDI+ and C# - Image Manipulation in the Real World
(Page 3 of 4 )
It is time to put the theory into practice. I will do this by enhancing the application developed in the last part by adding the following functionalities:
- Save the image in the format specified by user.
- Zoom in according to the percentage selected from the menu.
- Create a thumbnail of a loaded image.
The menus corresponding to the operations are:
- mnuSave – the submenu of File menu to save the image.
- mnu200Zoom – zooms the image by 200%.
- mnuThumbNail- creates a thumbnail of the image.
Here is the method that handles the click event of mnuSave:
private void mnuSave_Click(object sender,
System.EventArgs e)
{
// If image is created
if(curImage == null)
return;
// Call SaveFileDialog
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Title = "Save Image As";
saveDlg.OverwritePrompt = true;
saveDlg.CheckPathExists = true;
saveDlg.Filter =
"Bitmap File(*.bmp)|*.bmp|" +
"Gif File(*.gif)|*.gif|" +
"JPEG File(*.jpg)|*.jpg|" +
"PNG File(*.png)|*.png" ;
saveDlg.ShowHelp = true;
// If selected, save
if(saveDlg.ShowDialog() == DialogResult.OK)
{
// Get the user-selected file name
string fileName = saveDlg.FileName;
// Get the extension
string strFilExtn =
fileName.Remove(0, fileName.Length - 3);
// Save file
switch(strFilExtn)
{
case "bmp":
curImage.Save(fileName, ImageFormat.Bmp);
break;
case "jpg":
curImage.Save(fileName, ImageFormat.Jpeg);
break;
case "gif":
curImage.Save(fileName, ImageFormat.Gif);
break;
case "tif":
curImage.Save(fileName, ImageFormat.Tiff);
break;
case "png":
curImage.Save(fileName, ImageFormat.Png);
break;
default:
break;
}
}
}
First the save dialog box is shown with acceptable extensions. Then, from the file name returned by the dialog box, the extension is retrieved. Finally, according to the extension, the Save() method is called with the corresponding image format parameter.
Next: Image Manipulation in the Real World, continued >>
More C# Articles
More By A.P.Rajshekhar