简体   繁体   English

如何正确地将Texture2D保存到MediaLibrary wp7

[英]How correctly to save texture2d to MediaLibrary wp7

The problem is in method Texture2D.SaveAsPng . 问题出在方法Texture2D.SaveAsPng中 I recently found out memory leak in this method in similar problem but managed to solve it. 我最近在类似问题中用这种方法发现了内存泄漏 ,但设法解决了。 But I can't fit that solution here. 但是我不能在这里使用该解决方案。 What I'm trying now is: 我现在正在尝试的是:

        MediaLibrary library = new MediaLibrary();
        MemoryStream ms = new MemoryStream();
        pic.SaveAsJpeg(ms, pic.Width, pic.Height);
        ms.Seek(0, SeekOrigin.Begin);
        library.SavePicture(path, ms);
        ms.Close();

And, every call I lose about 4mb of memory (texture dimension 800x620). 而且,每次通话我都会丢失大约4mb的内存(纹理尺寸800x620)。 I have tried to create MemoryStream from byte array but it throws Value does not fall within the expected range exception. 我试图从字节数组创建MemoryStream ,但是它抛出Value does not fall within the expected range异常。

        byte[] textureData = new byte[4 * picHeight * picWidth];
        pic.GetData(textureData);
        library.SavePicture(path, textureData); //exception on this line

So, I guess, I need to convert Texture2D in byte array so that library.SavePicture(path, ms) won't throw exception, but I have no clue how to do that. 因此,我想我需要在字节数组中转换Texture2D ,以便library.SavePicture(path, ms)不会引发异常,但是我不知道如何执行此操作。 Any help would be appreciated. 任何帮助,将不胜感激。

Note: memory leak in Texture2D.SaveAsJpeg occurs only on windows phone 7. 注意: Texture2D.SaveAsJpeg内存泄漏仅发生在Windows Phone 7上。

Upd: Memory stream length created from byte array from Texture.GetData is 1984000, when length of memory stream from Texture2D.SaveAsJpeg is 141520. UPD:从字节数组从创建的存储器流长度Texture.GetData是1984000,当从存储器流的长度Texture2D.SaveAsJpeg是141520。

I don't understand why you copied texture pic data in textureData but you never use it. 我不明白为什么您将纹理pic数据复制到textureData但从未使用过。
Maybe you need to do: 也许您需要做:

byte[] textureData = new byte[4 * picHeight * picWidth];
pic.GetData(textureData);
library.SavePicture(path, textureData); 

UPDATE 更新

According to MSDN for MediaLibrary.SavePicture : 根据MSDN的MediaLibrary.SavePicture

Image data that is passed in must be in a JPEG file format. 传入的图像数据必须为JPEG文件格式。 Additionally, SavePicture always saves the image in a JPEG file format. 此外,SavePicture始终将图像保存为JPEG文件格式。

It seems that the byte[] that you get from Texture2D.GetData is not in that format, but I don't know how to convert it. Texture2D.GetData获得的byte[]似乎不是这种格式,但是我不知道如何转换它。

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

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