Windows Store 8.1: Save visual element to bitmap image file

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!

This Post Has 0 Comments

  1. tome 1 pdf

    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!

    1. ASFDASFDAFSDFASD

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

  2. download book

    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!

  3. Amine Dammak

    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); }

Leave a Reply