C# 转换流时出错
我尝试从C# 转换流时出错,c#,windows-phone-7,windows-phone-8,windows-phone,isolatedstorage,C#,Windows Phone 7,Windows Phone 8,Windows Phone,Isolatedstorage,我尝试从IsolatedStorage获取图像流,然后为其分配位图图像,如下所示: using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(filePath, FileMode.Open, isolatedStorage)) { BitmapImage image = new BitmapImage(); image.SetSource(fileStream); return
IsolatedStorage
获取图像流,然后为其分配位图图像,如下所示:
using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(filePath, FileMode.Open, isolatedStorage))
{
BitmapImage image = new BitmapImage();
image.SetSource(fileStream);
return image;
}
但是在image.SetSource(fileStream)
行中,我得到了以下错误:
找不到该组件。(HRESULT的异常:0x88982F50)
更新我删除了使用block,但当它到达该行时,仍然会发生错误。也许我一开始写错了文件?。以下是我保存文件的步骤:
IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
if (!isolatedStorage.DirectoryExists("MyImages"))
{
isolatedStorage.CreateDirectory("MyImages");
}
var filePath = Path.Combine("MyImages", name + ".jpg");
using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(filePath, FileMode.Create, isolatedStorage))
{
StreamWriter sw = new StreamWriter(fileStream);
sw.Write(image);
sw.Close();
}
保存图片的代码错误。您正在写入的是
image.ToString()
的结果,而不是实际的图像。根据经验,记住StreamWriter
用于将字符串写入流。千万不要试图用它来写二进制数据
using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var fileStream = new IsolatedStorageFileStream(filePath, FileMode.Create, isolatedStorage))
{
var bitmap = new WriteableBitmap(image, null);
bitmap.SaveJpeg(fileStream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100);
}
}
保存图片的代码错误。您正在写入的是
image.ToString()
的结果,而不是实际的图像。根据经验,记住StreamWriter
用于将字符串写入流。千万不要试图用它来写二进制数据
using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var fileStream = new IsolatedStorageFileStream(filePath, FileMode.Create, isolatedStorage))
{
var bitmap = new WriteableBitmap(image, null);
bitmap.SaveJpeg(fileStream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100);
}
}
我不记得
SetSource
是立即加载内容还是稍后加载内容。你能不处理这条小溪就试试吗?(没有using
块)当无法找到图像文件的解码器时,将生成HResult。这当然可以通过你在BitmaImage有机会阅读文件之前猛拉地板垫并处理文件来解释。@KooKiz我已经更新了question@HansPassant我已经更新了question@HansPassant布尔西,看起来它不是有效图片我不记得是立即还是稍后加载内容。你能不处理这条小溪就试试吗?(没有using
块)当无法找到图像文件的解码器时,将生成HResult。这当然可以通过你在BitmaImage有机会阅读文件之前猛拉地板垫并处理文件来解释。@KooKiz我已经更新了question@HansPassant我已经更新了question@HansPassantBullseye,看起来这不是一张有效的照片