How to rotate images in Asp.net


Here I will show you how you can rotate image in web page in asp.net.

1.gif 

After clicking on button it will rotate and shown as:
 
2.gif

.aspx code:

<asp:Image  ImageUrl="~/H.GIF" ID="Image1" runat="server" />
<asp:Button ID="Button1" runat="server"  Text="Button" onclick="Button1_Click2" />

Here I have taken one Image Control and One button.

.aspx.cs code (code- behind page)

protected void Button1_Click2(object sender, EventArgs e)
{
    // get the full path of image url
    string path = Server.MapPath(Image1.ImageUrl) ;

    // creating image from the image url
    System.Drawing.Image i = System.Drawing.Image.FromFile(path);

    // rotate Image 90' Degree
    i.RotateFlip(RotateFlipType.Rotate90FlipXY);

    // save it to its actual path
    i.Save(path);

    // release Image File
    i.Dispose();

    // Set Image Control Attribute property to new image(but its old path)
    Image1.Attributes.Add("ImageUrl", path);
}

Hope you understand it.

Thank you.

Up Next
    Ebook Download
    View all
    Learn
    View all