C# WritableBitmap-将自定义活动磁贴另存为透明PNG

C# WritableBitmap-将自定义活动磁贴另存为透明PNG,c#,image,windows-phone-8,bitmap,C#,Image,Windows Phone 8,Bitmap,创建可写位图并将其保存为只支持JPEG的可写位图时遇到了一些问题。显然,这给我带来了很大的不便,我真的不知道该怎么办。如何将我的可写位图另存为支持Windows Phone 8.1透明平铺和背景的PNG文件? 请注意,我的目标是8.0 我的代码: private static void CreateBitmap(int width, int height) { WriteableBitmap b = new WriteableBitmap(width, height);

创建可写位图并将其保存为只支持JPEG的可写位图时遇到了一些问题。显然,这给我带来了很大的不便,我真的不知道该怎么办。如何将我的可写位图另存为支持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);
            }
        }
}

请尝试使用此代码段。似乎您可以在
WriteableBitmap
上调用
ToImage
扩展方法,该方法是作为
ImageTools
的一部分提供的

var img = bitmap.ToImage();
var encoder = new PngEncoder();
using (var stream = new IsolatedStorageFileStream(fileName, FileMode.Create, store))
{
encoder.Encode(img, stream);
stream.Close();
}
更多信息,请查看这些线程。


希望有帮助

有一个名为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);

        }
    }