Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to rotate an image in Visual Basic.Net

Author: DevASP
Download Source Code : 391_Rotate Image.zip

This article is about how you can rotate an image at your desire angle. In this article we will use the system.math namespace to rotate image on desired angle.

  • Steps you will do.
  • Start visual studio and create a new window application. 
  • Drop a text field on the form and set its following properties.

 

Text = “30”

Name = “txtAngle”

  • Drop a button control on the form and set its following properties.

 

Name = “btnRotate”

Text = “Rotate”

  •  Drop two picture box control on the form and set their properties as below.

 

First picture box.

Name = “picSource”

Image = “Browse and give any image”

Modifiers = “Friend”

Sizemode = “Autosize”

 

 

Second picture box.

Name = “picDest”

Modifiers = “Friend”

Sizemode = “Autosize”

 

  •  Drop a label control on the form and set its following properties.

 

Text = “Angle”
 
  • Open your code window and import system.math namespace as below.

Imports System.Math

 

  •  Now in the click event of the button write following code to rotate the image at your desire angle.

Dim bm_in As New Bitmap(picSource.Image)

Dim wid As Single = bm_in.Width

Dim hgt As Single = bm_in.Height

Dim corners As Point() = { _

New Point(0, 0), _

New Point(wid, 0), _

New Point(0, hgt), _

New Point(wid, hgt)}

Dim cx As Single = wid / 2

Dim cy As Single = hgt / 2

Dim i As Long

For i = 0 To 3

corners(i).X -= cx

corners(i).Y -= cy

Next i

Dim theta As Single = Single.Parse(txtAngle.Text) * PI / 180.0

Dim sin_theta As Single = Sin(theta)

Dim cos_theta As Single = Cos(theta)

Dim X As Single

Dim Y As Single

For i = 0 To 3

X = corners(i).X

Y = corners(i).Y

corners(i).X = X * cos_theta + Y * sin_theta

corners(i).Y = -X * sin_theta + Y * cos_theta

Next i

Dim xmin As Single = corners(0).X

Dim ymin As Single = corners(0).Y

For i = 1 To 3

If xmin > corners(i).X Then xmin = corners(i).X

If ymin > corners(i).Y Then ymin = corners(i).Y

Next i

For i = 0 To 3

corners(i).X -= xmin

corners(i).Y -= ymin

Next i

Dim bm_out As New Bitmap(CInt(-2 * xmin), CInt(-2 * ymin))

Dim gr_out As Graphics = Graphics.FromImage(bm_out)

ReDim Preserve corners(2)

gr_out.DrawImage(bm_in, corners)

picDest.Image = bm_out

Article Comments
It's good

Posted on 7/25/2006 12:40:26 AM by MuthuKumar

Hello,
I need help about rotate an image in vb.net. I tested a piece of code was posted in this forum, but, it works well only in the first time. I saved the rezult of the first time in a bitmap and then I run this operation again and again. Any time I run it again, the rezult picture was been bigger and bigger while the orginal picture was in a same size, but the background was be bigger and bigger.
Can any help me?
I send below the code I used to rotate the bitmap. In the beggining of the code I get the width and the height of the bitmap. If you will run this code again and again You will see that the width and the height of the bitmap will be bigger and bigger.
Thanks,
Yocheved

bm_in was initted in the load event on the form like this:
dim bm_in as bitmap = new bitmap("d:\temp\pic1.jpg")
after it I run the code below on bm_in again and again.

' Make an array of points defining the
' image's corners.
Dim wid As Single = bm_in.Width
Dim hgt As Single = bm_in.Height
Dim corners As Point() = { _
New Point(0, 0), _
New Point(wid, 0), _
New Point(0, hgt), _
New Point(wid, hgt)}

' Translate to center the bounding box at the origin.
Dim cx As Single = wid / 2
Dim cy As Single = hgt / 2
Dim i As Long
For i = 0 To 3
corners(i).X -= cx
corners(i).Y -= cy
Next i

' Rotate.
Dim theta As Single = Single.Parse(30) * Math.PI _
/ 180.0
Dim sin_theta As Single = Math.Sin(theta)
Dim cos_theta As Single = Math.Cos(theta)
Dim X As Single
Dim Y As Single
For i = 0 To 3
X = corners(i).X
Y = corners(i).Y
corners(i).X = X * cos_theta + Y * sin_theta
corners(i).Y = -X * sin_theta + Y * cos_theta
Next i

