Last active
August 29, 2015 14:25
-
-
Save clarkezone/7f1fda6090fbbed30caa to your computer and use it in GitHub Desktop.
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 Microsoft.Graphics.Canvas; | |
using Microsoft.Graphics.Canvas.DirectX; | |
using Microsoft.Graphics.Canvas.Text; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Runtime.InteropServices.WindowsRuntime; | |
using Windows.ApplicationModel; | |
using Windows.Foundation; | |
using Windows.Foundation.Collections; | |
using Windows.Storage; | |
using Windows.Storage.Streams; | |
using Windows.UI; | |
using Windows.UI.Xaml; | |
using Windows.UI.Xaml.Controls; | |
using Windows.UI.Xaml.Controls.Primitives; | |
using Windows.UI.Xaml.Data; | |
using Windows.UI.Xaml.Input; | |
using Windows.UI.Xaml.Media; | |
using Windows.UI.Xaml.Navigation; | |
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238 | |
namespace TestWin2DImage | |
{ | |
/// <summary> | |
/// An empty page that can be used on its own or navigated to within a Frame. | |
/// </summary> | |
public sealed partial class MainPage : Page | |
{ | |
CanvasBitmap offscreenRenderedBitmap; | |
public MainPage() | |
{ | |
this.InitializeComponent(); | |
} | |
private async void MakeImage_Click(object sender, RoutedEventArgs e) | |
{ | |
using (var renderTarget = new CanvasRenderTarget(image, 100, 100, 96)) | |
{ | |
using (var ds = renderTarget.CreateDrawingSession()) | |
{ | |
ds.Clear(Colors.Aqua); | |
ds.DrawText("Test at " + DateTime.Now.ToString(), 100, 100, Colors.Orange, | |
new CanvasTextFormat() | |
{ | |
FontSize = 10, | |
HorizontalAlignment = CanvasHorizontalAlignment.Left, | |
VerticalAlignment = CanvasVerticalAlignment.Bottom | |
}); | |
using (var imageBytestream = new InMemoryRandomAccessStream()) | |
{ | |
await renderTarget.SaveAsync(imageBytestream, CanvasBitmapFileFormat.Png); | |
offscreenRenderedBitmap = await CanvasBitmap.LoadAsync(image, imageBytestream); | |
image.Invalidate(); | |
} | |
} | |
} | |
} | |
private void image_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) | |
{ | |
args.DrawingSession.Clear(Colors.Yellow); | |
args.DrawingSession.DrawText("Hello", new Microsoft.Graphics.Canvas.Numerics.Vector2() { X = 10, Y = 10 }, Colors.Red); | |
if (offscreenRenderedBitmap != null) | |
{ | |
// call succeeds but nothing draws. | |
args.DrawingSession.DrawImage(offscreenRenderedBitmap); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment