Answered by:
Take a Screenshot programmatically

Question
-
Hello,
i wonder if it is possible to take a Screenshot from any Device and save it into the Filesystem as Jpeg-StorageFile.
This is what i already have:
RenderTargetBitmap bitmap = new RenderTargetBitmap(); await bitmap.RenderAsync(MyOuterGrid); IBuffer buffer = await bitmap.GetPixelsAsync();
This is what i have tried:
byte[] bytearray = buffer.ToArray(); Stream stream = buffer.AsStream(); IRandomAccessStream randomaccessstream = stream.AsRandomAccessStream(); randomaccessstream.Seek(0); var encoderId = BitmapEncoder.JpegEncoderId; var encoder = await BitmapEncoder.CreateAsync(encoderId, randomaccessstream); //I don't get what this method does encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, 10, 15, 64, 64, bytearray); //Here it stops await encoder.FlushAsync();
How can i encode the Bitmap to a Jpeg?
Tuesday, October 21, 2014 9:03 AM
Answers
-
Have you checked the dimensions of the created RenderTargetBitmap? You're not setting the dimensions yourself so perhaps something's wrong there.
If I go by this sample you might also change the BitmapAlphaMode to Ignore: http://basquang.wordpress.com/2013/09/26/windows-store-8-1-save-visual-element-to-bitmap-image-file/
Edit: I now also noticed that you are using the buffer that you are reading the image from as the target for saving it to. This might also be a problem - especially given that this Buffer is still connected to the RenderTargetBitmap.
You might want to first test this with writing everything to a file and then with a newly created Buffer to avoid issues.
- Edited by Oliver Ulm Tuesday, October 21, 2014 11:41 AM
- Marked as answer by B.Ke Tuesday, October 21, 2014 12:51 PM
Tuesday, October 21, 2014 11:40 AM
All replies
-
encoder.SetPixelData hands over the data the encoder is supposed to encode. In order to do that it needs to know the dimensions of the image (width, height). So right now you tell the encoder that you are providing an image 10 pixels wide and 15 pixels high with a pixel density of 64 dpi.
For correct usage this should read like this:
encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, (uint)bitmap.Width, (uint)bitmap.Height, 96, 96, bytearray);
Tuesday, October 21, 2014 10:54 AM -
Thanks for your explanation. I got it now.
But it still does lock or freeze the screen at encoder.FlushAsync();
Tuesday, October 21, 2014 11:24 AM -
Have you checked the dimensions of the created RenderTargetBitmap? You're not setting the dimensions yourself so perhaps something's wrong there.
If I go by this sample you might also change the BitmapAlphaMode to Ignore: http://basquang.wordpress.com/2013/09/26/windows-store-8-1-save-visual-element-to-bitmap-image-file/
Edit: I now also noticed that you are using the buffer that you are reading the image from as the target for saving it to. This might also be a problem - especially given that this Buffer is still connected to the RenderTargetBitmap.
You might want to first test this with writing everything to a file and then with a newly created Buffer to avoid issues.
- Edited by Oliver Ulm Tuesday, October 21, 2014 11:41 AM
- Marked as answer by B.Ke Tuesday, October 21, 2014 12:51 PM
Tuesday, October 21, 2014 11:40 AM -
Oh ye. Now it works perfectly :) Thanks
Tuesday, October 21, 2014 12:53 PM