如何在Windows Phone应用程序中从StackPanel制作位图图像

本文关键字:StackPanel 位图 图像 Windows Phone 应用程序 | 更新日期: 2023-09-27 18:18:41

我想做一个像这样的应用程序。该应用程序允许使用文本制作贴片。

我发现我不能很容易地在tile中编写文本,因为StandardTileData类没有这样的函数来在tile中编写文本。StandardTileData类只允许设置标题,背景图像,计数等。样品如下:

StandardTileData secondaryTile = new StandardTileData
{
    BackgroundImage = new Uri("/TileColors..png", UriKind.Relative),
    Title = "title",
    Count = null,
};
ShellTile.Create(new Uri("/MainPage.xaml?id=1", UriKind.Relative), secondaryTile);

所以,我认为我们可能需要使位图图像包含文本。我没有其他好主意。有人知道如何从stackpanel制作位图图像吗?

我的代码是这样的,

<StackPanel Height="173" Width="173" x:Name="TilePanel" Background="Wheat" >
    <TextBlock x:Name="tileText" Text="I'd like to add the text to a tile." Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left"  FontSize="20"/>
</StackPanel>

如何在Windows Phone应用程序中从StackPanel制作位图图像

也许这个函数能帮到你:

    public static void SaveToIsolatedStorage(FrameworkElement element, string file, bool scaled=true)
    {
        try
        {
            var bmp = new WriteableBitmap(element, null);
            IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
            double width = Math.Round(element.ActualWidth * ((double)Application.Current.Host.Content.ScaleFactor / 100f), MidpointRounding.AwayFromZero);
            double height = Math.Round(element.ActualHeight * ((double)Application.Current.Host.Content.ScaleFactor / 100f), MidpointRounding.AwayFromZero);
            if (!scaled)
            {
                width = element.ActualWidth;
                height = element.ActualHeight;
            }

            using (var stream = iso.CreateFile(file))
            {
                bmp.SaveJpeg(stream, (int)width, (int)height, 0, 100);
                stream.Close();
            }
        }
        catch
        {
        } 
    }

用法:

    SaveToIsolatedStorage(uiElement, "uiElement_as_screenshot.jpg");

伊迪丝:这件事我好像晚了,但它可以帮助其他人;