Thumbnails and Zooming with GDI+ and C# - Image Manipulation in the Real World, continued
(Page 4 of 4 )
Next comes the handler for mnu200Zoom. But before that certain things have to be done. First add the following line to the class at the application level:
private double curZoom = 1.0;
Then the mnuLoad (from previous part) has to be changed slightly. The added code is shown in bold:
private void mnuLoad_Click(object sender,
System.EventArgs e)
{
//Change the AutoScrollMinSize property
this.AutoScrollMinSize = new Size
((int)(curImage.Width * curZoom),
(int)(curImage.Height * curZoom)); // 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 added code multiples the image width and height with the zoom factor to render an image with the appropriate zoom setting. Next the paint event handler has to be changed. The DrawImage() method has to be changed into the following:
g.DrawImage(curImage, new Rectangle
(this.AutoScrollPosition.X,
this.AutoScrollPosition.Y,
(int)(curRect.Width * curZoom),
(int)(curRect.Height * curZoom)));
The image should have the correct height and width according to the zoom factor. For that the current width and height is multiplied with the current zoom factor represented by curZoom variable. The last step in zooming is the event handler for mnu200Zoom:
private void mnu200_Click(object sender,
System.EventArgs e)
{
if(curImage != null)
{
curZoom = (double)200/100;
Invalidate();
}
}
Finally we have the event handler for the mnuThumbNail:
private void mnuThumbNail_Click(object sender,
System.EventArgs e)
{
if(curImage != null)
{
// Callback
Image.GetThumbnailImageAbort tnCallBack =
new Image.GetThumbnailImageAbort(tnCallbackMethod);
// Get the thumbnail image
Image thumbNailImage = curImage.GetThumbnailImage
(100, 100, tnCallBack, IntPtr.Zero);
// Create a Graphics object
Graphics tmpg = this.CreateGraphics();
tmpg.Clear(this.BackColor);
// Draw thumbnail image
tmpg.DrawImage(thumbNailImage, 40, 20);
// Dispose of Graphics object
tmpg.Dispose();
}
}
// Must be called, but not used
public bool tnCallbackMethod()
{
return false;
}
It first creates a variable of type GetThumbnailImageAbort and assigns the tnCallbackMethod() to it by passing the method to the GetThumbnailImageAbort. Then it creates a new instance of the Image class to hold the image returned by the GetThumbnailImage method, which is then used to draw the thumbnail onto the screen.
This brings us to the end of this discussion. In this part I discussed more advanced features of GDI+. I will continue along the same lines in next part. 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. |