0

我正在生成一个图像并将其用作实时图像,以便我可以提供更多信息并更好地控制它的外观。问题是,虽然我可以生成图像,但其中不包括我自己的背景图像。

以下代码可以正常工作并且可以很好地拾取图像

ShellTile defaultTile = ShellTile.ActiveTiles.First();
defaultTile.Update(new StandardTileData
{
    Title = item2.NextDateTime.ToShortTimeString(),
    BackBackgroundImage = new Uri("Images/Tiles/tilebacking.png", UriKind.Relative),
    BackTitle = item2.NextDateTime.ToShortTimeString(),
    BackContent = item2.Details
});

然而,以下代码在生成图像时不包括我的初始图像(根据 Var 徽标)。我在网上找到了执行此操作的代码,除了包含我的初始背景图像外,它似乎工作正常。

var bmp = new WriteableBitmap(173, 173);
var logo = new BitmapImage(new Uri("Images/Tiles/tilebacking.png", UriKind.Relative));
var img = new Image { Source = logo };

// Force the bitmapimage to load it's properties so the transform will work
logo.CreateOptions = BitmapCreateOptions.None;
var bl = new TextBlock();
bl.Foreground = new SolidColorBrush(Colors.White);
bl.FontSize = 24.0;
bl.Text = "any text we want!";

bmp.Render(bl, null);

var tt = new TranslateTransform();
tt.X = 173 - logo.PixelWidth;
tt.Y = 173 - logo.PixelHeight;

bmp.Render(img, tt);

bmp.Invalidate();
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
    var filename = "/Shared/ShellContent/testtile.jpg";
    using (var st = new IsolatedStorageFileStream(filename, FileMode.Create, FileAccess.Write, store))
    {
        bmp.SaveJpeg(st, 173, 173, 0, 100);
    }
}


ShellTile defaultTile = ShellTile.ActiveTiles.First();

defaultTile.Update(new StandardTileData
{
    Title = item2.NextDateTime.ToShortTimeString(),
    BackgroundImage = new Uri("isostore:/Shared/ShellContent/testtile.jpg", UriKind.Absolute),
    BackBackgroundImage = new Uri("isostore:/Shared/ShellContent/testtile.jpg", UriKind.Absolute),
    BackTitle = item2.NextDateTime.ToShortTimeString(),
    BackContent = item2.Details
});

有什么明显的事情我在这里做错了。这是在第一个示例中可以正常工作的相同文件和相同文件路径。

谢谢。

4

1 回答 1

3

您需要使用BitmapImage.ImageOpened事件来确保它正确绘制。

这意味着您应该像这样编写代码:

var logo = new BitmapImage(new Uri("Images/Tiles/tilebacking.png", UriKind.Relative));
logo.CreateOptions = BitmapCreateOptions.None;
logo.ImageOpened += (sender, e) =>
{
    var bmp = new WriteableBitmap(173, 173);

    var bl = new TextBlock();
    bl.Foreground = new SolidColorBrush(Colors.White);
    bl.FontSize = 24.0;
    bl.Text = "any text we want!";

    bmp.Render(bl, null);

    var tt = new TranslateTransform();
    tt.X = 173 - logo.PixelWidth;
    tt.Y = 173 - logo.PixelHeight;

    bmp.Render(img, tt);

    bmp.Invalidate();

    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        var filename = "/Shared/ShellContent/testtile.jpg";
        using (var st = new IsolatedStorageFileStream(filename, FileMode.Create, FileAccess.Write, store))
        {
            bmp.SaveJpeg(st, 173, 173, 0, 100);
        }
    }

    ...
}

我已经写了一个关于这个主题的指南:如何:直接在手机上生成自定义动态磁贴。

于 2011-09-11T21:34:21.453 回答