To render a XAML tree as a bitmap:
The Windows Runtime for Windows 8.1 adds a new type for the Windows.UI.Xaml.Media.Imaging namespace:rendertargetbitmap.
This type provides two key methods:
Rendertargetbitmap.renderasync, which is used to extract the XAML visual tree and create a bitmap representation for it.
Note This operation asynchronously renders the given XAML element tree as a bitmap. This method is not synchronized with the screen refresh and does not guarantee precise frame timing, so the bitmap may be rendered at a moment before and after the assumed capture time.
A Rendertargetbitmap.getpixelsasyncthat is used to return a byte array of pixels in a particular format.
The following example shows how to render a XAML element tree.
var New RenderTargetBitmap (); await = RenderTargetBitmap;
RenderTargetBitmap inherits from ImageSource, so it can be set directly as the source of an Image object without calling Getpixelsasync To get and display the bitmap data.
The following example shows how to write a rendered bitmap to a file.
varBitmap =NewRenderTargetBitmap (); awaitBitmap. Renderasync ( This. C1); IBuffer Buffer=awaitbitmap. Getpixelsasync (); varPixelstream =buffer. Asstream (); Filesavepicker Savepicker=NewFilesavepicker (); Savepicker.suggestedstartlocation=Pickerlocationid.desktop; SAVEPICKER.FILETYPECHOICES.ADD ("Bitmap",Newlist<string> () {". PNG" }); Savepicker.suggestedfilename="New Bitmap"; StorageFile Saveditem=awaitSavepicker.picksavefileasync (); Guid Encoderid=Bitmapencoder.pngencoderid; Irandomaccessstream FileStream=awaitSaveditem.openasync (Windows.Storage.FileAccessMode.ReadWrite); Bitmapencoder Encoder=awaitBitmapencoder.createasync (Encoderid, FileStream); byte[] pixels =New byte[Pixelstream.length]; Pixelstream.read (Pixels,0, pixels. Length); //pixal format Shouldconvert to Rgba8 for(inti =0; I < pixels. Length; i + =4) { bytetemp =Pixels[i]; Pixels[i]= Pixels[i +2]; Pixels[i+2] =temp; } encoder. Setpixeldata (Bitmappixelformat.rgba8, Bitmapalphamode.straight, (UINT) bitmap. Pixelwidth, (UINT) bitmap. Pixelheight, the,//Horizontal DPI the,//Vertical DPIpixels); awaitEncoder. Flushasync ();
Metroapp save Uiement The same code as the picture http://www.cnblogs.com/manupstairs/p/3556642.html. its//Pixal format shouldconvert to Rgba8 The following section of the Exchange code does not change color.