创建 LiveTile 图像 - 在屏幕上看起来不错 - 看起来像 LiveTile 不对

Creating a LiveTile Image - Looks Good On Screen - Looks Wrong As LiveTile

我正在 Windows 通用应用程序的屏幕上绘制一些形状,然后我还尝试将基本 UIElement 保存到文件中并分配我的 LiveTile 模板以使用它。

我想我很接近了,但有些地方很不对劲。这是它在我的屏幕上的样子:

这是它在 Tile 上的样子:

第一张图中的黑色是透明的,live tile似乎有很多透明像素(浅灰色是我的背景图)。我可以看到很多黄色和绿色,所以它似乎是正确的 creating/finding 文件和更新动态磁贴模板。我只是无法让它看起来像我想要的那样 - 在我的 Windows Phone.

但是,在我的 Windows 10 桌面上 - 它看起来和我期望的一样(只是它似乎失去了透明度 - 我对此很满意)

Canvas 为 150x150。动态磁贴模板是 TileSquare150x150Image。我正在使用 .png 文件。我试过更改 BitmapPixelFormat,手动设置调用 encoder.SetPixelData 时要使用的 logicalDpi,并使用不同的 BitmapAlphaModes...偶尔会抛出异常,否则渲染效果似乎大致相同。

任何人都可以在正确的方向推动我吗?我认为通用应用程序会确保设备之间的行为相同,所以我很惊讶地看到它在 phone 和桌面之间的行为如此不同......但我确定我只是做错了什么。我的代码如下所示:

var renderBitMap = new RenderTargetBitmap();
await renderBitMap.RenderAsync(_container);

var pixels = await renderBitMap.GetPixelsAsync();            
byte[] myBytes = pixels.ToArray();

var folder = ApplicationData.Current.LocalFolder;
var file = await folder.CreateFileAsync("ts150.png", CreationCollisionOption.ReplaceExisting);
var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)options.Size.Width, (uint)options.Size.Height, logicalDpi, logicalDpi, myBytes);

    await encoder.FlushAsync();
}

当您使用 RenderTargetBitmap 呈现控件时,图像会自动按 DisplayInformation.RawPixelsPerViewPixel 缩放(即使您在 RenderAsync() 调用中指定了宽度和高度)。

因此,您应该始终使用 RenderTargetBitmapPixelWidthPixelHeight 属性来确定渲染图像的实际大小。

题中的问题是options.Size.Widthoptions.Size.HeightRenderTargetBitmap的值不一样,所以像素数据和图像大小通过编码器不匹配。