In previous parts of this tutorial we have looked at loading an image and returning the image to the users browser. The only manipulation we have done is resize it. This article shows how we can copy, rotate, crop and flip the image.
There are three new commands to show you that do all the work in this article. First up is the most important, DrawImage, which takes a source image and draws a copy of the image onto another bitmap.
DrawImage takes several parameters, the minimum being
- the source image
- point (x,y) of the destination
For the more advanced operations it requires
- source image
- rectangle of destination (x,y, width, height)
- rectangle of source (x,y, width, height)
- Graphical Unit (eg. pixel)
As you can see, there is a lot of flexibility with this already, for example you can take a portion of your source image (crop) then draw it onto the destination bitmap at a new size, or tile the image with multiple draw statements.
Rotating
You can rotate two ways (at least), one method uses RotateTransform method of the graphic object. This allows you to rotate the coordinates of the object by the number of degrees you specify.
The other option allows you specify a "flip" as well as a rotate. This time it works on your source image rather than destination. It is the RotateFlip method of the image object and takes a parameter of type System.Drawing.RotateFlip
See it in action
/chrisg/tools/rotateimage.aspx
View Source
/chrisg/tools/view-rotateimage.aspx.asp
Download Source Files
/chrisg/tools/download.asp?file=rotate.zip