Saturday, November 29, 2014

Windows 8 Save CANVAS as IMAGE WinRT Metro App C#

Hi


You can use following snipped to save Canvas or other Ui element as image



RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();

await renderTargetBitmap.RenderAsync(ImageCanvas);//put the name of Canvas or Image contanier to be saved
var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();


var savePicker = new FileSavePicker();
savePicker.DefaultFileExtension = ".png";
savePicker.FileTypeChoices.Add(".png", new List<string> { ".png" });
savePicker.FileTypeChoices.Add(".jpg", new List<string> { ".jpg" });
savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
savePicker.SuggestedFileName = "photo.png";



var saveFile = await savePicker.PickSaveFileAsync();

if (saveFile == null)

return;

using (var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream);

encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)renderTargetBitmap.PixelWidth,
(uint)renderTargetBitmap.PixelHeight,

DisplayInformation.GetForCurrentView().LogicalDpi,
DisplayInformation.GetForCurrentView().LogicalDpi,
pixelBuffer.ToArray());

await encoder.FlushAsync();
}


Mark this answer if you find this helpful


No comments:

Post a Comment