Rotate an Image in ASP.NET using VB.NET

This article will demonstrate you how to rotate images in ASP.NET.
  • 14321

ASP.NET Image Rotation

This article will demonstrate you how to rotate images in ASP.NET. First of all we load the image into memory, rotate it, and then overwrite the old image by saving the new rotated image in its place of it.

Insert an image and then add a button control to rotate the image. Now double click the button control to generate the button click event method and open the Default.aspx.vb code behind file for editing. Add the following code in the Button1_Click event method.

Default.aspx.vb

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Partial Public Class _Default
 Inherits System.Web.UI.Page
 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
   Dim strFilename As String
   Dim i As System.Drawing.Image
   strFilename = Server.MapPath("images.jpg")
       i = System.Drawing.Image.FromFile(strFilename)
       Dim b As New System.Drawing.Bitmap(i.Width, iHeight, PixelFormat.Format24bppRgb)
       Dim g As Graphics = Graphics.FromImage(b)

       ' blank the image  
 
      g.Clear(Color.Blue)

       ' rotated 90 degrees and flipped on x axis  
 
      i.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipX)
       g.DrawImage(i, New Point(0, 0))
       i.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipY)

       ' Set the content type 
      
Response.ContentType = "images.jpg"

       ' send the image to the viewer  
       b.Save(Response.OutputStream, ImageFormat.Jpeg)

       ' tidy up  
       b.Dispose()
  End Sub
End Class

The Default.aspx source looks like this:

<%@ Page Language="vb" AutoEventWireup="true"  CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
  <title>Image Rotation</title>
</head>
<
body> 
 <form id="form1" runat="server">
   <div> 
     <asp:Image ID="Image1" runat="server" Height="200px" ImageUrl="~/images.jpg" 
         Width="200px" />
    
 <asp:Button ID="Button1" runat="server" Text="Rotate" onclick="Button1_Click" 
         Font-Bold="True" Font-Names="Verdana" />   
   </div> 
 </form>
</
body>
</html>

Output
image1.gif

Now click on the rotate button and the image will look like as:

image1.gif

Hope you will like this.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.