Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispose device after using in ImageCropper.Helpers.cs #3283

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,45 +51,47 @@ private static async Task CropImageAsync(WriteableBitmap writeableBitmap, IRando

private static async Task CropImageWithShapeAsync(WriteableBitmap writeableBitmap, IRandomAccessStream stream, Rect croppedRect, BitmapFileFormat bitmapFileFormat, CropShape cropShape)
{
var device = CanvasDevice.GetSharedDevice();
var clipGeometry = CreateClipGeometry(device, cropShape, new Size(croppedRect.Width, croppedRect.Height));
if (clipGeometry == null)
using (CanvasDevice device = CanvasDevice.GetSharedDevice())
{
return;
}
var clipGeometry = CreateClipGeometry(device, cropShape, new Size(croppedRect.Width, croppedRect.Height));
if (clipGeometry == null)
{
return;
}

CanvasBitmap sourceBitmap = null;
using (var randomAccessStream = new InMemoryRandomAccessStream())
{
await CropImageAsync(writeableBitmap, randomAccessStream, croppedRect, bitmapFileFormat);
sourceBitmap = await CanvasBitmap.LoadAsync(device, randomAccessStream);
}
CanvasBitmap sourceBitmap = null;
using (var randomAccessStream = new InMemoryRandomAccessStream())
{
await CropImageAsync(writeableBitmap, randomAccessStream, croppedRect, bitmapFileFormat);
sourceBitmap = await CanvasBitmap.LoadAsync(device, randomAccessStream);
}

using (var offScreen = new CanvasRenderTarget(device, (float)croppedRect.Width, (float)croppedRect.Height, 96f))
{
using (var drawingSession = offScreen.CreateDrawingSession())
using (var markCommandList = new CanvasCommandList(device))
using (var offScreen = new CanvasRenderTarget(device, (float)croppedRect.Width, (float)croppedRect.Height, 96f))
{
using (var markDrawingSession = markCommandList.CreateDrawingSession())
using (var drawingSession = offScreen.CreateDrawingSession())
using (var markCommandList = new CanvasCommandList(device))
{
markDrawingSession.FillGeometry(clipGeometry, Colors.Black);
using (var markDrawingSession = markCommandList.CreateDrawingSession())
{
markDrawingSession.FillGeometry(clipGeometry, Colors.Black);
}

var alphaMaskEffect = new AlphaMaskEffect
{
Source = sourceBitmap,
AlphaMask = markCommandList
};
drawingSession.DrawImage(alphaMaskEffect);
alphaMaskEffect.Dispose();
}

var alphaMaskEffect = new AlphaMaskEffect
{
Source = sourceBitmap,
AlphaMask = markCommandList
};
drawingSession.DrawImage(alphaMaskEffect);
alphaMaskEffect.Dispose();
clipGeometry.Dispose();
sourceBitmap.Dispose();
var pixelBytes = offScreen.GetPixelBytes();
var bitmapEncoder = await BitmapEncoder.CreateAsync(GetEncoderId(bitmapFileFormat), stream);
bitmapEncoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, offScreen.SizeInPixels.Width, offScreen.SizeInPixels.Height, 96.0, 96.0, pixelBytes);
await bitmapEncoder.FlushAsync();
}

clipGeometry.Dispose();
sourceBitmap.Dispose();
var pixelBytes = offScreen.GetPixelBytes();
var bitmapEncoder = await BitmapEncoder.CreateAsync(GetEncoderId(bitmapFileFormat), stream);
bitmapEncoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, offScreen.SizeInPixels.Width, offScreen.SizeInPixels.Height, 96.0, 96.0, pixelBytes);
await bitmapEncoder.FlushAsync();
}
}

Expand Down