You are on page 1of 6

How to rotate an image in Visual Basic.Net - .Net Articles & Samples http://www.devasp.net/net/articles/display/391.

html

GLP NMR Services Selcia perform GLP compliant PI NMR studies www.selcia.com

Mobile App UI & UX Design We create usable and stylish user interfaces for iOS, Android and WM innovationbox.com

Douglas Labs 25% Off Great Discounts on Douglas Labs 25% off. www.vnfnutrition.com

Search - Articles

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with
Windows Server Purchase
Home
Dev Articles How to rotate an image in Visual Basic.Net
Sample Chapters Visual FoxPro 10
Add a Listing Author: DevASP Cross platform Visual
Contact Download Source Code : 391_Rotate Image.zip FoxPro for
Windows/OS X/Linux
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 /Cloud/Mobile
Dev Articles www.lianja.com
rotate image on desired angle.
ASP.NET (502)
C# (300) Diindolylmethane
SQL Server (59) Image resizing on the fly www.aspjpeg.com Dietary ingredient
Make high-quality image thumbnails with this raw material ETI -
VB.NET (195)
ASP/ASP.NET component.
Web Services (6) bulk and laboratory
sales
www.etichem.com
Search Directory Get a Better BASIC www.powerbasic.com
Multi-Core Coding at a better price A professional
compiler @ $49 Word Processor
ASP.Net AJAX
AJAX ToolKit
Control
.NET Namespaces Use Word as control
10% OFF Radiochemicals www.arcincusa.com in Net, ASP.Net Java
Applications American Radiolabeled Chemicals High quality 14C,
without local
Articles & Samples 3H, 125I, 35S
Assembly & installation
www.pintexx.com
Controls
Community Customized Mobile Modules www.acktie.com/
Developer Sites Rapidly develop mobile apps by piecing our clean Package Design
Downloads modules together!
Integrated Design &
Hosting Services Prepress Accelerate
Introduction Speed to Market
Knowledge Base Steps you will do. www.sgsintl.com
Sample Chapters Start visual studio and create a new window application.
WebCasts Drop a text field on the form and set its following properties.
Visual Basic
ASP.NET Programming
Free Help &
Text = “30” Discussion with VB
Applications Name = “txtAngle”
Articles & Samples Pros & Experts.
ASP.Net Sites Register Today!
www.daniweb.com
Assembly & Drop a button control on the form and set its following properties .
Controls
Downloads
Errors & Bugs
Introduction Name = “btnRotate”
Knowledge Base Text = “Rotate”
Sample Chapters
WebCasts
Drop two picture box control on the form and set their properties as below.

VB.Net

Articles & Samples


First picture box.
Name = “picSource”
Downloads
Image = “Browse and give any image”
Errors, Bugs &
Modifiers = “Friend”
Fixes
Sizemode = “Autosize”
Introduction
Knowledge Base
Sample Chapters
Second picture box.
WebCasts Name = “picDest”
Modifiers = “Friend”
C-Sharp Sizemode = “Autosize”

Applications
Articles & Samples
C-Sharp Sites Drop a label control on the form and set its following properties.
Errors, Bugs &
Fixes
Introduction
Text = “Angle”
Knowledge Base
Sample Chapters
WebCasts
Open your code window and import system.math namespace as below.
SQL Server

Applications Imports System.Math


Articles & Samples
SQL Sites
Downloads
Errors, Bugs &
Fixes Now in the click event of the button write following code to rotate the image at your desire angle.
Introduction
Knowledge Base
Sample Chapters
WebCasts

1 of 6 13/07/2012 11:54 AM
How to rotate an image in Visual Basic.Net - .Net Articles & Samples http://www.devasp.net/net/articles/display/391.html

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

MuthuKumar It's good

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

Yocheved Hello,

2 of 6 13/07/2012 11:54 AM
How to rotate an image in Visual Basic.Net - .Net Articles & Samples http://www.devasp.net/net/articles/display/391.html

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

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

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

Colin Ong Thanks for the code, it is great

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

Noob1000 Well done!Thank you for this informations.

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

Ronald Wells 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)"

3 of 6 13/07/2012 11:54 AM
How to rotate an image in Visual Basic.Net - .Net Articles & Samples http://www.devasp.net/net/articles/display/391.html

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

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

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

ShipsBridge Works precisely how I wanted it to! Thanks!

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

uma wow, thats excellent code, keep it up

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

Pete This is excellent - works a treat!

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

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

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

naderenator excellent

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

rockwell 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

Youssof 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))

4 of 6 13/07/2012 11:54 AM
How to rotate an image in Visual Basic.Net - .Net Articles & Samples http://www.devasp.net/net/articles/display/391.html

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

Dalia 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

ekalo 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 :

Send me an email about this article when other users post a Comment

5 of 6 13/07/2012 11:54 AM
How to rotate an image in Visual Basic.Net - .Net Articles & Samples http://www.devasp.net/net/articles/display/391.html

<< Create Splash Screen using VB.Net

Serializing & Deserializing Objects Using Standard and Binary Formatter in VB.Net >>

Disclaimer - Privacy
© 2002-2012 DevASP.net

6 of 6 13/07/2012 11:54 AM

You might also like