More Examples of Simplified Image Processing in GDI+ - Sample Eight-Zooming in and out
(Page 4 of 4 )
In this sample, we will continue to work with the DrawImage() function, but to achieve the zooming in and out effect. First, let us quickly examine the signature of this function, as follows:
// Summary:
// Draws the specified portion of the specified System.Drawing.Image at the
// specified location and with the specified size.
// Parameters:
// image:
// System.Drawing.Image to draw.
// destRect:
// System.Drawing.Rectangle structure that specifies the location and size of
// the drawn image. The image is scaled to fit the rectangle.
// srcX:
// The x-coordinate of the upper-left corner of the portion of the source image
// to draw.
// srcY:
// The y-coordinate of the upper-left corner of the portion of the source image
// to draw.
// srcWidth:
// Width of the portion of the source image to draw.
// srcHeight:
// Height of the portion of the source image to draw.
// srcUnit:
// Member of the System.Drawing.GraphicsUnit enumeration that specifies the
// units of measure used to determine the source rectangle.
// Exceptions:
// System.ArgumentNullException:
// image is null.
public void DrawImage(Image image, Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, GraphicsUnit srcUnit);
Note that the source area and the target area of the DrawImage() function are irrelevant. If the source area is bigger the target area, invoking the DrawImage() function will result in a zooming out result; otherwise, we will see a converse zooming in effect. Now, let us start to write the application whose crucial code is listed below:
Graphics graphics=this.CreateGraphics();
graphics.Clear(Color.White);
//load the image
Bitmap image=new Bitmap("photo.bmp");
//define the area to show the image
Rectangle rect=new Rectangle(0,0,image.Width,image.Height);
graphics.DrawImage(image,rect);
//set the size of the zooming out area to 80*80
graphics.TranslateTransform(image.Width+10,0);
Rectangle smallrect=new Rectangle(0,0,80,80);
//zooming out effect
graphics.DrawImage(image,smallrect,80,10,106,112,GraphicsUnit.Pixel);
graphics.TranslateTransform(0,100);
//set the size of the zooming in area to 80*80
Rectangle largerect=new Rectangle(0,0,80,80);
//zooming in effect
graphics.DrawImage(image,largerect,56,101,35,40,GraphicsUnit.Pixel);
Now, press F5 to launch this sample, and you will see the running time snapshot as shown in Figure 9.
Figure 9-the running time snapshot to achieve the zooming in and out effects
_html_m4706ceeb.png)
Apparently, in Figure 9, the head of Gandalf is zoomed out, while the head of Aragorn is zoomed in. Regrettably, herein we have only utilized the hard-code means. However, if you continue to add mouse dragging to select certain shapes to zoom into or out from your target, that would be better.
Summary
The two most important classes in GDI+ are Image and Bitmap; they greatly simplify image processing. Moreover, to handle a Metafile file it is highly recommended that you fall back on the Metafile class. By leveraging several interpolating algorithms, you can easily control the different kinds of rendering qualities. At last, we've discussed the GetThumbnailImage() method as well as Clone() to achieve other kinds of special image effects.
| 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. |