' Translate so X >= 0 and Y >=0 for all corners.
Di

Posted on 3/12/2007 2:29:23 AM by Yocheved

Fantastic, finally i found what i have been looking for, thanks heaps!

Posted on 3/19/2007 6:29:25 PM by curseddagger

Thanks for the code, it is great

Posted on 5/3/2007 12:41:32 AM by Colin Ong

Well done!Thank you for this informations.

Posted on 5/7/2007 8:17:46 AM by Noob1000

Question.

Been trying to find an answer for awhile. Your code is the closest. Only issue is I am working with the .net compact frameworks and it won't accet the following code:

"gr_out.DrawImage(bm_in, corners)"

Error reads, "Overload resolution failed because no accessible 'DrawImage' accepts this number of arguments."

This happens only with the .net compact framework and smart device apps. Please help.
Thanks.

Posted on 9/13/2007 10:40:44 PM by Ronald Wells

I was looking for something like this. Thank you so much

Posted on 11/17/2007 3:20:45 PM by L0wger

Works precisely how I wanted it to! Thanks!

Posted on 1/2/2008 9:54:53 PM by ShipsBridge

wow, thats excellent code, keep it up

Posted on 1/20/2008 3:30:05 PM by uma

This is excellent - works a treat!

Posted on 9/2/2008 3:52:30 AM by Pete

how to rotate the image in Asp.net with C#

Posted on 3/21/2010 2:36:18 AM by Ram

excellent

Posted on 5/27/2010 12:48:04 PM by naderenator

To Yocheved:

If you rotate a bitmap the picture has to become bigger. You cant rotate a square inside another square of the same size, so the outer square (the bounds of your image) have to enlarge to accompany the change of the inner square (the image itself).

If you rotate a square clockwise, for example, the top left corner raises up, the bottom left corner move farther left, the bottom right corner moves down, and the top right corner moves right. Since a bitmap is saved only as a square that its edges face vertical and horizontal, it compensates and increases the image size every time you rotate (unless its a perfect square and your rotation is in increments of 90 degrees)

Posted on 6/28/2010 12:36:09 PM by rockwell

Dim Word As String = "Youssof"
Dim FontType As String = "Arial"
Dim FontSize As Integer = 12
Dim Angle As Integer = 90
Dim G As Graphics = PictureBox1.CreateGraphics

Dim X As Single = 30
Dim Y As Single = 50



Dim WordDimention As Drawing.SizeF = G.MeasureString(Word, New Font(FontType, FontSize))

Dim Width As Single = WordDimention.Width

Dim Height As Single = WordDimention.Height



' Set the StringFormat to center the text.

Dim string_format As New StringFormat

string_format.Alignment = StringAlignment.Center

string_format.LineAlignment = StringAlignment.Center


Dim layout_rect As New RectangleF(X, Y, Width, Height)

Dim InitialState As Drawing2D.GraphicsState = G.Save()

G.DrawString(Word, New Font(FontType, FontSize), Brushes.Black, layout_rect, string_format)



' Translate to the origin, rotate, and translate back.

G.TranslateTransform(-Width \ 2 - X, -Height \ 2 - Y, MatrixOrder.Append)

G.RotateTransform(Angle, MatrixOrder.Append)

G.TranslateTransform(Width \ 2 + X, Height \ 2 + Y, MatrixOrder.Append)



' Draw the text and layout rectangle.

G.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit

G.DrawString(Word, New Font(FontType, FontSize), Brushes.Black, layout_rect, string_format)

G.Restore(InitialState)

Posted on 8/10/2010 4:04:28 AM by Youssof

To Youssof
your method rotates a text not an image
but it is very interesting too!

Posted on 8/12/2010 9:37:37 AM by Dalia

Hi there,

I need same rotating from video (mediaplayer or another player), can we do?
Thnx.

Posted on 11/28/2010 7:37:12 AM by ekalo

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Create Splash Screen using VB.Net

Disclaimer - Privacy
© 2002-2017 DevASP.net