Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 7248f21

Browse files
return null instead of exception (#1339)
1 parent dc46d32 commit 7248f21

File tree

7 files changed

+23
-14
lines changed

7 files changed

+23
-14
lines changed

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Service/DrawingViewService.android.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static Stream GetImageStream(IList<Point> points,
4848
return stream;
4949
}
5050

51-
static Bitmap GetImageInternal(IList<Point> points,
51+
static Bitmap? GetImageInternal(IList<Point> points,
5252
float lineWidth,
5353
Color strokeColor,
5454
Color backgroundColor)
@@ -59,7 +59,7 @@ static Bitmap GetImageInternal(IList<Point> points,
5959
var drawingHeight = points.Max(p => p.Y) - minPointY;
6060
const int minSize = 1;
6161
if (drawingWidth < minSize || drawingHeight < minSize)
62-
throw new Exception($"The image size should be at least {minSize} x {minSize}.");
62+
return null;
6363

6464
var image = Bitmap.CreateBitmap((int)drawingWidth, (int)drawingHeight, Bitmap.Config.Argb8888!)!;
6565
using var canvas = new Canvas(image);

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Service/DrawingViewService.gtk.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static Stream GetImageStream(IList<Point> points,
5050
}
5151
}
5252

53-
static Bitmap GetImageInternal(IList<Point> points,
53+
static Bitmap? GetImageInternal(IList<Point> points,
5454
float lineWidth,
5555
Color strokeColor,
5656
Color backgroundColor)
@@ -62,7 +62,7 @@ static Bitmap GetImageInternal(IList<Point> points,
6262
const int minSize = 1;
6363
if (drawingWidth < minSize || drawingHeight < minSize)
6464
{
65-
throw new Exception($"The image size should be at least {minSize} x {minSize}.");
65+
return null;
6666
}
6767

6868
var bm = new Bitmap((int)drawingWidth, (int)drawingHeight);

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Service/DrawingViewService.ios.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static Stream GetImageStream(IList<Point> points,
4141
return resizedImage.AsJPEG().AsStream();
4242
}
4343

44-
static UIImage GetImageInternal(IList<Point> points,
44+
static UIImage? GetImageInternal(IList<Point> points,
4545
float lineWidth,
4646
Color strokeColor,
4747
Color backgroundColor)
@@ -53,7 +53,7 @@ static UIImage GetImageInternal(IList<Point> points,
5353
const int minSize = 1;
5454
if (drawingWidth < minSize || drawingHeight < minSize)
5555
{
56-
throw new Exception($"The image size should be at least {minSize} x {minSize}.");
56+
return null;
5757
}
5858

5959
var imageSize = new CGSize(drawingWidth, drawingHeight);

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Service/DrawingViewService.macos.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,15 @@ public static Stream GetImageStream(IList<Point> points,
3232
}
3333

3434
var image = GetImageInternal(points, lineWidth, strokeColor, backgroundColor);
35+
if (image is null)
36+
{
37+
return Stream.Null;
38+
}
39+
3540
return image.AsTiff().AsStream();
3641
}
3742

38-
static NSImage GetImageInternal(IList<Point> points,
43+
static NSImage? GetImageInternal(IList<Point> points,
3944
float lineWidth,
4045
Color strokeColor,
4146
Color backgroundColor)
@@ -47,7 +52,7 @@ static NSImage GetImageInternal(IList<Point> points,
4752
const int minSize = 1;
4853
if (drawingWidth < minSize || drawingHeight < minSize)
4954
{
50-
throw new Exception($"The image size should be at least {minSize} x {minSize}.");
55+
return null;
5156
}
5257

5358
var imageSize = new CGSize(drawingWidth, drawingHeight);

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Service/DrawingViewService.tizen.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static Stream GetImageStream(IList<Point> points,
4646
}
4747
}
4848

49-
static SKImage GetImageInternal(IList<Point> points,
49+
static SKImage? GetImageInternal(IList<Point> points,
5050
float lineWidth,
5151
Color strokeColor,
5252
Color backgroundColor)
@@ -58,7 +58,7 @@ static SKImage GetImageInternal(IList<Point> points,
5858
const int minSize = 1;
5959
if (drawingWidth < minSize || drawingHeight < minSize)
6060
{
61-
throw new Exception($"The image size should be at least {minSize} x {minSize}.");
61+
return null;
6262
}
6363

6464
var bm = new SKBitmap((int)drawingWidth, (int)drawingHeight);

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Service/DrawingViewService.uwp.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static Stream GetImageStream(IList<Point> points,
5050
}
5151
}
5252

53-
static CanvasRenderTarget GetImageInternal(IList<Point> points,
53+
static CanvasRenderTarget? GetImageInternal(IList<Point> points,
5454
float lineWidth,
5555
Color lineColor,
5656
Color backgroundColor)
@@ -62,7 +62,7 @@ static CanvasRenderTarget GetImageInternal(IList<Point> points,
6262
const int minSize = 1;
6363
if (drawingWidth < minSize || drawingHeight < minSize)
6464
{
65-
throw new Exception($"The image size should be at least {minSize} x {minSize}.");
65+
return null;
6666
}
6767

6868
var device = CanvasDevice.GetSharedDevice();

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Service/DrawingViewService.wpf.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public static Stream GetImageStream(IList<Point> points,
3535
}
3636

3737
var image = GetImageInternal(points, lineWidth, strokeColor, backgroundColor);
38+
if (image is null)
39+
{
40+
return Stream.Null;
41+
}
3842

3943
var resizedImage = MaxResizeImage(image, (float) imageSize.Width, (float) imageSize.Height);
4044
using (resizedImage)
@@ -46,7 +50,7 @@ public static Stream GetImageStream(IList<Point> points,
4650
}
4751
}
4852

49-
static Bitmap GetImageInternal(ICollection<Point> points,
53+
static Bitmap? GetImageInternal(ICollection<Point> points,
5054
float lineWidth,
5155
Color strokeColor,
5256
Color backgroundColor)
@@ -58,7 +62,7 @@ static Bitmap GetImageInternal(ICollection<Point> points,
5862
const int minSize = 1;
5963
if (drawingWidth < minSize || drawingHeight < minSize)
6064
{
61-
throw new Exception($"The image size should be at least {minSize} x {minSize}.");
65+
return null;
6266
}
6367

6468
var bm = new Bitmap((int) drawingWidth, (int) drawingHeight);

0 commit comments

Comments
 (0)