【发布时间】:2014-04-17 07:31:19
【问题描述】:
我需要将 Texture2D 转换为 BitmapImage,并且我找到了一种方法来做到这一点,但是 windows phone 7 上的这个解决方案在 Texture2D.SaveAsJpeg 方法调用中存在内存泄漏问题。我尝试过:通过调用 Texture2D.SaveAsJpeg 将 texture2d 保存在隔离存储中,然后使用此纹理从隔离存储流加载并从该流创建 BitmapImage
public static void SaveTextureToISF(string fileName, Texture2D texture)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
using (
IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(fileName, FileMode.Create, file)
)
{
texture.SaveAsPng(fileStream, texture.Width, texture.Height); //Memory leak Here
fileStream.Close();
}
}
}
public static BitmapImage LoadTextureFrom(string fileName)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
using (
IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(fileName,
FileMode.Open,
FileAccess.Read, file))
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(fileStream);
return bmp;
}
}
}
任何其他解决方案,不会泄漏?
【问题讨论】:
-
我建议的解决方案是如何在 ISF 中保存 textures2d 的解决方法。但是这个关于将texture2d转换为BitmapImage的问题。
-
对不起,没有注意到其他解决方案不适用于WinPhone。
-
我想这样做的方法是使用
GetData方法接收原始Texture2D数据,然后手动转换它。为此,我已经看到很多对imagetools.codeplex.com 的引用,但找不到示例。这个适用于桌面 XNA benbarefield.com/blog/2009/03/04/bitmap-from-texture2d 但也许它可以给你任何想法.. -
Imagetools 解决了这个问题,我稍后会发布解决方案
标签: c# windows-phone-7 xna