Click here to Skip to main content
15,123,727 members
Articles / Multimedia / GDI+
Posted 30 Dec 2017

Tagged as


11 bookmarked

Resize and Rotate Shapes in GDI+

Rate me:
Please Sign up or sign in to vote.
4.75/5 (4 votes)
30 Dec 2017CPOL2 min read
Draw a shape-like editor implementing the use of anchors to resize and rotate a selection rectangle


The code implements a mouse-driven shape editor that supports moving, resizing and rotating a selection rectangle via anchors, which may be used to outline any type of contents, such as images, shapes, text and so on.

Image 1


Drawing rotated elements in GDI+ is a fairly simple task. Creating a resizing mechanism via mouse movements is also rather simple. A few problems may arise when you try to resize a rotated element, due to the way GDI+ enables you to rotate a shape around a given point (usually the center).

After many days of frustrated Googling, I came up with this solution that I'm sharing here in the hope that it might help you save time.

Using the Code

The forms's mouse_ events show how to handle mouse movements and how to detect the type of operation being executed (move, resize, rotate).

All drawing takes place in the Render method which is also responsible for storing regions used in mouse events to detect where and how the mouse is being used.

When dealing with rotated object, mouse hit testing needs to be done via regions (or paths) so that the cursor location can be properly linked to the elements on screen.

During the rendering of a rotated element being resized, the key is to select the proper center origin to rotate around, which is the center of the rectangle as it was the last time it was rendered before resizing started.

' Define center origin
Dim ptCenter As PointF = _
      New PointF(_rcRect.Left + (_rcRect.Width / 2), _rcRect.Top + (_rcRect.Height / 2))

' Check for on-going resize operation
Select Case _eMouseOperation
    Case MouseOperation.Nwse To MouseOperation.We
        ' Use last known center origin
        ptCenter = _ptCenter
    Case Else
        _ptCenter = ptCenter
End Select

Also, right after rendering a rotated element, you need to compute the position of the rectangle as it would be if it were flat, calculated around the new center origin. This will be the new rectangle to be used after the resize operation is over.

' Check for rotation
If (_rgRect IsNot Nothing) AndAlso (_snAngle <> 0.0) Then

    ' Get rotatetd rectangle bounds
    Dim rfNewBounds As RectangleF = _rgRect.GetBounds(oGfx)

    ' Check for resize operation on a rotated element
    If (_eMouseOperation >= MouseOperation.Nwse) AndAlso _eMouseOperation <= MouseOperation.We Then

        ' Get center origin of the region
        Dim ptNewScreenCenterOrigin As New PointF_
          (rfNewBounds.Left + (rfNewBounds.Width / 2), rfNewBounds.Top + (rfNewBounds.Height / 2))

        ' Compute a rectangle based on source rectangle size and located around the center point
        ' of the bounds of the rotated region
        Dim rcNewRenderRect As New RectangleF((ptNewScreenCenterOrigin.X - (_rcRect.Width / 2)), _
                                              (ptNewScreenCenterOrigin.Y - (_rcRect.Height / 2)), _
                                              _rcRect.Width, _

        ' Store for mouse up
        _rcResizeMouseUpRenderRect = rcNewRenderRect

    End If

End If

Points of Interest

The method AnchorToCursor returns the most suitable cursor when the mouse is hovering over one of the anchors. The selection is processed taking into account the anchor type (cardinal point) and the current rotation angle. As it turns out, rendering a custom rotated arrow cursor can lead to bad results, so it's best to always use one of the factory arrows, even if the angle doesn't precisely match that of the rotation/cardinal point.
Microsoft Office shape editor uses the same approach.


  • First version submitted on December 30th, 2017


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


About the Author

Andy De Filippo
Software Developer (Senior)
Spain Spain
Developing Windows desktop applications since 1995.
Works for Delivery Tech Corp. San Diego - USA


Comments and Discussions

PraiseJump to TopLeft corner Pin
Member 127939611-Dec-21 0:42
MemberMember 127939611-Dec-21 0:42 
SuggestionGreat, very instructive Pin
Klaus Ruttkowski19-Feb-21 3:16
MemberKlaus Ruttkowski19-Feb-21 3:16 
I have not found a project in the handling of Resize and rotation as well as setting the corresponding cursor is so well realized.Thumbs Up | :thumbsup: Great and thank you. I have translated the project into C# and can make it available.
QuestionWould be nice to have this in C#... Pin
Shwing14-Nov-19 10:33
MemberShwing14-Nov-19 10:33 
AnswerRe: Would be nice to have this in C#... Pin
Andy De Filippo17-Nov-19 3:19
MemberAndy De Filippo17-Nov-19 3:19 
GeneralMy vote of 5 Pin
Member 130667931-Jan-18 0:12
MemberMember 130667931-Jan-18 0:12 
GeneralRe: My vote of 5 Pin
Andy De Filippo1-Jan-18 10:00
MemberAndy De Filippo1-Jan-18 10:00 
GeneralRe: My vote of 5 Pin
Member 130667932-Jan-18 16:06
MemberMember 130667932-Jan-18 16:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.