|
Question : How do I rotate a picturebox in 15 degree increments?
|
|
I have an application that allows the users to place items of furniture on a layout. They can rotate the items in 90 degree increments using code like below, when the user right clicks on the item.
picFlip.Image.RotateFlip(RotateFlipType.Rotate90FlipXY)
I am now being asked to allow rotations of 15 degree increments. I basically just get them to hold the 'Alt' key while right clicking instead. Problem is how to rotate the picture box...not just the image it holds. I understand this will still come down to rotating the image, but how can I resize the picturebox to allow for size changes when rotating rectangular items?
|
|
Answer : How do I rotate a picturebox in 15 degree increments?
|
|
True, I didn't test it, I just copied it from an accepted answer from eric37.
could you try this, I tested this and it seems to work without much degrading.
Dim bm_orig As Bitmap Dim currentrotation As Integer = 0
Private Sub Form15_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load bm_orig = PictureBox1.Image End Sub
Private Sub btnRotate_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click Dim bm_in As New Bitmap(bm_orig)
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(15 + currentrotation) * Math.PI _ / 180.0 currentrotation +=15 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
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)
PictureBox1.Image = bm_out End Sub End Class
|
|
|
|