问题描述
我想一个位图保存到我的独立存储为PNG文件。我发现codePLEX称为ImageTools一个库,人们一直在建议,但是当我尝试它,并尝试打开该文件它说,它的损坏。任何知道我做错了吗?
私有静态无效SaveImageToIsolatedStorageAsPng(BitmapImage的位图,字符串文件名)
{
//转化为内存流
MemoryStream的MemoryStream的=新的MemoryStream();
WriteableBitmap的writableBitmap =新的WriteableBitmap的(位图);
writableBitmap.SaveJpeg(MemoryStream的,bitmap.PixelWidth,bitmap.PixelHeight,0,100); // EN code内存流为PNG
ExtendedImage形象=新ExtendedImage();
image.SetSource(MemoryStream的); PngEn codeR EN codeR =新PngEn codeR(); //保存到IsolatedStorage
使用(VAR店= IsolatedStorageFile.GetUserStoreForApplication())
使用(VAR writeStream =新IsolatedStorageFileStream(文件名,FileMode.Create,存储))
{
EN coder.En code(图像,writeStream);
}
}
您正在试图将JPEG存储数据流转换成PNG。这将使它损坏 - 你应该直接保存位图PNG
我没有尝试过与这个特殊的任务,但如果你的,它看起来像你需要调用在
扩展方法是作为ImageTools的一部分。然后你可以使用EN codeR借此形象,写出你的开放式流。 WriteableBitmap的
这ToImage
IMG VAR = bitmap.ToImage();
变种EN codeR =新PngEn codeR();
使用(VAR流=新IsolatedStorageFileStream(文件名,FileMode.Create,存储))
{
EN coder.En code(IMG,流);
stream.Close();
}
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);
}
}
You're attempting to convert the JPEG memory stream into PNG. That will make it corrupt - you should save the Bitmap directly to PNG.
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();
}
这篇关于保存位图PNG WP7上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!