Created
February 19, 2016 16:59
-
-
Save LanceMcCarthy/4863d10602dad7eae4de to your computer and use it in GitHub Desktop.
Custom RadImageEditor Win2D GaussianBlur Tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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