Rotate and Flip an Image

Introduction
 
This article explains image rotation and flipping using ASP.NET. Image rotation and flipping is also called mirror image. This article will tell you about the RotateFlipType Enumerator of the System.Drawing namespace. The rotation and flipping operation can be done using Enum values that are available inside the System.Drawing Namespace.
 
Various types of enumerators for flipping and rotating images are listed below in the following table:

S. No. Member Name Description
1 Rotate180FlipNone This rotates 180 degree Clockwise but not flip the image.
2 Rotate180FlipX This rotates 180 degree Clockwise with a horizontal flip.
3 Rotate180FlipXY This rotates 180 degree Clockwise with a horizontal flip and vertical flip.
4 Rotate180FlipY This rotates 180 degree Clockwise with a vertical flip.
5 Rotate270FlipNone This rotates 270 degree Clockwise but not flip the image.
6 Rotate270FlipX This rotates 270 degree Clockwise with a horizontal flip.
7 Rotate270FlipXY This rotates 270 degree Clockwise with a horizontal flip and vertical flip.
8 Rotate270FlipY This rotates 270 degree Clockwise with a vertical flip.
9 Rotate90FlipNone This rotates 90 degree Clockwise but not flip the image.
10 Rotate90FlipX This rotates 90 degree Clockwise with a horizontal flip.
11 Rotate90FlipXY This rotates 90 degree Clockwise with a horizontal flip and vertical flip.
12 Rotate90FlipY This rotates 270 degree Clockwise with a vertical flip.
13 RotateNoneFlipNone No clockwise rotation and no flipping.
14 RotateNoneFlipX No clockwise rotation but horizontal flip.
15 RotateNoneFlipXY No clockwise rotation but horizontal and vertical flip.
16 RotateNoneFlipY No clockwise rotation but vertical flip.
 
Use the following procedure to create a simple application for flipping and rotating an image using ASP.NET. 
 
(Note: steps 1 to 5 are similar to my previous article: Control Image Brightness Using ASP.NET.)
 
Step 1: First open Visual Studio.
 
Step 2: In the menu bar click on "File" > "New Web Site" or press "Shift + Alt + N".
 
Step 3: Now a dialog box will be shown; select "ASP.NET Empty Web Site" and provide the website name (like: "FlipAndRotate") and press "OK".
 
Step 4: Now open Solution Explorer and right-click on the project and click on "Add" > "Add New Item" or simply press "Ctrl + Shift + A".
 
Step 5: A dialog will be opened. Select "Web Form" and provide the name of the Web Form (like: "FlipAndRotate.aspx") and click on the "Add" button.
 
Step 6: Now a Web Form will be in front of you and that code will be as in the following:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="FlipAndRotate.aspx.cs" Inherits="FlipAndRotate" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.         <div>  
  12.         </div>  
  13.     </form>  
  14. </body>  
  15. </html>  
Step 7: Now create your Web Form like the following:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="FlipAndRotate.aspx.cs" Inherits="FlipAndRotate" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.         <div>  
  12.             Upload an Image:<asp:FileUpload ID="FileUpload1" runat="server" />  
  13.              <asp:Button ID="btnUpload" runat="server" Text="Upload"/>  
  14.             <br />  
  15.             <asp:DropDownList ID="ddlRotateAndFlip" runat="server" AutoPostBack="True">  
  16.                 <asp:ListItem>--Select Rotation And Flip Option--</asp:ListItem>  
  17.                 <asp:ListItem>Rotates 180 degree Clockwise but not flip the image</asp:ListItem>  
  18.                 <asp:ListItem>Rotate 180 degree Clockwise with a horizontal flip</asp:ListItem>  
  19.                 <asp:ListItem>Rotate 180 degree Clockwise with a horizontal flip and vertical flip</asp:ListItem>  
  20.                 <asp:ListItem>Rotate 180 degree Clockwise with a vertical flip</asp:ListItem>  
  21.                 <asp:ListItem>Rotates 270 degree Clockwise but not flip the image</asp:ListItem>  
  22.                 <asp:ListItem>Rotate 270 degree Clockwise with a horizontal flip</asp:ListItem>  
  23.                 <asp:ListItem>Rotate 270 degree Clockwise with a horizontal flip and vertical flip</asp:ListItem>  
  24.                 <asp:ListItem>Rotate 270 degree Clockwise with a vertical flip</asp:ListItem>  
  25.                 <asp:ListItem>Rotates 90 degree Clockwise but not flip the image</asp:ListItem>  
  26.                 <asp:ListItem>Rotate 90 degree Clockwise with a horizontal flip</asp:ListItem>  
  27.                 <asp:ListItem>Rotate 90 degree Clockwise with a horizontal flip and vertical flip</asp:ListItem>  
  28.                 <asp:ListItem>Rotate 90 degree Clockwise with a vertical flip</asp:ListItem>  
  29.                 <asp:ListItem>No clockwise rotation and no flipping</asp:ListItem>  
  30.                 <asp:ListItem>No clockwise rotation but horizontal flip</asp:ListItem>  
  31.                 <asp:ListItem>No clockwise rotation but horizontal and vertical flip</asp:ListItem>  
  32.                 <asp:ListItem>No clockwise rotation but vertical flip</asp:ListItem>  
  33.             </asp:DropDownList>  
  34.             <br />  
  35.             <br />  
  36.             <asp:Image ID="Img" runat="server" />  
  37.         </div>  
  38.     </form>  
  39. </body>  
  40. </html>   
