Skip to content

Commit 81755dc

Browse files
committed
Update CropAvatar.cs
Fixed #35
1 parent f4f449c commit 81755dc

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/WPFDevelopers.Shared/Controls/CropAvatar/CropAvatar.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class CropAvatar : Control
3636
private Image image;
3737
private int initialX, initialY, voffsetX, voffsetY;
3838
private bool isDown;
39-
private bool isLeft;
39+
private bool? isLeft;
4040
private Path path;
4141
private Point point;
4242
private Button replaceButton, addButton;
@@ -97,13 +97,12 @@ private void Image_MouseLeave(object sender, MouseEventArgs e)
9797

9898
void SettingPoint()
9999
{
100-
if (isLeft)
100+
if (isLeft == true)
101101
{
102102
_StartX = Canvas.GetLeft(image);
103103
initialX = voffsetX;
104104
}
105-
106-
else
105+
else if(isLeft == false)
107106
{
108107
_StartY = Canvas.GetTop(image);
109108
initialY = voffsetY;
@@ -120,7 +119,7 @@ private void Image_MouseMove(object sender, MouseEventArgs e)
120119
if (e.LeftButton == MouseButtonState.Pressed && isDown)
121120
{
122121
var vPoint = e.GetPosition(this);
123-
if (isLeft)
122+
if (isLeft == true)
124123
{
125124
var voffset = vPoint.X - point.X;
126125
vNewStartX = _StartX + voffset;
@@ -133,7 +132,7 @@ private void Image_MouseMove(object sender, MouseEventArgs e)
133132
crop = new CroppedBitmap(bitmapFrame, new Int32Rect(voffsetX, 0, _size, _size));
134133
}
135134
}
136-
else
135+
else if (isLeft == false)
137136
{
138137
var voffset = vPoint.Y - point.Y;
139138
vNewStartY = _StartY + voffset;
@@ -146,7 +145,6 @@ private void Image_MouseMove(object sender, MouseEventArgs e)
146145
crop = new CroppedBitmap(bitmapFrame, new Int32Rect(0, voffsetY, _size, _size));
147146
}
148147
}
149-
150148
OutImageSource = crop;
151149
}
152150
}
@@ -173,7 +171,11 @@ private void InitialImage()
173171
vNewStartY = 0;
174172
var uri = ControlsHelper.ImageUri();
175173
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();
177179
if (bitmap.Height > bitmap.Width)
178180
{
179181
var scale = bitmap.Width / path.Width;
@@ -188,6 +190,12 @@ private void InitialImage()
188190
image.Height = _size;
189191
isLeft = true;
190192
}
193+
else
194+
{
195+
image.Width = _size;
196+
image.Height = _size;
197+
isLeft = null;
198+
}
191199

192200
bitmapFrame = ControlsHelper.CreateResizedImage(bitmap, (int)image.Width, (int)image.Height, 0);
193201
image.Source = bitmapFrame;
@@ -203,19 +211,20 @@ private void InitialImage()
203211
_StartY = (canvas.ActualHeight - image.Height) / 2.0d;
204212
Canvas.SetLeft(image, _StartX);
205213
Canvas.SetTop(image, _StartY);
206-
if (isLeft)
214+
if (isLeft == true)
207215
{
208216
initialX = ((int)image.Width - 200) / 2;
209217
initialY = 0;
210218
crop = new CroppedBitmap(bitmapFrame, new Int32Rect(initialX, 0, _size, _size));
211219
}
212-
else
220+
else if (isLeft == false)
213221
{
214222
initialY = ((int)image.Height - 200) / 2;
215223
initialX = 0;
216224
crop = new CroppedBitmap(bitmapFrame, new Int32Rect(0, initialY, _size, _size));
217225
}
218-
226+
else
227+
crop = new CroppedBitmap(bitmapFrame, new Int32Rect(0, 0, _size, _size));
219228
OutImageSource = crop;
220229
}
221230
}

0 commit comments

Comments
 (0)