0

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

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

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

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);
}

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

我该如何解决这个问题?谢谢

4

1 回答 1

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 方法参数。

于 2013-12-03T03:00:41.630 回答