Click here to Skip to main content
15,881,248 members
Articles / Multimedia / GDI+
Tip/Trick

Resize and Rotate Shapes in GDI+

Rate me:
Please Sign up or sign in to vote.
4.97/5 (12 votes)
9 Dec 2021CPOL2 min read 27.4K   2.7K   24   13
Draw a shape-like editor implementing the use of anchors to resize and rotate a selection rectangle

Introduction

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

Background

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.

VB.NET
' 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.

VB.NET
' 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, _
                                              _rcRect.Height)

        ' 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.

History

  • First version submitted on December 30th, 2017
  • Bug fixed submitted on December 9th, 2021

License

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


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

Projects:
http://www.labeljoy.com
http://www.newslettercreator.com

Comments and Discussions

 
PraiseNice Job Pin
sgf 20219-Nov-23 21:35
sgf 20219-Nov-23 21:35 
QuestionZoom Feature Pin
Member 803575228-Sep-22 5:51
Member 803575228-Sep-22 5:51 
AnswerRe: Zoom Feature Pin
Andy De Filippo30-Sep-22 0:37
Andy De Filippo30-Sep-22 0:37 
GeneralRe: Zoom Feature Pin
Member 80357524-Oct-22 19:41
Member 80357524-Oct-22 19:41 
PraiseJump to TopLeft corner Pin
Member 1279396130-Nov-21 23:42
Member 1279396130-Nov-21 23:42 
GeneralRe: Jump to TopLeft corner Pin
Andy De Filippo8-Dec-21 0:41
Andy De Filippo8-Dec-21 0:41 
SuggestionGreat, very instructive Pin
Klaus Ruttkowski19-Feb-21 2:16
Klaus Ruttkowski19-Feb-21 2:16 
QuestionWould be nice to have this in C#... Pin
Shwing14-Nov-19 9:33
Shwing14-Nov-19 9:33 
Is there any way to get this converted to C#?
AnswerRe: Would be nice to have this in C#... Pin
Andy De Filippo17-Nov-19 2:19
Andy De Filippo17-Nov-19 2:19 
AnswerRe: Would be nice to have this in C#... Pin
Southmountain2-Jun-22 14:57
Southmountain2-Jun-22 14:57 
GeneralMy vote of 5 Pin
Member 1306679331-Dec-17 23:12
Member 1306679331-Dec-17 23:12 
GeneralRe: My vote of 5 Pin
Andy De Filippo1-Jan-18 9:00
Andy De Filippo1-Jan-18 9:00 
GeneralRe: My vote of 5 Pin
Member 130667932-Jan-18 15:06
Member 130667932-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.