ASP.NET images: Manipulation
page 1 of 1
Published: 24 Mar 2004
Unedited - Community Contributed
Abstract
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.
by Chris Garrett
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 44811/ 49

ASP.NET images: Manipulation

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

 



User Comments

Title: pandora bracelets   
Name: pandora bracelets
Date: 2011-11-26 8:59:23 PM
Comment:
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
Title: Service Unavailable   
Name: Service Unavailable
Date: 2009-06-22 4:10:04 PM
Comment:
Service Unavailable

HTTP Error 503. The service is unavailable.
Title: Image cropping   
Name: Mehul
Date: 2009-04-23 1:44:11 AM
Comment:
hey this was nice but i need a program in c graphics to crop a image so can u please help me by uploding the same here....Thanks in advance....
Title: Image Rotation   
Name: NagiReddy
Date: 2008-01-07 2:27:16 AM
Comment:
Hi Chris,
the topic is good but it lack some description and code readability.

amiably,
reddy
Title: Thanks a lot   
Name: Kaz
Date: 2007-08-22 1:28:22 PM
Comment:
Thank you very much. I had been looking for a complete example like this for a while. I didn't need great quality after rotating, so it was fine.
Title: poor quality after rotating   
Name: Nikolas
Date: 2006-04-12 12:17:13 PM
Comment:
I have the same problem with the quality of the image after rotating it a few times. The more times I rotate it the more I lose quality of the image. Anyone knows why this is happening and how can I prevent it?

Thanks in advance.
Title: Poor quality   
Name: Justin
Date: 2005-03-02 1:53:40 PM
Comment:
Hi,

This function is great but there is some quality lost from the original pic. Is that because it is being converted from Image to Graphics to Bitmap? Is there a way to prevent the degradation?

Or is it just because it is using response.output?

Also can this be saved directly to a file without displaying to the screen?

Thanks,

Justin
Title: i see now   
Name: Oli
Date: 2004-06-21 9:05:09 PM
Comment:
sorry i see how now :D my bad..
Title: where's crop?   
Name: Oli Warner
Date: 2004-06-21 9:03:14 PM
Comment:
Chris I love your tutorials on the subject but I need crop kinda urgently :D looking foreward as to when it comes out.

Oli

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-12-16 9:55:33 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search