Flip and Rotate Image in Any Direction in Asp.Net




Step 1: Create Webpage and Drag and Drop File Upload, Button, Image, Drop down Controls in your Webpage and Design Like Below. 

<div style='float: left; width: 550px; overflow-y: auto; height: 100px; border: 1px solid black;'>
<div>
            Upload an Image:<asp:FileUpload ID="FileUpload1" runat="server" />
            &nbsp;<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
            <br />
            <asp:DropDownList ID="ddlRotateAndFlip" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlRotateAndFlip_SelectedIndexChanged">
                <asp:ListItem Value="--Select Rotation And Flip Option--">--Select Rotation And Flip Option--</asp:ListItem>
                <asp:ListItem>Rotates 180 degree Clockwise but not flip the image</asp:ListItem>
                <asp:ListItem>Rotate 180 degree Clockwise with a horizontal flip</asp:ListItem>
                <asp:ListItem>Rotate 180 degree Clockwise with a horizontal flip and vertical flip</asp:ListItem>
                <asp:ListItem>Rotate 180 degree Clockwise with a vertical flip</asp:ListItem>
                <asp:ListItem>Rotates 270 degree Clockwise but not flip the image</asp:ListItem>
                <asp:ListItem>Rotate 270 degree Clockwise with a horizontal flip</asp:ListItem>
                <asp:ListItem>Rotate 270 degree Clockwise with a horizontal flip and vertical flip</asp:ListItem>
                <asp:ListItem>Rotate 270 degree Clockwise with a vertical flip</asp:ListItem>
                <asp:ListItem>Rotates 90 degree Clockwise but not flip the image</asp:ListItem>
                <asp:ListItem>Rotate 90 degree Clockwise with a horizontal flip</asp:ListItem>
                <asp:ListItem>Rotate 90 degree Clockwise with a horizontal flip and vertical flip</asp:ListItem>
                <asp:ListItem>Rotate 270 degree Clockwise with a vertical flip</asp:ListItem>
                <asp:ListItem>No clockwise rotation and no flipping</asp:ListItem>
                <asp:ListItem>No clockwise rotation but horizontal flip</asp:ListItem>
                <asp:ListItem>No clockwise rotation but horizontal and vertical flip</asp:ListItem>
                <asp:ListItem>No clockwise rotation but vertical flip</asp:ListItem>
            </asp:DropDownList>
            <br />
            <br />
            <asp:Image ID="Img" runat="server" Height="238px" Width="396px" />
        </div>
</div>






















Step 2: Now Code For File Upload in Save in Folder in your Webpage.

        Bitmap _current;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["filepath"] != null)
            {
                Img.ImageUrl = Session["filepath"].ToString();
                _current = (Bitmap)Bitmap.FromFile(Server.MapPath(Session["filepath"].ToString()));
            }
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string filePath = Server.MapPath("Images/" + FileUpload1.FileName);
                FileUpload1.SaveAs(filePath);
                Img.ImageUrl = "Images/" + FileUpload1.FileName;
                Session["filepath"] = "Images/" + FileUpload1.FileName;
            }
            else
            {
                Response.Write("Please Select The File");
            }
        }

Step 3: Now Create Drop down Selection Change in Code For Rotation in C#.

protected void ddlRotateAndFlip_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bitmap temp = (Bitmap)_current;
            Bitmap bmap = (Bitmap)temp.Clone();
            if (ddlRotateAndFlip.SelectedIndex == 1)
            {
                bmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
            }
            else if (ddlRotateAndFlip.SelectedIndex == 2)
            {
                bmap.RotateFlip(RotateFlipType.Rotate180FlipX);
            }
            else if (ddlRotateAndFlip.SelectedIndex == 3)
            {
                bmap.RotateFlip(RotateFlipType.Rotate180FlipXY);
            }
            else if (ddlRotateAndFlip.SelectedIndex == 4)
            {
                bmap.RotateFlip(RotateFlipType.Rotate180FlipY);
            }
            else if (ddlRotateAndFlip.SelectedIndex == 5)
            {
                bmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
            }
            else if (ddlRotateAndFlip.SelectedIndex == 6)
            {
                bmap.RotateFlip(RotateFlipType.Rotate270FlipX);
            }
            else if (ddlRotateAndFlip.SelectedIndex == 7)
            {
                bmap.RotateFlip(RotateFlipType.Rotate270FlipXY);
            }
            else if (ddlRotateAndFlip.SelectedIndex == 8)
            {
                bmap.RotateFlip(RotateFlipType.Rotate270FlipY);
            }
            else if (ddlRotateAndFlip.SelectedIndex == 9)
            {
                bmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
            }
            else if (ddlRotateAndFlip.SelectedIndex == 10)
            {
                bmap.RotateFlip(RotateFlipType.Rotate90FlipX);
            }
            else if (ddlRotateAndFlip.SelectedIndex == 11)
            {
                bmap.RotateFlip(RotateFlipType.Rotate90FlipXY);
            }
            else if (ddlRotateAndFlip.SelectedIndex == 12)
            {
                bmap.RotateFlip(RotateFlipType.Rotate90FlipY);
            }
            else if (ddlRotateAndFlip.SelectedIndex == 13)
            {
                bmap.RotateFlip(RotateFlipType.RotateNoneFlipNone);
            }
            else if (ddlRotateAndFlip.SelectedIndex == 14)
            {
                bmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
            }
            else if (ddlRotateAndFlip.SelectedIndex == 15)
            {
                bmap.RotateFlip(RotateFlipType.RotateNoneFlipXY);
            }
            else if (ddlRotateAndFlip.SelectedIndex == 16)
            {
                bmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
            }
            else
            {
                // Response.Write("<script>alert('Invalid Selection')</script>");
            }
            _current = (Bitmap)bmap.Clone();
            Random rnd = new Random();
            int a = rnd.Next();
            _current.Save(Server.MapPath("Images/" + a + ".png"));
            Img.ImageUrl = "Images/" + a + ".png";
        }






















Step 4: Now run your Webpage and upload File and Select Option from Dropdown and See Image its Rotating.

Related Posts

Previous
Next Post »

Thanks for comments.....