Skip to content

Commit

Permalink
Fix GetImageStream without await
Browse files Browse the repository at this point in the history
  • Loading branch information
myroot committed Oct 24, 2022
1 parent 08bf36d commit 91e9cc7
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ public static class DrawingViewService
/// <param name="strokeColor">Line color</param>
/// <param name="background">Image background</param>
/// <returns>Image stream</returns>
public static async ValueTask<Stream> GetImageStream(IList<PointF> points, Size imageSize, float lineWidth, Color strokeColor, Paint? background)
public static ValueTask<Stream> GetImageStream(IList<PointF> points, Size imageSize, float lineWidth, Color strokeColor, Paint? background)
{
var image = GetBitmapForPoints(points, lineWidth, strokeColor, background);

if (image is null)
{
return Stream.Null;
return ValueTask.FromResult(Stream.Null);
}

// Defer to thread pool thread https://github.com/CommunityToolkit/Maui/pull/692#pullrequestreview-1150202727
return await Task.Run(() =>
return new ValueTask<Stream>(Task.Run<Stream>(() =>
{
var resized = image.Resize(new SKImageInfo((int)imageSize.Width, (int)imageSize.Height, SKColorType.Bgra8888, SKAlphaType.Opaque), SKFilterQuality.High);
var data = resized.Encode(SKEncodedImageFormat.Png, 100);
Expand All @@ -37,7 +37,7 @@ public static async ValueTask<Stream> GetImageStream(IList<PointF> points, Size
stream.Seek(0, SeekOrigin.Begin);

return stream;
}).ConfigureAwait(false);
}));
}

/// <summary>
Expand All @@ -47,17 +47,17 @@ public static async ValueTask<Stream> GetImageStream(IList<PointF> points, Size
/// <param name="imageSize">Image size</param>
/// <param name="background">Image background</param>
/// <returns>Image stream</returns>
public static async ValueTask<Stream> GetImageStream(IList<IDrawingLine> lines, Size imageSize, Paint? background)
public static ValueTask<Stream> GetImageStream(IList<IDrawingLine> lines, Size imageSize, Paint? background)
{
var image = GetBitmapForLines(lines, background);

if (image is null)
{
return Stream.Null;
return ValueTask.FromResult(Stream.Null);
}

// Defer to thread pool thread https://github.com/CommunityToolkit/Maui/pull/692#pullrequestreview-1150202727
return await Task.Run(() =>
return new ValueTask<Stream>(Task.Run<Stream>(() =>
{
var resized = image.Resize(new SKImageInfo((int)imageSize.Width, (int)imageSize.Height, SKColorType.Bgra8888, SKAlphaType.Opaque), SKFilterQuality.High);
var data = resized.Encode(SKEncodedImageFormat.Png, 100);
Expand All @@ -67,7 +67,7 @@ public static async ValueTask<Stream> GetImageStream(IList<IDrawingLine> lines,
stream.Seek(0, SeekOrigin.Begin);

return stream;
}).ConfigureAwait(false);
}));
}

static (SKBitmap?, SizeF offset) GetBitmap(in ICollection<PointF> points)
Expand Down

0 comments on commit 91e9cc7

Please sign in to comment.