创建一个WritableBitmap并保存它只是一个问题,因为WritableBitmap似乎只支持JPEG。显然,这是一个非常不方便,我真的不知道该怎么办。如何将我的WritableBitmap保存为支持Windows Phone 8.1透明磁贴w /背景的
请注意,我的定位是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);
}
}
}
答案 0 :(得分:2)
有一个名为Cimbalino WP的工具包,它有一个使用WriteableBitmap保存PNG的扩展方法。 Cimbalino可以在NuGet上获得,用于获取背景组件所需的SavePng扩展方法。
以下是有关如何使用它的代码示例:
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);
}
}
答案 1 :(得分:0)
只需尝试使用此代码段即可。您似乎可以在ToImage
上调用WriteableBitmap
扩展方法,该方法是ImageTools
的一部分。
var img = bitmap.ToImage();
var encoder = new PngEncoder();
using (var stream = new IsolatedStorageFileStream(fileName, FileMode.Create, store))
{
encoder.Encode(img, stream);
stream.Close();
}
详细了解这些主题。 Save WriteableBitmap to file using WPF http://www.codeproject.com/Questions/272722/Converting-WriteableBitmap-to-a-Bitmap-for-saving
希望它有所帮助!