Skip to content

Instantly share code, notes, and snippets.

@jamesmcroft
Created March 8, 2016 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmcroft/42b6e79079298ca11618 to your computer and use it in GitHub Desktop.
Save jamesmcroft/42b6e79079298ca11618 to your computer and use it in GitHub Desktop.
namespace WinUX.Extensions
{
using System;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Graphics.DirectX;
using Windows.Graphics.Display;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
using Microsoft.Graphics.Canvas;
/// <summary>
/// A collection of <see cref="InkCanvas"/> extensions.
/// </summary>
public static class InkCanvasExtensions
{
/// <summary>
/// Captures the ink from an <see cref="InkCanvas"/> control.
/// </summary>
/// <param name="canvas">
/// The <see cref="InkCanvas"/> control.
/// </param>
/// <param name="rootRenderElement">
/// A <see cref="FrameworkElement"/> which wraps the canvas.
/// </param>
/// <param name="encoderId">
/// A <see cref="BitmapEncoder"/> ID to use to render the image.
/// </param>
/// <returns>
/// Returns an awaitable task.
/// </returns>
public static async Task<StorageFile> CaptureInkAsImageAsync(
this InkCanvas canvas,
FrameworkElement rootRenderElement,
Guid encoderId)
{
var targetFile =
await
ApplicationData.Current.TemporaryFolder.CreateFileAsync(
$"{Guid.NewGuid()}.png",
CreationCollisionOption.ReplaceExisting);
if (targetFile != null)
{
var renderBitmap = new RenderTargetBitmap();
await renderBitmap.RenderAsync(rootRenderElement);
var bitmapSizeAt96Dpi = new Size(renderBitmap.PixelWidth, renderBitmap.PixelHeight);
var pixels = await renderBitmap.GetPixelsAsync();
var win2DDevice = CanvasDevice.GetSharedDevice();
using (
var target = new CanvasRenderTarget(
win2DDevice,
(float)rootRenderElement.ActualWidth,
(float)rootRenderElement.ActualHeight,
96.0f))
{
using (var drawingSession = target.CreateDrawingSession())
{
using (
var canvasBitmap = CanvasBitmap.CreateFromBytes(
win2DDevice,
pixels,
(int)bitmapSizeAt96Dpi.Width,
(int)bitmapSizeAt96Dpi.Height,
DirectXPixelFormat.B8G8R8A8UIntNormalized,
96.0f))
{
drawingSession.DrawImage(
canvasBitmap,
new Rect(0, 0, target.SizeInPixels.Width, target.SizeInPixels.Height),
new Rect(0, 0, bitmapSizeAt96Dpi.Width, bitmapSizeAt96Dpi.Height));
}
drawingSession.Units = CanvasUnits.Pixels;
drawingSession.DrawInk(canvas.InkPresenter.StrokeContainer.GetStrokes());
}
using (var stream = await targetFile.OpenAsync(FileAccessMode.ReadWrite))
{
var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
var encoder = await BitmapEncoder.CreateAsync(encoderId, stream);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)renderBitmap.PixelWidth,
(uint)renderBitmap.PixelHeight,
logicalDpi,
logicalDpi,
target.GetPixelBytes());
await encoder.FlushAsync();
}
}
}
return targetFile;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment