从路径wp8获取流

时间:2013-12-01 22:26:56

标签: c# windows-phone-8 isolatedstorage media-library

我想将图片库中选择的图像设置为背景。所以我在IsolatedStorageSetting中选择了所选照片的​​原始名称。但后来我无法从路径中获取文件流..这里是代码:

bitmapimage.UriSource = new Uri(Settings.BackgroundPhotoUrl, UriKind.Absolute);
BackgroundImg.ImageSource = bitmapimage;

但是这段代码不起作用。没有例外。只是背景是黑色的。 所以我试图在StreamStorageSetting中保存Stream(我不喜欢这个解决方案!!)但是在这种情况下我获得了一个例外:

Operation denied

这里是代码:

Settings.MyStream = e.ChosenPhoto

最后,我尝试将图像保存在独立存储中:

using (System.IO.IsolatedStorage.IsolatedStorageFile isf = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
{
    isf.CopyFile(e.OriginalFileName, "background" + System.IO.Path.GetExtension(e.OriginalFileName), true);
}

但在这种情况下我也获得了操作拒绝异常

我该如何解决问题?感谢名单

1 个答案:

答案 0 :(得分:0)

似乎你误解了溪流。流是指向您可以读取或写入的文件中的位置的指针。如果您正在使用照片选择器,那么结果将为您提供文件流。您需要从流中读取字节,然后保存本地存储。然后你可以从那里访问图像。

在我的Live Countdown应用程序中,我使用WriteableBitmap类将Jpeg保存到流中。有点像这样:

var store =     
    System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
var newPath = "MyFileName.png";

if (store.FileExists(newPath)) store.DeleteFile(newPath);

var stream = store.CreateFile(newPath);

BitmapImage i = new BitmapImage();
i.SetSource(photoResult.ChosenPhoto);
WriteableBitmap imageToSave = new WriteableBitmap(i);
imageToSave.SaveJpeg(stream, 173, 173, 0, 100);

stream.Flush();
stream.Close();

这是一种流动。我不得不从不同的功能中取出部件并将它们放在一起,因为应用程序允许用户首先缩放应用程序。在保存图块的图像时,会对SaveJpeg方法参数进行缩放。

相关问题