Last active
October 23, 2015 06:36
-
-
Save dotMorten/9cc44a63631337974a13 to your computer and use it in GitHub Desktop.
XamlRenderingBackgroundTask snippets
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
internal class GraphTile | |
{ | |
public static Task CreateTileGraphAsync(Data data, int width, int height, string filename) | |
{ | |
return RenderAndSaveToFileAsync(GetVisual(data, width, height), (uint)width, (uint)height, filename); | |
} | |
public static Task CreateTileGraphAsync(Data data, int width, int height, IRandomAccessStream stream) | |
{ | |
return RenderAndSaveToStreamAsync(GetVisual(data, width, height), (uint)width, (uint)height, stream); | |
} | |
public static Windows.UI.Xaml.UIElement GetVisual(Data data, int width, int height) | |
{ | |
return GetVisual(data, width, height, new SolidColorBrush(Colors.White)); | |
} | |
public static Windows.UI.Xaml.UIElement GetVisual(Data data, int width, int height, Brush pathbrush) | |
{ | |
//TODO: Create you XAML UI here and return root element | |
return new Border() { Width = width, Height = height, Background = pathbrush }; | |
} | |
private static async Task RenderAndSaveToFileAsync(UIElement element, UInt32 width, UInt32 height, string filename) | |
{ | |
var resultStorageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; | |
var outputStorageFile = await resultStorageFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting); | |
using (var outputFileStream = await outputStorageFile.OpenAsync(FileAccessMode.ReadWrite)) | |
{ | |
await RenderAndSaveToStreamAsync(element, width, height, outputFileStream); | |
} | |
} | |
private static async Task RenderAndSaveToStreamAsync(UIElement element, UInt32 width, UInt32 height, IRandomAccessStream outputFileStream) | |
{ | |
RenderTargetBitmap rtb = new RenderTargetBitmap(); | |
await rtb.RenderAsync(element, (int)width, (int)height); | |
var pixelBuffer = await rtb.GetPixelsAsync(); | |
var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(pixelBuffer); | |
var pixelWidth = (uint)rtb.PixelWidth; | |
var pixelHeight = (uint)rtb.PixelHeight; | |
rtb = null; | |
byte[] buffer = new byte[pixelBuffer.Length]; | |
dataReader.ReadBytes(buffer); | |
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, outputFileStream); | |
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, pixelWidth, pixelHeight, 96, 96, buffer); | |
await encoder.FlushAsync(); | |
buffer = null; | |
//await outputFileStream.FlushAsync(); | |
} | |
public static async Task<WriteableBitmap> RenderMiniToWritableBitmap(Data data, UInt32 width, UInt32 height) | |
{ | |
RenderTargetBitmap rtb = new RenderTargetBitmap(); | |
var element = GetMiniVisual(data, (int)width, (int)height, new SolidColorBrush(Colors.White)); | |
await rtb.RenderAsync(element, (int)width, (int)height); | |
var wb = new WriteableBitmap((int)width, (int)height); | |
IBuffer pixelBuffer = await rtb.GetPixelsAsync(); | |
using (var stream = new InMemoryRandomAccessStream()) | |
{ | |
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream); | |
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, | |
(uint)rtb.PixelWidth, (uint)rtb.PixelHeight, 96, 96, pixelBuffer.ToArray()); | |
await encoder.FlushAsync(); | |
stream.Seek(0); | |
wb.SetSource(stream); | |
} | |
// Redraw the WriteableBitmap | |
wb.Invalidate(); | |
return wb; | |
} |
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
public sealed class UpdateTileHandler : XamlRenderingBackgroundTask | |
{ | |
BackgroundTaskDeferral _deferral; | |
protected async override void OnRun(IBackgroundTaskInstance taskInstance) | |
{ | |
_deferral = taskInstance.GetDeferral(); | |
base.OnRun(taskInstance); | |
Update(taskInstance); | |
} | |
private async void Update(IBackgroundTaskInstance taskInstance) | |
{ | |
string filename = "tile.png"; | |
await GraphTile.CreateTileGraphAsync(null, 310, 150, filename); | |
//todo: Update tile | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment