繁体   English   中英

WritableBitmap-将自定义活动图块另存为透明PNG

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM