@@ -36,7 +36,7 @@ public class CropAvatar : Control
36
36
private Image image ;
37
37
private int initialX , initialY , voffsetX , voffsetY ;
38
38
private bool isDown ;
39
- private bool isLeft ;
39
+ private bool ? isLeft ;
40
40
private Path path ;
41
41
private Point point ;
42
42
private Button replaceButton , addButton ;
@@ -97,13 +97,12 @@ private void Image_MouseLeave(object sender, MouseEventArgs e)
97
97
98
98
void SettingPoint ( )
99
99
{
100
- if ( isLeft )
100
+ if ( isLeft == true )
101
101
{
102
102
_StartX = Canvas . GetLeft ( image ) ;
103
103
initialX = voffsetX ;
104
104
}
105
-
106
- else
105
+ else if ( isLeft == false )
107
106
{
108
107
_StartY = Canvas . GetTop ( image ) ;
109
108
initialY = voffsetY ;
@@ -120,7 +119,7 @@ private void Image_MouseMove(object sender, MouseEventArgs e)
120
119
if ( e . LeftButton == MouseButtonState . Pressed && isDown )
121
120
{
122
121
var vPoint = e . GetPosition ( this ) ;
123
- if ( isLeft )
122
+ if ( isLeft == true )
124
123
{
125
124
var voffset = vPoint . X - point . X ;
126
125
vNewStartX = _StartX + voffset ;
@@ -133,7 +132,7 @@ private void Image_MouseMove(object sender, MouseEventArgs e)
133
132
crop = new CroppedBitmap ( bitmapFrame , new Int32Rect ( voffsetX , 0 , _size , _size ) ) ;
134
133
}
135
134
}
136
- else
135
+ else if ( isLeft == false )
137
136
{
138
137
var voffset = vPoint . Y - point . Y ;
139
138
vNewStartY = _StartY + voffset ;
@@ -146,7 +145,6 @@ private void Image_MouseMove(object sender, MouseEventArgs e)
146
145
crop = new CroppedBitmap ( bitmapFrame , new Int32Rect ( 0 , voffsetY , _size , _size ) ) ;
147
146
}
148
147
}
149
-
150
148
OutImageSource = crop ;
151
149
}
152
150
}
@@ -173,7 +171,11 @@ private void InitialImage()
173
171
vNewStartY = 0 ;
174
172
var uri = ControlsHelper . ImageUri ( ) ;
175
173
if ( uri == null ) return ;
176
- var bitmap = new BitmapImage ( uri ) ;
174
+ var bitmap = new BitmapImage ( ) ;
175
+ bitmap . BeginInit ( ) ;
176
+ bitmap . CacheOption = BitmapCacheOption . OnLoad ;
177
+ bitmap . UriSource = uri ;
178
+ bitmap . EndInit ( ) ;
177
179
if ( bitmap . Height > bitmap . Width )
178
180
{
179
181
var scale = bitmap . Width / path . Width ;
@@ -188,6 +190,12 @@ private void InitialImage()
188
190
image . Height = _size ;
189
191
isLeft = true ;
190
192
}
193
+ else
194
+ {
195
+ image . Width = _size ;
196
+ image . Height = _size ;
197
+ isLeft = null ;
198
+ }
191
199
192
200
bitmapFrame = ControlsHelper . CreateResizedImage ( bitmap , ( int ) image . Width , ( int ) image . Height , 0 ) ;
193
201
image . Source = bitmapFrame ;
@@ -203,19 +211,20 @@ private void InitialImage()
203
211
_StartY = ( canvas . ActualHeight - image . Height ) / 2.0d ;
204
212
Canvas . SetLeft ( image , _StartX ) ;
205
213
Canvas . SetTop ( image , _StartY ) ;
206
- if ( isLeft )
214
+ if ( isLeft == true )
207
215
{
208
216
initialX = ( ( int ) image . Width - 200 ) / 2 ;
209
217
initialY = 0 ;
210
218
crop = new CroppedBitmap ( bitmapFrame , new Int32Rect ( initialX , 0 , _size , _size ) ) ;
211
219
}
212
- else
220
+ else if ( isLeft == false )
213
221
{
214
222
initialY = ( ( int ) image . Height - 200 ) / 2 ;
215
223
initialX = 0 ;
216
224
crop = new CroppedBitmap ( bitmapFrame , new Int32Rect ( 0 , initialY , _size , _size ) ) ;
217
225
}
218
-
226
+ else
227
+ crop = new CroppedBitmap ( bitmapFrame , new Int32Rect ( 0 , 0 , _size , _size ) ) ;
219
228
OutImageSource = crop ;
220
229
}
221
230
}
0 commit comments