Save a Grid and elements in it as a jpeg image - WinRT

六月ゝ 毕业季﹏ 提交于 2019-12-24 12:27:02

问题


I am stuck in my app. I have a grid with some elements in it. These elements are buttons, images and other controls like stackpanel and nested grids. I want to save what appears to the user as image on click of a button, but i dont know how to proceed on this.

Can I write elements like grid and buttons onto a writable bitmap? Or is there some other way?

In short I want to take a screen shot of my app screen when the user clicks the button. Please help.


回答1:


Taking "screenshots" of winrt apps, or just controls is not possible on WinRt. Not implemented, and right now they don't plan on doing it.




回答2:


Unfortunately, it's not possible at this point. As @FilipSkakun mentions in a response here, you might be able to get part of the way depending on your requirements.




回答3:


This has changed a bit in Windows 8.1 and can be accomplished using a RenderTargetBitmap. RenderTargetBitmap.RenderAsync(UIElement) will allow you to get the pixels for any element, including a page.

Here's an example that will save a PNG to a file specified using a FileSavePicker.

var filePicker = new FileSavePicker();
var file = await filePicker.PickSaveFileAsync();
var renderTargetBitmap = new RenderTargetBitmap();

using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
    await renderTargetBitmap.RenderAsync(this);
    var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
    var pixelBytes = pixelBuffer.ToArray();

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

    await encoder.FlushAsync();
}



回答4:


You can convert any UIElement into Jpeg image using the following option in Windows Phone 8.

var bitmap = new WriteableBitmap(element,null);

using (MemoryStream s = new MemoryStream())
{
     bitmap.SaveJpeg(s, (int)ContentPanel.Width, (int)ContentPanel.Height, 0, 100);
}

I hope it will be helpful to you.



来源:https://stackoverflow.com/questions/15918296/save-a-grid-and-elements-in-it-as-a-jpeg-image-winrt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!