Click here to Skip to main content
13,735,393 members
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 30 Dec 2017
Licenced CPOL

Resize and Rotate Shapes in GDI+

, 30 Dec 2017
Rate this:
Please Sign up or sign in to vote.
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.


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


You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
31-Dec-17 23:12
member31-Dec-17 23:12 
GeneralRe: My vote of 5 Pin
Andy De Filippo1-Jan-18 9:00
memberAndy De Filippo1-Jan-18 9:00 
GeneralRe: My vote of 5 Pin
2-Jan-18 15:06
member2-Jan-18 15: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180920.1 | Last Updated 30 Dec 2017
Article Copyright 2017 by Andy De Filippo
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid