|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionHow many times have you wanted to rotate an image in a This control makes it simple to rotate your images. Referencing it is easy in code - the namespace is Properties/MethodsThis control inherits everything so any property/method a
Adding the ControlRight-click on an empty area of your toolbox and choose Add/Remove Items. At the dialog screen on the .NET Framework Components tab, choose Browse and navigate to the DLL file. Once you've done this, hit OK. You've now added the control to the toolbox. Using the ControlUsing the control is simple. It works almost exactly like the Now just set your If you'd like to set a transparent color for your image - you can do that with How it worksAll of the rotation is handled in the Dim bm_in As New Bitmap(MyBase.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)}
Next I grab the center of the image - the point we want to rotate around - and subtract from each of the corners. 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
Now we need to get the Sine of theta, and the Cosine of theta - which means we need theta. Dim theta As Single = CSng((_degree) * _direction) * PI / 180
Dim sin_theta As Single = Sin(theta)
Dim cos_theta As Single = Cos(theta)
Here is where the magic comes in, we need to apply the rotation formulas to all the corners. 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 = (Y * cos_theta) + (X * sin_theta)
Next
OK, we've got the rotated corners, let's fix the offset we created when finding the rotation point, and for that we'll need the minimum x and y values. 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
For i = 0 To 3
corners(i).X -= xmin
corners(i).Y -= ymin
Next
Now we can actually output the rotated image, but first I create a region using a function to create it based on these corners. Dim bm_out As New Bitmap(CInt(-2 * xmin), CInt(-2 * ymin))
Dim bgr As Graphics = Graphics.FromImage(bm_out)
Dim rg As Region = CreateTransRegion(corners)
Dim tp As Point = corners(3)
ReDim Preserve corners(2)
bgr.DrawImage(bm_in, corners)
Now we have the rotated image in an output buffer, plus a region to allow transparency for the parts of the control that won't draw the image. Now comes the implementation of the Dim gr_out As Graphics = pe.Graphics
gr_out.FillRectangle(New SolidBrush(Me.BackColor), 0, 0, Me.Width, Me.Height)
bm_in.MakeTransparent(_transColor)
If _sizemode = PictureBoxSizeMode.StretchImage Then
Dim maxW As Integer = tp.X
Dim maxH As Integer = tp.Y
For t As Integer = 0 To 2
If maxW < corners(t).X Then maxW = corners(t).X
If maxH < corners(t).Y Then maxH = corners(t).Y
Next
'get hscale
Dim hscale As Double = Me.Width / maxW
'get vscale
Dim vscale As Double = Me.Height / maxH
'convert points
corners(0) = New Point(corners(0).X * hscale, corners(0).Y * vscale)
corners(1) = New Point(corners(1).X * hscale, corners(1).Y * vscale)
corners(2) = New Point(corners(2).X * hscale, corners(2).Y * vscale)
gr_out.DrawImage(bm_out, 0, 0, Me.Width, Me.Height)
Dim np(3) As Point
np(0) = corners(0)
np(1) = corners(1)
np(2) = corners(2)
np(3) = New Point(tp.X * hscale, tp.Y * vscale)
rg = CreateTransRegion(np)
We don't need quite so much for centering the image. ElseIf _sizemode = PictureBoxSizeMode.CenterImage Then
Dim wadd As Integer = CInt((Me.Width / 2) - (bm_out.Width / 2))
Dim hadd As Integer = CInt((Me.Height / 2) - (bm_out.Height / 2))
corners(0) = New Point(corners(0).X + wadd, corners(0).Y + hadd)
corners(1) = New Point(corners(1).X + wadd, corners(1).Y + hadd)
corners(2) = New Point(corners(2).X + wadd, corners(2).Y + hadd)
gr_out.DrawImage(bm_in, corners)
Dim np(3) As Point
np(0) = corners(0)
np(1) = corners(1)
np(2) = corners(2)
np(3) = New Point(tp.X + wadd, tp.Y + hadd)
rg = CreateTransRegion(np)
Any other size modes just get output to 0,0, so we don't need anything special there, except for Else
gr_out.DrawImage(bm_in, corners)
End If
If _sizemode = PictureBoxSizeMode.AutoSize Then
MyBase.Width = bm_out.Width
MyBase.Height = bm_out.Height
End If
The last thing we take care of is the region (for transparency), but if the property Me.Region = Nothing
If _showThrough Then
Me.Region = rg
End If
And that's it! It is certainly possible to modify it to rotate around a point other than the center (although I haven't tried). Suggestions/BugsIf you want to send me any bugs/suggestions, please send them to codeproject@stdominion.net.
|
||||||||||||||||||||||