2011-12-24 69 views
3

我想制作一个应用程序,如this one。该应用程序可以使用文本制作拼贴。如何在Windows Phone应用程序中从StackPanel制作位图图像

我发现我无法轻松写入文本,因为StandardTileData类不具备写入文本的功能。 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 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> 

回答

1

也许该功能将帮助你:

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"); 

伊迪丝:看来我迟到了这个一但它可以帮助其他人;)

相关问题