Click here to Skip to main content
15,917,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code to rotate a picturebox. How can I make this work for any selected picturebox?

VB
Dim bm_in As New Bitmap(PictureBox9.Image)
        ' 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(5) * 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
        ' Translate so X >= 0 and Y >=0 for all corners.
        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
        ' Create an output Bitmap and Graphics object.
        Dim bm_out As New Bitmap(CInt(-2 * xmin), CInt(-2 * ymin))
        Dim gr_out As Graphics = Graphics.FromImage(bm_out)
        ' Drop the last corner lest we confuse DrawImage,
        ' which expects an array of three corners.
        ReDim Preserve corners(2)
        ' Draw the result onto the output Bitmap.
        gr_out.DrawImage(bm_in, corners)
        ' Display the result.
        PictureBox9.Image = bm_out
Posted
Comments
wiswalld 26-Jan-11 10:22am    
Sorry for the picturebox rotate I need to rotate the image. The first line is the problem. I can't figure out how to code that part. Drawing a complete blank

You're not rotating a picturebox, you're rotating the bitmap displayed in the picturebox. All you have to do is wrpa this code in a Function that returns a Bitmap instead of "Display the result...in a picturebox".

When the result is returned back to the called, you just set the PictureBox.Image property to it.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Jan-11 11:20am    
Good note - a 5.
Whats the issue with it?
The first line is the line where you need to take care of it.
Dim bm_in As New Bitmap(PictureBox9.Image)

Instead of a fixed PictureBox9, you need to use the one that is selected.

You can make the above code as a method that takes picturebox as a parameter and just pass the one that is selected.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Jan-11 11:19am    
A 5.
Put the code in a function of its own that accepts a PictureBox as a parameter. Replace "PictureBox9" in your code with the parameter. Wherever you need to do this rotation, call the function, passing the PictureBox whose image needs to be rotated.
 
Share this answer
 
Comments
wiswalld 26-Jan-11 11:01am    
I was thinking something like this but it will not work from a toolstrip

Unable to cast object of type 'System.Windows.Forms.ToolStripMenuItem' to type 'System.Windows.Forms.PictureBox'.

For Each ctrl As Control In LeftPanel1.Controls
If TypeOf ctrl Is PictureBox Then
Dim myPicBox As PictureBox = sender
'Dim bm_in As Image = sender.image

Dim bm_in As New Bitmap(myPicBox.Image)
' 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(-5) * 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
' Translate so X >= 0 and Y >=0 for all corners.
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
' Create an output Bitmap and Graphics object.
Dim bm_out As New Bitmap(CInt(-2 * xmin), CInt(-2 * ymin))
Dim gr_out As Graphics = Graphics.FromImage(bm_out)
' Drop the last corner lest we confuse DrawImage,
' which expects an array of three corners.
ReDim Preserve corners(2)
' Draw the result onto the output Bitmap.
gr_out.DrawImage(bm_in, corners)
' Display the result.
myPicBox.Image = bm_out

End If
Next
Marc A. Brown 26-Jan-11 11:10am    
Please note Dave Kreskowiak's answer to this question where he recommends creating a function that returns a bitmap object. The function would need to accept a bitmap parameter (or a System.Drawing.Image parameter for that matter, if that works better for you).
wiswalld 27-Jan-11 9:10am    
For testing purposes I have shortened the code to just rotate 90 degrees. I can not however seem to select the image.

Error - Object reference not set to an instance of an object.
on line: Dim input As New Bitmap(pb.Image)

Maybe I am going about this the wrong way. I have this function and I call it from a contextmenustrip
Rotate()

Function Rotate()
Dim ctl As Control
For Each ctl In Me.LeftPanel1.Controls
If TypeOf ctl Is PictureBox Then
Dim pb As PictureBox = CType(ctl, PictureBox)
Dim pic As Image = pb.Image
Dim wid As Integer
Dim hgt As Integer
Dim X As Integer
Dim Y As Integer
Dim input As New Bitmap(pb.Image)
wid = input.Width
hgt = input.Height
Dim output As New Bitmap(hgt, wid)

For X = 0 To wid - 1
For Y = 0 To hgt - 1
output.SetPixel(hgt - Y - 1, X, _
input.GetPixel(X, Y))
Next Y
Next X
pb.Image = output
End If
Next
End Function

Private Sub ToolStripMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem3.Click
Rotate()

End Sub
wiswalld 27-Jan-11 10:23am    
Private pb As PictureBox = Nothing
Sub RotatePicture(ByVal sender, ByVal e)
Dim ctl As Control
For Each ctl In Me.LeftPanel1.Controls
If TypeOf ctl Is PictureBox Then
'If e.Button = MouseButtons.Right Then
Dim pb As PictureBox = CType(sender, PictureBox)
'Dim pb As PictureBox = CType(sender, PictureBox)
Dim pic As Image = pb.Image
Dim wid As Integer
Dim hgt As Integer
Dim X As Integer
Dim Y As Integer
Dim input As New Bitmap(pb.Image)
wid = input.Width
hgt = input.Height
Dim output As New Bitmap(hgt, wid)

For X = 0 To wid - 1
For Y = 0 To hgt - 1
output.SetPixel(hgt - Y - 1, X, _
input.GetPixel(X, Y))
Next Y
Next X
pb.Image = output
'End If
End If
Next
End Sub

Private Sub ToolStripMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem3.Click
RotatePicture(sender, e)

End Sub

Error: Unable to cast object of type 'System.Windows.Forms.ToolStripMenuItem' to type 'System.Windows.Forms.PictureBox'.
wiswalld 27-Jan-11 10:24am    
I can't use a mousedown event as that does something else

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900