Changeset 119 for trunk/Snippets


Ignore:
Timestamp:
Sep 9, 2012, 6:41:33 AM (10 years ago)
Author:
roman
Message:

Added PreserveAspectRatio? property

Location:
trunk/Snippets/CropImage
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Snippets/CropImage/ImageCropBox.vb

    r111 r119  
    1111    Private _MinimalSelectionSize As Size
    1212    Private _SelectionRectangle As Rectangle
     13    Private _PreserveAspectRatio As Boolean = False
    1314    Private ClientImageOrigin As Point
    1415    Private ClientImageExtent As Size
     
    3031        ' Add any initialization after the InitializeComponent() call.
    3132        _MinimalSelectionSize = New Size(0, 0)
    32     End Sub
     33        _PreserveAspectRatio = False
     34    End Sub
     35
     36    Private Function ApplyMinimalSelection(ByVal SelectionRectangle As Rectangle) As Rectangle
     37        If SelectionRectangle.Width < MinimalSelectionSize.Width Then
     38            SelectionRectangle.Width = MinimalSelectionSize.Width
     39            If _Image IsNot Nothing AndAlso SelectionRectangle.Right > _Image.Width Then SelectionRectangle.X = Math.Max(0, _Image.Width - SelectionRectangle.Width)
     40        End If
     41        If SelectionRectangle.Height < MinimalSelectionSize.Height Then
     42            SelectionRectangle.Height = MinimalSelectionSize.Height
     43            If _Image IsNot Nothing AndAlso SelectionRectangle.Bottom > _Image.Height Then SelectionRectangle.Y = Math.Max(0, _Image.Height - SelectionRectangle.Height)
     44        End If
     45        ApplyMinimalSelection = SelectionRectangle
     46    End Function
     47    Private Function ApplyAspectRatio(ByVal SelectionRectangle As Rectangle, ByRef AdjustedSelectionRectangle As Rectangle) As Boolean
     48        AdjustedSelectionRectangle = SelectionRectangle
     49        ApplyAspectRatio = False
     50        If Not _PreserveAspectRatio Then Exit Function
     51        If _Image Is Nothing Then Exit Function
     52        Debug.Assert(_Image.Height > 0 AndAlso _Image.Width > 0)
     53        If SelectionRectangle.Width <= 0 Or SelectionRectangle.Height <= 0 Then Exit Function
     54        Dim AdjustedHeight As Integer = Math.Round(SelectionRectangle.Width * _Image.Height / _Image.Width)
     55        Dim AdjustedWidth As Integer = Math.Round(SelectionRectangle.Height * _Image.Width / _Image.Height)
     56        Debug.Assert(AdjustedWidth >= SelectionRectangle.Width Or AdjustedHeight >= SelectionRectangle.Height)
     57        If AdjustedWidth < SelectionRectangle.Width Then
     58            AdjustedSelectionRectangle.Width = AdjustedWidth
     59            ApplyAspectRatio = True
     60        ElseIf AdjustedHeight < SelectionRectangle.Height Then
     61            AdjustedSelectionRectangle.Height = AdjustedHeight
     62            ApplyAspectRatio = True
     63        End If
     64    End Function
     65    Private Function ApplyAspectRatioAndCenterRectangle(ByVal SelectionRectangle As Rectangle) As Rectangle
     66        Dim AdjustedSelectionRectangle As Rectangle
     67        If ApplyAspectRatio(SelectionRectangle, AdjustedSelectionRectangle) Then
     68            AdjustedSelectionRectangle.Offset((SelectionRectangle.Width - AdjustedSelectionRectangle.Width) / 2, (SelectionRectangle.Height - AdjustedSelectionRectangle.Height) / 2)
     69            ApplyAspectRatioAndCenterRectangle = AdjustedSelectionRectangle
     70        Else
     71            ApplyAspectRatioAndCenterRectangle = SelectionRectangle
     72        End If
     73    End Function
    3374
    3475    Public Property Image As Bitmap
     
    3980            _Image = value
    4081            If _Image IsNot Nothing Then
    41                 _SelectionRectangle = New Rectangle(value.Width * 1 / 16, value.Height * 1 / 16, value.Width * 14 / 16, value.Height * 14 / 16)
    42                 '_SelectionRectangle = New Rectangle(0, 0, value.Width, value.Height)
     82                SelectionRectangle = New Rectangle(value.Width * 1 / 16, value.Height * 1 / 16, value.Width * 14 / 16, value.Height * 14 / 160)
     83                'SelectionRectangle = New Rectangle(0, 0, value.Width, value.Height)
    4384                _FadeImage = _Image.Clone
    4485                Dim FadeImageGraphics As Graphics = Graphics.FromImage(_FadeImage)
     
    67108        End Get
    68109        Set(value As Rectangle)
    69             Dim SelectionRectangleChanged As Boolean = _SelectionRectangle <> value
    70             ' TODO: Validate against MinimalSelectionSize
    71             _SelectionRectangle = value
     110            Dim EffectiveSelectionRectangle As Rectangle = ApplyMinimalSelection(value)
     111            If PreserveAspectRatio Then EffectiveSelectionRectangle = ApplyAspectRatioAndCenterRectangle(EffectiveSelectionRectangle)
     112            Dim SelectionRectangleChanged As Boolean = _SelectionRectangle <> EffectiveSelectionRectangle
     113            _SelectionRectangle = EffectiveSelectionRectangle
    72114            RaiseEvent SelectionRectangleChanged(Me, New System.EventArgs())
    73115            If SelectionRectangleChanged Then Invalidate()
     116        End Set
     117    End Property
     118    Public Property PreserveAspectRatio As Boolean
     119        Get
     120            PreserveAspectRatio = _PreserveAspectRatio
     121        End Get
     122        Set(value As Boolean)
     123            If _PreserveAspectRatio = value Then Exit Property
     124            _PreserveAspectRatio = value
     125            If _PreserveAspectRatio Then SelectionRectangle = ApplyAspectRatioAndCenterRectangle(_SelectionRectangle)
    74126        End Set
    75127    End Property
     
    153205        If Capture Then
    154206            Dim P1 As Point, P2 As Point
     207            Dim NewSelectionRectangle As Rectangle, NewAdjustedSelectionRectangle As Rectangle
    155208            Select Case DragSpotIndex
    156209                Case 0 ' Left Top
     
    159212                    ApplyPointConstraint(Position, P1, P2)
    160213                    SourcePosition = SourcePointFromPoint(Position)
    161                     SelectionRectangle = Rectangle.FromLTRB(SourcePosition.X, SourcePosition.Y, SelectionRectangle.Right, SelectionRectangle.Bottom)
     214                    NewSelectionRectangle = Rectangle.FromLTRB(SourcePosition.X, SourcePosition.Y, SelectionRectangle.Right, SelectionRectangle.Bottom)
     215                    If ApplyAspectRatio(NewSelectionRectangle, NewAdjustedSelectionRectangle) Then
     216                        NewAdjustedSelectionRectangle.Offset(NewSelectionRectangle.Width - NewAdjustedSelectionRectangle.Width, NewSelectionRectangle.Height - NewAdjustedSelectionRectangle.Height)
     217                        SelectionRectangle = NewAdjustedSelectionRectangle
     218                    Else
     219                        SelectionRectangle = NewSelectionRectangle
     220                    End If
    162221                Case 1 ' Right Top
    163222                    P1 = New Point(ClientImageOrigin.X + ClientImageExtent.Width, ClientImageOrigin.Y)
     
    165224                    ApplyPointConstraint(Position, New Point(P2.X, P1.Y), New Point(P1.X, P2.Y))
    166225                    SourcePosition = SourcePointFromPoint(Position)
    167                     SelectionRectangle = Rectangle.FromLTRB(SelectionRectangle.Left, SourcePosition.Y, SourcePosition.X, SelectionRectangle.Bottom)
     226                    NewSelectionRectangle = Rectangle.FromLTRB(SelectionRectangle.Left, SourcePosition.Y, SourcePosition.X, SelectionRectangle.Bottom)
     227                    If ApplyAspectRatio(NewSelectionRectangle, NewAdjustedSelectionRectangle) Then
     228                        NewAdjustedSelectionRectangle.Offset(0, NewSelectionRectangle.Height - NewAdjustedSelectionRectangle.Height)
     229                        SelectionRectangle = NewAdjustedSelectionRectangle
     230                    Else
     231                        SelectionRectangle = NewSelectionRectangle
     232                    End If
    168233                Case 2 ' Left Bottom
    169234                    P1 = New Point(ClientImageOrigin.X, ClientImageOrigin.Y + ClientImageExtent.Height)
     
    171236                    ApplyPointConstraint(Position, New Point(P1.X, P2.Y), New Point(P2.X, P1.Y))
    172237                    SourcePosition = SourcePointFromPoint(Position)
    173                     SelectionRectangle = Rectangle.FromLTRB(SourcePosition.X, SelectionRectangle.Top, SelectionRectangle.Right, SourcePosition.Y)
     238                    NewSelectionRectangle = Rectangle.FromLTRB(SourcePosition.X, SelectionRectangle.Top, SelectionRectangle.Right, SourcePosition.Y)
     239                    If ApplyAspectRatio(NewSelectionRectangle, NewAdjustedSelectionRectangle) Then
     240                        NewAdjustedSelectionRectangle.Offset(NewSelectionRectangle.Width - NewAdjustedSelectionRectangle.Width, 0)
     241                        SelectionRectangle = NewAdjustedSelectionRectangle
     242                    Else
     243                        SelectionRectangle = NewSelectionRectangle
     244                    End If
    174245                Case 3 ' Right Bottom
    175246                    P1 = PointFromSourcePoint(New Point(SelectionRectangle.Left + MinimalSelectionSize.Width, SelectionRectangle.Top + MinimalSelectionSize.Height))
     
    177248                    ApplyPointConstraint(Position, P1, P2)
    178249                    SourcePosition = SourcePointFromPoint(Position)
    179                     SelectionRectangle = Rectangle.FromLTRB(SelectionRectangle.Left, SelectionRectangle.Top, SourcePosition.X, SourcePosition.Y)
     250                    NewSelectionRectangle = Rectangle.FromLTRB(SelectionRectangle.Left, SelectionRectangle.Top, SourcePosition.X, SourcePosition.Y)
     251                    If ApplyAspectRatio(NewSelectionRectangle, NewAdjustedSelectionRectangle) Then
     252                        'NewAdjustedSelectionRectangle.Offset(0, 0)
     253                        SelectionRectangle = NewAdjustedSelectionRectangle
     254                    Else
     255                        SelectionRectangle = NewSelectionRectangle
     256                    End If
    180257                Case 4 ' Move
    181258                    Dim Move As Size = SourcePointFromPoint(e.Location) - SourcePointFromPoint(MouseDownPosition)
  • trunk/Snippets/CropImage/MainForm.vb

    r109 r119  
    1111        Box = New ImageCropBox
    1212        Box.MinimalSelectionSize = New Size(320, 240)
     13        Box.PreserveAspectRatio = True
    1314        Box.Image = Bitmap.FromFile("D:\Projects\Alax.Info\Repository-Public\Snippets\CropImage\20120821_185619.jpg")
    1415        Controls.Add(Box)
Note: See TracChangeset for help on using the changeset viewer.