WritableBitmap -将自定义活动tile保存为透明PNG

本文关键字:保存 透明 PNG tile 活动 自定义 WritableBitmap | 更新日期: 2023-09-27 18:09:43

创建WritableBitmap并保存它有点问题,WritableBitmap似乎只支持JPEG。显然,这很不方便,我真的不知道该怎么做。我怎么能去使我的WritableBitmap保存为PNG文件,支持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);
            }
        }
}

WritableBitmap -将自定义活动tile保存为透明PNG

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

尝试使用这个代码片段。似乎你可以在你的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();
}

查看这些线程了解更多信息。使用WPF保存WriteableBitmap到文件http://www.codeproject.com/Questions/272722/Converting-WriteableBitmap-to-a-Bitmap-for-saving

希望有帮助!