Windows Phone 8.1 生成和存储图片

network | | 访问(57)
<StackPanel>
    <Button x:Name="bt_save" Content="存储生成的图片" Click="bt_save_Click"></Button>
    <Button x:Name="bt_show" Content="展示存储的图片" Click="bt_show_Click"></Button>
    <ScrollViewer BorderBrush="Red" BorderThickness="2" Height="350">
        <Image x:Name="img" />
    </ScrollViewer>
</StackPanel>
private async void bt_save_Click(object sender, RoutedEventArgs e) {
    RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
    await renderTargetBitmap.RenderAsync(root);
    var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();

    IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
    IStorageFile saveFile = await applicationFolder.CreateFileAsync("snapshot.png", CreationCollisionOption.OpenIfExists);

    using(var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite)) {
        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream);

        encoder.SetPixelData(BitmapPixelFormat.Bgra8, 
        BitmapAlphaMode.Ignore, 
        (uint) renderTargetBitmap.PixelWidth, 
        (uint) renderTargetBitmap.PixelHeight, 
        DisplayInformation.GetForCurrentView().LogicalDpi, 
        DisplayInformation.GetForCurrentView().LogicalDpi, 
        pixelBuffer.ToArray());

        await encoder.FlushAsync();
    }
}

private void bt_show_Click(object sender, RoutedEventArgs e) {
    BitmapImage bitmapImage = new BitmapImage(new Uri("ms-appdata:///local/snapshot.png", UriKind.Absolute));

    img.Source = bitmapImage;
}