Monday 16 April 2012

ROTATE IMAGE IN ASP.NET


The image can be rotated in a desired degree using the Namespace System.Drawing in C#. The method that will be used is RotateFlip which has various rotatefliptypes from 90, 180, 270, none. In the below example, I have used the 90 degree rotation. The rotated image will be stored in the same path with the same name using the Save method.
















In aspx page,

<asp:ImageButton ID="ImageButton" runat="server" ImageUrl="~/images/boat_nature.jpg" Width="300px" Height="300px" /><br /><br />
<asp:Button ID="RotateButton" runat="server" Text="Rotate" onclick="Button1_Click" />

 
In aspx.cs page

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

        // create image from the image url
        System.Drawing.Image img = System.Drawing.Image.FromFile(path);

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

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

        // release Image File
        img.Dispose();

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