如何使用WritableBitmapEx從xaml控件創建位圖圖像。在我的winRT應用程序中,我需要爲我的窗口創建一個快照,以實現Pin to Tile(固定二級貼圖)。我發現winRT中缺少WritableBitmap.render()。我如何通過使用WritableBitmapEx來實現此功能。使用WritableBitmapEx從xaml控件創建位圖圖像
回答
不知怎的,他們錯過了實施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
的內容。
太好了。 RenderTargetBitmap.RenderAsync()是大多數場景的最佳解決方案。 – pauldendulk 2014-02-05 16:54:11
我一直在使用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);
}
}
}
}
這不是我所期待的答案。在這裏您演示了創建PinToTile的方式。我被問到的是不同的東西。我沒有一個圖像用我的手釘住;我的要求是,我用一些數據(例如網格或畫布)控制着我,我需要從該控件創建一個圖像,然後我需要將該圖像固定到屏幕上。希望你瞭解我的問題。我可以在wp7和wpf中使用WritableBitmap.Render()方法從控件創建圖像。但在WINRT中不。我需要解決這個問題。 – StezPet 2012-07-25 06:27:44
因爲這有人問WritableBitmapEx已更新,現在支持WinRT的 - 所以它應該只是工作。
有演示的一個夢幻般的量,以顯示這個和答案一批在SO問題:https://gist.github.com/2834070
WritableBitmapEx.Render在Windows Phone 8.1 RT上不可用,但它在8.1 Silverlight中可用。 – 2014-12-15 16:40:46
@ user925327與以前的版本相比,很可能缺少某些方法,但現在很多工作正在進行。 – 2014-12-17 07:37:45
- 1. 使用Sharpdx庫(WinRT)從xaml控件創建位圖圖像或內存流
- 2. XAML控件從PNG圖像
- 3. 從圖像創建位圖
- 4. 關於在C#中使用Xaml創建圖像控件#
- 5. Novacode Docx從位圖創建圖像
- 6. WP7:從位圖圖像創建電影
- 7. 從tiff圖像創建位圖對象
- 8. 綁定Xaml位圖圖像
- 9. 從現有圖像創建新位圖時不縮放圖像
- 10. 在xaml中創建圖像樣式
- 11. 從位圖圖像列表創建一個.avi文件
- 12. 如何從Android中的圖像文件創建位圖
- 13. 從原始頭文件和圖像數據創建位圖
- 14. 無法從圖像文件創建位圖
- 15. 從圖形創建圖像
- 16. 創建8位圖像
- 17. 使用小圖像創建圖像
- 18. XAML:將控件放在圖像上
- 19. 在WebBrowser控件中顯示XAML圖像
- 20. 使用FormattedText創建位圖
- 21. 使用PHP從一個圖像文件夾創建縮略圖
- 22. 如何從位圖創建`像素化'的SVG圖像?
- 23. 使用ImageIO從BLOB創建圖像?
- 24. 使用FFMPEG從圖像創建視頻
- 25. 使用iText從TIFF圖像創建PDF
- 26. 使用FFmpeg4Android從圖像創建視頻
- 27. 使用php從URL創建png圖像
- 28. 使用TCPDF從PDF創建圖像
- 29. WPF:創建動態創建的用戶控件的圖像
- 30. 爲動態創建用戶控件創建圖像
不能做 – Denis 2012-07-25 06:55:53