basquang™ on clouds

September 26, 2013

Windows Store 8.1: Save visual element to bitmap image file

Filed under: Windows Store 8 — basquang @ 3:56 PM
Tags: , , ,

If you has already familiar with Windows Phone development. You may able to capture screen or visual element to image file using the Render method of WriteableBitmap object. When develop Windows Store app, the Render method will not exist at this time. If you are developing Windows Store 8.0 application, it will not easy for you to create a screen capture programmatically of saving visual element to image file. Fortunately with Windows Store 8.1, Microsoft provides the alternative object to do this case, that is RenderTargetBitmap object.

This method below allow you to saving visual element to file (it would saved to Local Storage or Library)

 

async Task SaveVisualElementToFile(FrameworkElement element, StorageFile file)
        {
            string fileName = "customphoto.jpg";
            var renderTargetBitmap = new RenderTargetBitmap();
            await renderTargetBitmap.RenderAsync(element, (int)element.Width, (int)element.Height);
            var pixels = await renderTargetBitmap.GetPixelsAsync();

            using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                var encoder = await
                    BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
                byte[] bytes = pixels.ToArray();
                encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                     BitmapAlphaMode.Ignore,
                                     (uint)element.Width, (uint)element.Height,
                                     96, 96, bytes);

                await encoder.FlushAsync();
            }
        }

NOTE: You cannot save visual element which in code (offscreen) to image file as mention here http://social.msdn.microsoft.com/Forums/sqlserver/en-US/257a1dda-3b0c-4eec-9b08-85c1b9cda187/rendertargetbitmap-not-working-with-simple-stack-panel

Hope this help!

Advertisements

4 Comments »

  1. Today, I went to the beach front with my children. I found
    a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She
    placed the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear.
    She never wants to go back! LoL I know this is totally off topic but I had to tell someone!

    Comment by tome 1 pdf — July 3, 2014 @ 9:52 PM | Reply

    • thank you for the story. i hope this comment reminds you nostalgia and brings joy to you

      Comment by ASFDASFDAFSDFASD — April 2, 2015 @ 11:42 PM | Reply

  2. My brother recommended I might like this web site.
    He was totally right. This post truly made my day.
    You cann’t imagine simply how much time I had spent for this info!
    Thanks!

    Comment by download book — July 27, 2014 @ 12:33 AM | Reply

  3. how about to save an image to local folder picked from a file ?
    StorageFile file = await openPicker.PickSingleFileAsync();
    if (file != null)
    {
    var stream = await file.OpenAsync(FileAccessMode.Read);
    var bitmapImage = new BitmapImage();
    await bitmapImage.SetSourceAsync(stream);
    img.Source = bitmapImage;
    var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream); }

    Comment by Amine Dammak — August 23, 2015 @ 11:13 AM | Reply


RSS feed for comments on this post. TrackBack URI

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

Blog at WordPress.com.

%d bloggers like this: