Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Custom RadImageEditor Win2D GaussianBlur Tool
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Effects;
using Telerik.UI.Xaml.Controls.Input.ImageEditor;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
namespace RadImageEditorAndWin2D.CustomTools
{
public class GaussianBlurTool : RangeTool
{
public override string Name => "Gaussian Blur";
public override string Icon => "ms-appx:///CustomTools/ToolIcons/blur.png";
public override double Min => 0; //Maximum value for the slider
public override double Max => 20; //Minimum value for the slider
protected override async Task<WriteableBitmap> ApplyCore(IRandomAccessStream stream, WriteableBitmap targetBitmap)
{
try
{
stream.Seek(0);
using (var device = CanvasDevice.GetSharedDevice())
using (CanvasBitmap cbm = await CanvasBitmap.LoadAsync(device, stream))
using (CanvasRenderTarget renderer = new CanvasRenderTarget(device, cbm.SizeInPixels.Width, cbm.SizeInPixels.Height, cbm.Dpi))
using (CanvasDrawingSession ds = renderer.CreateDrawingSession())
{
var blur = new GaussianBlurEffect
{
BlurAmount = (float) this.Value,
Source = cbm
};
ds.DrawImage(blur);
ds.Flush(); //important, this forces the drawing operation to complete
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
{
//IMPORTANT NOTE:
//You need to add using System.Runtime.InteropServices.WindowsRuntime in order to use CopyTo(IBuffer)
renderer.GetPixelBytes().CopyTo(targetBitmap.PixelBuffer);
});
}
return targetBitmap;
}
catch (Exception ex)
{
Debug.WriteLine($"ApplyCore in GaussianBlurTool Exception: {ex}");
throw;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment