RenderTargetBitmap在循环中呈现emojis的重影问题 - UWP

时间:2017-09-20 14:18:29

标签: uwp emoji

当在循环中后续的png也微弱地显示前一个。

Unexpected render in loop

有两个按钮,一个用于在循环中渲染多个表情符号,另一个用于单独执行。单独完成时,将按预期呈现表情符号。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Graphics.Display;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;

namespace RenderTargetBitmapLoopIssue
{

public sealed partial class MainPage : Page
{
    private List<EmojiDetail> emojiDetails;
    private int individualCounter = 0;
    public MainPage()
    {
        this.InitializeComponent();
        var texts = new string[] { "0023_fe0f_20e3", "002a_fe0f_20e3", "0030_fe0f_20e3", "0031_fe0f_20e3", "0032_fe0f_20e3", "0033_fe0f_20e3", "0034_fe0f_20e3", "0035_fe0f_20e3", "0036_fe0f_20e3", "0037_fe0f_20e3", "0038_fe0f_20e3", "0039_fe0f_20e3" };
        emojiDetails = texts.Select(t => new EmojiDetail { FileName = t, EmojiSequence = GetText(t) }).ToList();
    }
    private string GetText(string emoji)
    {
        var parts = emoji.Split(new string[] { "_" }, StringSplitOptions.None);
        var text = "";
        foreach (var part in parts)
        {
            text += HexToTextString(part);
        }
        return text;
    }
    private string HexToTextString(string hex)
    {
        int value = int.Parse(hex, System.Globalization.NumberStyles.HexNumber);
        return char.ConvertFromUtf32(value).ToString();
    }

    private  IAsyncOperation<StorageFolder> GetStorageFolder()
    {
        var folderPicker = new FolderPicker();
        folderPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
        folderPicker.FileTypeFilter.Add("*");
        return folderPicker.PickSingleFolderAsync();
    }
    private async void Button_RenderInLoop(object sender, RoutedEventArgs e)
    {
        var folder = await GetStorageFolder();

        foreach (var em in emojiDetails)
        {
            await Render(folder, em.FileName, em.EmojiSequence);
        }
    }
    private async Task Render(StorageFolder folder,string fileName,string emojiSequence)
    {
        renderedControl.Text = emojiSequence;
        var saveFile = await folder.CreateFileAsync(fileName+".png", CreationCollisionOption.ReplaceExisting);
        RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
        await renderTargetBitmap.RenderAsync(renderedControl);
        var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
        // Encode the image to the selected file on disk 
        using (var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite))
        {
            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream);

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

            await encoder.FlushAsync();
        }
    }
    private async void Button_RenderIndividually(object sender, RoutedEventArgs e)
    {
        var folder = await GetStorageFolder();
        var emoji = emojiDetails[individualCounter];
        await Render(folder, emoji.FileName + "_individual", emoji.EmojiSequence);
        individualCounter++;
    }
}
class EmojiDetail
{
    public string FileName { get; set; }
    public string EmojiSequence { get; set; }
}
}

<Page
x:Class="RenderTargetBitmapLoopIssue.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:RenderTargetBitmapLoopIssue"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBox FontSize="72" HorizontalAlignment="Left" x:Name="renderedControl"></TextBox>
    <Button Click="Button_RenderInLoop">Render in loop</Button>
    <Button Click="Button_RenderIndividually">Render individually</Button>
</StackPanel>
</Page>

请指教,

非常感谢Tony。

0 个答案:

没有答案