By using the code above your Web Form will become like:


 
Step 8: Now open the code behind of this Web Form and write the following code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Drawing;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8.   
  9. public partial class FlipAndRotate : System.Web.UI.Page  
  10. {  
  11.     Bitmap _current;  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {  
  14.         if (Session["filepath"] != null)  
  15.         {  
  16.             Img.ImageUrl = Session["filepath"].ToString();  
  17.             _current = (Bitmap)Bitmap.FromFile(Server.MapPath(Session["filepath"].ToString()));  
  18.         }  
  19.     }  
  20.     protected void btnUpload_Click(object sender, EventArgs e)  
  21.     {  
  22.         if (FileUpload1.HasFile)  
  23.         {  
  24.             string filePath = Server.MapPath("Images/" + FileUpload1.FileName);  
  25.             FileUpload1.SaveAs(filePath);  
  26.             Img.ImageUrl = "Images/" + FileUpload1.FileName;  
  27.             Session["filepath"] = "Images/" + FileUpload1.FileName;  
  28.         }  
  29.         else  
  30.         {  
  31.             Response.Write("Please Select The File");  
  32.         }  
  33.     }  
  34.   
  35.     protected void ddlRotateAndFlip_SelectedIndexChanged(object sender, EventArgs e)  
  36.     {  
  37.         Bitmap temp = (Bitmap)_current;  
  38.         Bitmap bmap = (Bitmap)temp.Clone();  
  39.         if (ddlRotateAndFlip.SelectedIndex == 1)  
  40.         {  
  41.             bmap.RotateFlip(RotateFlipType.Rotate180FlipNone);  
  42.         }  
  43.         else if (ddlRotateAndFlip.SelectedIndex == 2)  
  44.         {  
  45.             bmap.RotateFlip(RotateFlipType.Rotate180FlipX);  
  46.         }  
  47.         else if (ddlRotateAndFlip.SelectedIndex == 3)  
  48.         {  
  49.             bmap.RotateFlip(RotateFlipType.Rotate180FlipXY);  
  50.         }  
  51.         else if (ddlRotateAndFlip.SelectedIndex == 4)  
  52.         {  
  53.             bmap.RotateFlip(RotateFlipType.Rotate180FlipY);  
  54.         }  
  55.         else if (ddlRotateAndFlip.SelectedIndex == 5)  
  56.         {  
  57.             bmap.RotateFlip(RotateFlipType.Rotate270FlipNone);  
  58.         }  
  59.         else if (ddlRotateAndFlip.SelectedIndex == 6)  
  60.         {  
  61.             bmap.RotateFlip(RotateFlipType.Rotate270FlipX);  
  62.         }  
  63.         else if (ddlRotateAndFlip.SelectedIndex == 7)  
  64.         {  
  65.             bmap.RotateFlip(RotateFlipType.Rotate270FlipXY);  
  66.         }  
  67.         else if (ddlRotateAndFlip.SelectedIndex == 8)  
  68.         {  
  69.             bmap.RotateFlip(RotateFlipType.Rotate270FlipY);  
  70.         }  
  71.         else if (ddlRotateAndFlip.SelectedIndex == 9)  
  72.         {  
  73.             bmap.RotateFlip(RotateFlipType.Rotate90FlipNone);  
  74.         }  
  75.         else if (ddlRotateAndFlip.SelectedIndex == 10)  
  76.         {  
  77.             bmap.RotateFlip(RotateFlipType.Rotate90FlipX);  
  78.         }  
  79.         else if (ddlRotateAndFlip.SelectedIndex == 11)  
  80.         {  
  81.             bmap.RotateFlip(RotateFlipType.Rotate90FlipXY);  
  82.         }  
  83.         else if (ddlRotateAndFlip.SelectedIndex == 12)  
  84.         {  
  85.             bmap.RotateFlip(RotateFlipType.Rotate90FlipY);  
  86.         }  
  87.         else if (ddlRotateAndFlip.SelectedIndex == 13)  
  88.         {  
  89.             bmap.RotateFlip(RotateFlipType.RotateNoneFlipNone);  
  90.         }  
  91.         else if (ddlRotateAndFlip.SelectedIndex == 14)  
  92.         {  
  93.             bmap.RotateFlip(RotateFlipType.RotateNoneFlipX);  
  94.         }  
  95.         else if (ddlRotateAndFlip.SelectedIndex == 15)  
  96.         {  
  97.             bmap.RotateFlip(RotateFlipType.RotateNoneFlipXY);  
  98.         }  
  99.         else if (ddlRotateAndFlip.SelectedIndex == 16)  
  100.         {  
  101.             bmap.RotateFlip(RotateFlipType.RotateNoneFlipY);  
  102.         }  
  103.         else  
  104.         {  
  105.             Response.Write("<script>alert('Invalid Selection')</script>");  
  106.         }  
  107.         _current = (Bitmap)bmap.Clone();  
  108.         Random rnd = new Random();  
  109.         int a = rnd.Next();  
  110.         _current.Save(Server.MapPath("Images/" + a + ".png"));  
  111.         Img.ImageUrl = "Images/" + a + ".png";   
  112.     }  
  113. }  
Step 9: Now run the project and the output you will get is as the following:
  1. Initially when the page loads:


  2. After Uploading and Image:



  3. When an option is choosen from the drop-down list then:


    And:


Various output for the following image are:
  1. Actual Image



  2. Outputs of Rotation and Flip



Similar Articles