Tuesday, October 18, 2011

Rotation in asp.net with c#

lib
----
using System.Drawing;


Default.aspx
-------------

 <asp:Image ID="Image1" runat="server" Height="297px"
            ImageUrl="~/images/Winter.jpg" Width="343px" />

<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            Text="Rotate Image" />


Default.aspx.cs
----------------

   protected void Button1_Click(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);

    }

No comments:

Post a Comment