简体   繁体   English

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

[英]WritableBitmap - Save custom live tile as Transparent PNG

Having a bit of a problem creating a WritableBitmap and saving it being that WritableBitmap only seems to support JPEG. 创建WritableBitmap并将其保存为WritableBitmap时似乎有点问题,它似乎仅支持JPEG。 Obviously, this is quite an inconvenience and I really don't know what to do about it. 显然,这是一个不便,我真的不知道该怎么办。 How can I go about making my WritableBitmap save as a PNG file that supports Windows Phone 8.1 transparent tiles w/ background? 如何将我的WritableBitmap保存为支持Windows Phone 8.1透明背景(带有背景)的PNG文件

Note that i'm targeting 8.0. 请注意,我的目标是8.0。

My code: 我的代码:

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);
            }
        }
}

There is a toolkit called Cimbalino WP that has an extension method for saving PNGs using WriteableBitmap. 有一个名为Cimbalino WP的工具包,具有用于使用WriteableBitmap保存PNG的扩展方法。 Cimbalino is available on NuGet, for the SavePng extension method you need to get the Background Components. NuGet上提供了Cimbalino,对于SavePng扩展方法,您需要获取Background Components。

Here's a code sample on how to use it: 这是有关如何使用它的代码示例:

 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);

        }
    }

Just give a try using this snippet. 只需尝试使用此代码段即可。 Seems like you could call the ToImage extension method on your WriteableBitmap which is provided as part of ImageTools . 似乎您可以在作为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();
}

Have a look at these threads for more. 进一步了解这些线程。 Save WriteableBitmap to file using WPF http://www.codeproject.com/Questions/272722/Converting-WriteableBitmap-to-a-Bitmap-for-saving 使用WPF将WriteableBitmap保存到文件中 http://www.codeproject.com/Questions/272722/Converting-WriteableBitmap-to-a-Bitmap-for-Save

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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