Saving UI element as jpeg for Windows Phone 8.1

This is an follow-up of the Drawing pad for Windows Phone 8.1

Assume that the content that is needed to save into jpeg is in a scrollviewer, the below would be the implementation on saving the content. It works for most ui element.

The general idea is to create a bitmap object and thereafter render it into the stream and store it in the isolated storage of the app.


var bitmap = await SaveScreenshotAsync(scrollViewer);

Do replace “saved.bmp” with the filename and file type of your preferred choice.


async Task<RenderTargetBitmap> SaveScreenshotAsync(FrameworkElement uielement)
 {
 string fileName = System.IO.Path.GetFileName("saved.bmp");

// persist data into isolated storage
 StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
 return await SaveToFileAsync(uielement, file);
 }

async Task<RenderTargetBitmap> SaveToFileAsync(FrameworkElement uielement, StorageFile file)
 {
 if (file != null)
 {
 CachedFileManager.DeferUpdates(file);

Guid encoderId = GetBitmapEncoder(file.FileType);

try
 {
 using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
 {
 return await CaptureToStreamAsync(uielement, stream, encoderId);
 }
 }
 catch (Exception ex)
 {}

var status = await CachedFileManager.CompleteUpdatesAsync(file);
 }
 return null;
 }

Guid GetBitmapEncoder(string fileType)
 {
 Guid encoderId = BitmapEncoder.JpegEncoderId;
 switch (fileType)
 {
 case ".bmp":
 encoderId = BitmapEncoder.BmpEncoderId;
 break;
 case ".gif":
 encoderId = BitmapEncoder.GifEncoderId;
 break;
 case ".png":
 encoderId = BitmapEncoder.PngEncoderId;
 break;
 case ".tif":
 encoderId = BitmapEncoder.TiffEncoderId;
 break;
 }

return encoderId;
 }

async Task<RenderTargetBitmap> CaptureToStreamAsync(FrameworkElement uielement, IRandomAccessStream stream, Guid encoderId)
 {
 try
 {
 var renderTargetBitmap = new RenderTargetBitmap();
 await renderTargetBitmap.RenderAsync(uielement);

var pixels = await renderTargetBitmap.GetPixelsAsync();

var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
 var encoder = await BitmapEncoder.CreateAsync(encoderId, stream);
 encoder.SetPixelData(
 BitmapPixelFormat.Bgra8,
 BitmapAlphaMode.Ignore,
 (uint)renderTargetBitmap.PixelWidth,
 (uint)renderTargetBitmap.PixelHeight,
 logicalDpi,
 logicalDpi,
 pixels.ToArray());

await encoder.FlushAsync();

return renderTargetBitmap;
 }
 catch (Exception ex)
 {}

return null;
 }

Advertisements
This entry was posted in C#, Windows Phone, winrt and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s