Changeset 119 for trunk/Snippets
- Timestamp:
- Sep 9, 2012, 6:41:33 AM (12 years ago)
- Location:
- trunk/Snippets/CropImage
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Snippets/CropImage/ImageCropBox.vb
r111 r119 11 11 Private _MinimalSelectionSize As Size 12 12 Private _SelectionRectangle As Rectangle 13 Private _PreserveAspectRatio As Boolean = False 13 14 Private ClientImageOrigin As Point 14 15 Private ClientImageExtent As Size … … 30 31 ' Add any initialization after the InitializeComponent() call. 31 32 _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 33 74 34 75 Public Property Image As Bitmap … … 39 80 _Image = value 40 81 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) 43 84 _FadeImage = _Image.Clone 44 85 Dim FadeImageGraphics As Graphics = Graphics.FromImage(_FadeImage) … … 67 108 End Get 68 109 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 72 114 RaiseEvent SelectionRectangleChanged(Me, New System.EventArgs()) 73 115 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) 74 126 End Set 75 127 End Property … … 153 205 If Capture Then 154 206 Dim P1 As Point, P2 As Point 207 Dim NewSelectionRectangle As Rectangle, NewAdjustedSelectionRectangle As Rectangle 155 208 Select Case DragSpotIndex 156 209 Case 0 ' Left Top … … 159 212 ApplyPointConstraint(Position, P1, P2) 160 213 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 162 221 Case 1 ' Right Top 163 222 P1 = New Point(ClientImageOrigin.X + ClientImageExtent.Width, ClientImageOrigin.Y) … … 165 224 ApplyPointConstraint(Position, New Point(P2.X, P1.Y), New Point(P1.X, P2.Y)) 166 225 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 168 233 Case 2 ' Left Bottom 169 234 P1 = New Point(ClientImageOrigin.X, ClientImageOrigin.Y + ClientImageExtent.Height) … … 171 236 ApplyPointConstraint(Position, New Point(P1.X, P2.Y), New Point(P2.X, P1.Y)) 172 237 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 174 245 Case 3 ' Right Bottom 175 246 P1 = PointFromSourcePoint(New Point(SelectionRectangle.Left + MinimalSelectionSize.Width, SelectionRectangle.Top + MinimalSelectionSize.Height)) … … 177 248 ApplyPointConstraint(Position, P1, P2) 178 249 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 180 257 Case 4 ' Move 181 258 Dim Move As Size = SourcePointFromPoint(e.Location) - SourcePointFromPoint(MouseDownPosition) -
trunk/Snippets/CropImage/MainForm.vb
r109 r119 11 11 Box = New ImageCropBox 12 12 Box.MinimalSelectionSize = New Size(320, 240) 13 Box.PreserveAspectRatio = True 13 14 Box.Image = Bitmap.FromFile("D:\Projects\Alax.Info\Repository-Public\Snippets\CropImage\20120821_185619.jpg") 14 15 Controls.Add(Box)
Note: See TracChangeset
for help on using the changeset viewer.