Rotate an Image in ASP.NET using VB.NET

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

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.


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  

       ' rotated 90 degrees and flipped on x axis  
       g.DrawImage(i, New Point(0, 0))

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

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

       ' tidy up  
  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" "" 
<html xmlns="" >
<head id="Head1" runat="server">
  <title>Image Rotation</title>
 <form id="form1" runat="server">
     <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" />   


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


Hope you will like this.


More Articles

© 2020 DotNetHeaven. All rights reserved.