[英]WritableBitmap - Save custom live tile as Transparent PNG
創建WritableBitmap並將其保存為WritableBitmap時似乎有點問題,它似乎僅支持JPEG。 顯然,這是一個不便,我真的不知道該怎么辦。 如何將我的WritableBitmap保存為支持Windows Phone 8.1透明背景(帶有背景)的PNG文件?
請注意,我的目標是8.0。
我的代碼:
private static void CreateBitmap(int width, int height)
{
WriteableBitmap b = new WriteableBitmap(width, height);
var canvas = new Grid();
canvas.Width = b.PixelWidth;
canvas.Height = b.PixelHeight;
var background = new Canvas();
background.Height = b.PixelHeight;
background.Width = b.PixelWidth;
// Removed this area of code which contains the design for my live tile.
b.Render(background, null);
b.Render(canvas, null);
b.Invalidate(); //Draw bitmap
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream imageStream = new IsolatedStorageFileStream("/Shared/ShellContent/" + imagename + ".jpeg", System.IO.FileMode.Create, isf))
{
b.SaveJpeg(imageStream, b.PixelWidth, b.PixelHeight, 0, 100);
}
}
}
有一個名為Cimbalino WP的工具包,具有用於使用WriteableBitmap保存PNG的擴展方法。 NuGet上提供了Cimbalino,對於SavePng擴展方法,您需要獲取Background Components。
這是有關如何使用它的代碼示例:
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream imageStream = new IsolatedStorageFileStream("/Shared/ShellContent/" + imagename + ".png", System.IO.FileMode.Create, isf))
{
Cimbalino.Phone.Toolkit.Extensions.WriteableBitmapExtensions.SavePng(b, imageStream);
}
}
只需嘗試使用此代碼段即可。 似乎您可以在作為ImageTools
一部分提供的WriteableBitmap
上調用ToImage
擴展方法。
var img = bitmap.ToImage();
var encoder = new PngEncoder();
using (var stream = new IsolatedStorageFileStream(fileName, FileMode.Create, store))
{
encoder.Encode(img, stream);
stream.Close();
}
進一步了解這些線程。 使用WPF將WriteableBitmap保存到文件中 http://www.codeproject.com/Questions/272722/Converting-WriteableBitmap-to-a-Bitmap-for-Save
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.