【问题标题】:How to open and split the most recent image from isolated storage (WP8)?如何从隔离存储(WP8)中打开和拆分最新的图像?
【发布时间】:2014-11-04 20:46:24
【问题描述】:

我目前正在开发一个可以执行以下任务的应用程序:

1- 拍照并将其保存在两个:独立存储和照片库中。

2-然后将图片加载到不同的页面中,同时被分成不同的部分。

所以我按照本教程学习如何拍摄并保存照片:

http://msdn.microsoft.com/en-us/library/windows/apps/hh202956(v=vs.105).aspx

现在我被困在查看和拆分不同页面中的最新图像的部分。

有什么解决方案或想法吗?

【问题讨论】:

  • 澄清不同页面/不同部分的含义。您是否只想将图像列表加载到单个屏幕上,然后让他们点击图像并将全分辨率图像加载到单独的页面上?
  • 抱歉不清楚,我想要的只是查看我的应用程序拍摄的最新照片

标签: c# windows-phone-8 isolatedstorage


【解决方案1】:

抱歉不清楚,我想要的只是一种查看我的应用程序拍摄的最新照片的方法

示例相机应用程序将照片保存到相机胶卷以及应用程序隔离存储中。这是他们使用的代码 sn-p。

// Save photo as JPEG to the local folder.
using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
    {
        // Initialize the buffer for 4KB disk pages.
        byte[] readBuffer = new byte[4096];
        int bytesRead = -1;

        // Copy the image to the local folder. 
        while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
        {
            targetStream.Write(readBuffer, 0, bytesRead);
        }
    }
}

如您所见,他们使用文件名fileName 保存它,因此您所要做的就是在您使用的所有fileName 中保留一个List<string>。每次保存新图像时,您都希望将 fileName 添加到列表中。

您可以使用 ApplicationSettings 保存列表

if (!IsolatedStorageSettings.ApplicationSettings.Contains("recent_images"))
{
    IsolatedStorageSettings.ApplicationSettings.Add("recent_images", YOUR_LIST);        
}
IsolatedStorageSettings.ApplicationSettings.Save();    

这样下次你加载应用程序时,你可以再次获得列表(所以基本上你已经墓碑化了列表)

List<string> recent_images = (List<string>) IsolatedStorageSettings.ApplicationSettings["recent_images"];

现在加载您最近的图片

<!-- create the container in xaml -->
<Image x:Name="myImage"></Image>


// this function loads an image from isolated storage and returns a bitmap
private static BitmapImage GetImageFromIsolatedStorage(string imageName)
{
    var bimg = new BitmapImage();
    using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (var stream = iso.OpenFile(imageName, FileMode.Open, FileAccess.Read))
        {
            bimg.SetSource(stream);
        }
    }
    return bimg;
}


// load the first image in recent images
BitmapImage first = GetImageFromIsolatedStorage(recent_images[0]);

// set the BitmapImage as the source of myImage to display it on the screen
this.myImage.Source = first;

只需逐行阅读他们的代码和我的代码。不跳过步骤也不会太难。

【讨论】:

  • 非常感谢!你是最棒的
  • 我收到此错误:BitmapImage first = GetImageFromIsolatedStorage(recent_images[0]; System.Windows.ni.dll 中发生“System.NullReferenceException”类型的未处理异常附加信息:对象引用未设置为一个对象的实例。
  • @AnasWaleed 断点是你的朋友——使用它们。首先,验证您实际上是在保存图像,MSDN 相机应用程序是否运行并将其保存到独立存储中?基本上在那个while循环中放一个断点@targetStream.Write(readBuffer, 0, bytesRead);你的断点命中了吗?
  • @AnasWaleed 如果是,那么下一步就是用BitmapImage first = GetImageFromIsolatedStorage("1.jpg"); 加载您保存的同一个文件,这样行吗?
  • @AnasWaleed 如果是,那么您错误地保存了您的List&lt;string&gt;,或者您忘记了List&lt;string&gt; recent_images = new List&lt;string&gt;();,或者您没有向列表中添加任何内容,导致 0 索引为空
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-06
  • 1970-01-01
相关资源
最近更新 更多