在 WP7 上将位图另存为 PNG [英] Saving Bitmap as PNG on WP7

查看:21
本文介绍了在 WP7 上将位图另存为 PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将位图作为 png 文件保存到我的独立存储中.我在 Codeplex 上找到了一个名为 ImageTools 的库,人们一直在推荐它,但是当我尝试它并尝试打开该文件时,它说它已损坏.有谁知道我做错了什么?

I'm trying to save a bitmap to my isolated storage as a png file. I found a library on Codeplex called ImageTools which people have been recommending but when i try it and attempt to open the file it says that its corrupt. Any know what i am doing wrong?

private static void SaveImageToIsolatedStorageAsPng(BitmapImage bitmap, string fileName)
{
    //convert to memory stream
    MemoryStream memoryStream = new MemoryStream();
    WriteableBitmap writableBitmap = new WriteableBitmap(bitmap);
    writableBitmap.SaveJpeg(memoryStream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100);

    //encode memory stream as PNG
    ExtendedImage image = new ExtendedImage();
    image.SetSource(memoryStream);

    PngEncoder encoder = new PngEncoder();

    //Save to IsolatedStorage
    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    using (var writeStream = new IsolatedStorageFileStream(fileName, FileMode.Create, store))
    {
        encoder.Encode(image, writeStream);
    }
}

推荐答案

您正在尝试将 JPEG 内存流转换为 PNG.这将使它损坏 - 您应该将位图直接保存为 PNG.

You're attempting to convert the JPEG memory stream into PNG. That will make it corrupt - you should save the Bitmap directly to PNG.

我还没有用 imagetools 库 尝试过这个特殊的任务,但是如果你看约翰爸爸的博客,看来需要调用您的 WriteableBitmap 上的 ToImage 扩展方法,它作为 ImageTools 的一部分提供.然后,您可以使用编码器拍摄此图像并将其写入您的开放流中.

I haven't tried this particular task with the imagetools library, but if you see John Papa's blog, it looks like you need to call the ToImage extension method on your WriteableBitmap which is provided as part of ImageTools. Then you can use the encoder to take this image and write out to your open stream.

var img = bitmap.ToImage();
var encoder = new PngEncoder();
using (var stream = new IsolatedStorageFileStream(fileName, FileMode.Create, store))
{
    encoder.Encode(img, stream);
    stream.Close();
}

这篇关于在 WP7 上将位图另存为 PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