2012-07-24 20 views
4

如何使用WritableBitmapEx從xaml控件創建位圖圖像。在我的winRT應用程序中,我需要爲我的窗口創建一個快照,以實現Pin to Tile(固定二級貼圖)。我發現winRT中缺少WritableBitmap.render()。我如何通過使用WritableBitmapEx來實現此功能。使用WritableBitmapEx從xaml控件創建位圖圖像

+0

不能做 – Denis 2012-07-25 06:55:53

回答

9

不知怎的,他們錯過了實施WriteableBitmap的。 Render()雖然從我聽到的情況來看它可能會出現在SDK的更高版本中,但現在您的選擇是使用WriteableBitmap並使用控件的內容逐個像素地填充它,也許在WriteableBitmapEx的幫助下或者使用DirectX,或許在SharpDX的幫助下進行,這是DirectX的包裝,可以在C#應用程序中使用。如果要渲染任何文本,則需要使用DirectX/Direct2D/DirectWrite。如果您的用戶界面很簡單,那麼使用DirectX並不難。有一個樣本似乎是開始here的好地方。

編輯*

然後還有一個WriteableBitmap.Render() extension method我開始在WinRT XAML Toolkit具有用於渲染視覺樹比較有限,但可能有助於支持實施。我計劃將其擴展爲在必要時支持更多的UI元素。它目前支持典型的TextBlocks和具有純色或漸變背景的形狀。

EDIT 2 *

Windows 8.1中增加了RenderTargetBitmap.RenderAsync() API,這是相當有幫助的,儘管它有一定的侷限性例如它需要渲染的用戶界面成爲可視化樹的一部分(雖然它可以隱藏在一個面板中),沒有視頻播放,DirectX內容,我也相信WebView的內容。

+0

太好了。 RenderTargetBitmap.RenderAsync()是大多數場景的最佳解決方案。 – pauldendulk 2014-02-05 16:54:11

0

我一直在使用Windows.Storage.Streams中的RandomAccessStreamReference類來創建位圖。一個例子(這實際上是共享代碼):

  var reference = RandomAccessStreamReference.CreateFromUri(new Uri(item.ImagePath.AbsoluteUri)); 
      request.Data.SetBitmap(reference); 

也請記住,對於牽制二次瓷磚,你可以在一個URI傳遞的圖塊上的標誌,而不產生實際的位圖,只要標誌是你的應用程序包的一部分,就像這樣:

 var uri = new Uri(item.TileImagePath.AbsoluteUri); 

     var tile = new SecondaryTile(
       item.UniqueId,    // Tile ID 
       item.ShortTitle,   // Tile short name 
       item.Title,     // Tile display name 
       item.UniqueId,    // Activation argument 
       TileOptions.ShowNameOnLogo, // Tile options 
       uri       // Tile logo URI 
      ); 

     await tile.RequestCreateAsync(); 

最後,如果你想在次平鋪使用您的形象,而不是你的應用程序包的一部分在網上,你將必須在本地複製然後才能使用它。下面是一些代碼,做的是:

 // This is the URI that you will then pass as the last parameter into 
     // the Secondary Tile constructor, like the code above: 
     var logoUri = await GetLocalImageAsync(restaurant.ImagePath, restaurant.Key); 

     // and here's the method that does the meat of the work: 

    /// <summary> 
    /// Copies an image from the internet (http protocol) locally to the AppData LocalFolder. 
    /// This is used by some methods (like the SecondaryTile constructor) that do not support 
    /// referencing images over http but can reference them using the ms-appdata protocol. 
    /// </summary> 
    /// <param name="internetUri">The path (URI) to the image on the internet</param> 
    /// <param name="uniqueName">A unique name for the local file</param> 
    /// <returns>Path to the image that has been copied locally</returns> 
    private async Task<Uri> GetLocalImageAsync(string internetUri, string uniqueName) 
    { 
     if (string.IsNullOrEmpty(internetUri)) 
     { 
      return null; 
     } 

     using (var response = await HttpWebRequest.CreateHttp(internetUri).GetResponseAsync()) 
     { 
      using (var stream = response.GetResponseStream()) 
      { 
       var desiredName = string.Format("{0}.jpg", uniqueName); 
       var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(desiredName, CreationCollisionOption.ReplaceExisting); 

       using (var filestream = await file.OpenStreamForWriteAsync()) 
       { 
        await stream.CopyToAsync(filestream); 
        return new Uri(string.Format("ms-appdata:///local/{0}.jpg", uniqueName), UriKind.Absolute); 
       } 
      } 
     } 
    } 
+0

這不是我所期待的答案。在這裏您演示了創建PinToTile的方式。我被問到的是不同的東西。我沒有一個圖像用我的手釘住;我的要求是,我用一些數據(例如網格或畫布)控制着我,我需要從該控件創建一個圖像,然後我需要將該圖像固定到屏幕上。希望你瞭解我的問題。我可以在wp7和wpf中使用WritableBitmap.Render()方法從控件創建圖像。但在WINRT中不。我需要解決這個問題。 – StezPet 2012-07-25 06:27:44

2

因爲這有人問WritableBitmapEx已更新,現在支持WinRT的 - 所以它應該只是工作。

有演示的一個夢幻般的量,以顯示這個和答案一批在SO問題:https://gist.github.com/2834070

+0

WritableBitmapEx.Render在Windows Phone 8.1 RT上不可用,但它在8.1 Silverlight中可用。 – 2014-12-15 16:40:46

+0

@ user925327與以前的版本相比,很可能缺少某些方法,但現在很多工作正在進行。 – 2014-12-17 07:37:45