【问题标题】:Retrieving multiple image from isolated storage从隔离存储中检索多个图像
【发布时间】:2011-06-23 07:51:50
【问题描述】:

我将图像保存到独立存储中,每个图像都有不同的 imageFileName。但我在检索列表框中所有保存的图像时遇到问题。 仅设法检索保存的最新图像。 当我对文件路径进行硬编码时,可以检索它。 我希望任何人都可以帮助我编写代码。希望任何人都可以尝试编辑我的代码。谢谢。

保存代码:

private void SaveToLocalStorage(string imageFolder, string imageFileName)
        {
            imageFileName = App.imagePath;

            var isf = IsolatedStorageFile.GetUserStoreForApplication();
            if (!isf.DirectoryExists(imageFolder))
            {
                isf.CreateDirectory(imageFolder);
            }

            string filePath = Path.Combine(imageFolder, imageFileName);
            using (var stream = isf.CreateFile(filePath))
            {
                var bmp = new WriteableBitmap(inkCanvas, inkCanvas.RenderTransform);
                bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
            }
            MessageBox.Show(filePath            }

检索代码:

private void LoadFromLocalStorage(string imageFolder, string imageFileName )
        {
            var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
            if (!isoFile.DirectoryExists(imageFolder))
            {
                isoFile.CreateDirectory(imageFolder);
            }

            string filePath = Path.Combine(imageFolder, imageFileName);
            using (var imageStream = isoFile.OpenFile(filePath, FileMode.Open, FileAccess.Read))
            {
                var imageSource = PictureDecoder.DecodeJpeg(imageStream);

                BitmapImage bi = new BitmapImage();

                ListBoxItem item = new ListBoxItem();
                bi.SetSource(imageStream);
                item.Content = new Image() { Source = bi, MaxHeight = 100, MaxWidth = 100 };
                listBox1.Items.Add(item);
            }
}

【问题讨论】:

    标签: windows-phone-7 isolatedstorage


    【解决方案1】:

    尝试类似:

    私人无效LoadFromLocalStorage(字符串imageFolder) { var isoFile = 隔离存储文件.GetUserStoreForApplication(); // 检查目录是否存在 如果(!isoFile.DirectoryExists(imageFolder)) { throw new Exception("找不到图片目录"); } // 清除列表框 listBox1.Items.Clear(); // 获取文件 foreach(isoFile.GetFileNames() 中的字符串文件名) { string filePath = Path.Combine(imageFolder, fileName); 使用(var imageStream = isoFile.OpenFile(filePath, FileMode.Open, FileAccess.Read)) { var imageSource = PictureDecoder.DecodeJpeg(imageStream); BitmapImage bi = new BitmapImage(); ListBoxItem 项 = 新 ListBoxItem(); bi.SetSource(imageStream); item.Content = new Image() { Source = bi, MaxHeight = 100, MaxWidth = 100 }; listBox1.Items.Add(item); } } }

    【讨论】:

    • 嗨.. 是否可以检索指定图像出来?就像从孤立的存储中只检索第二个或第三个
    • @ben tan:当然,只要在foreach() 中添加一个if() 条件@ 即continue; 是否应该忽略图像...
    【解决方案2】:

    很难说没有看到更多代码,但看起来您只是检索一个文件。在您的代码的其他地方,您是否获得了应位于 IsolateStorage 目录中的所有文件的列表并循环访问它们?您是否看到任何错误消息,还是只是静默失败?

    【讨论】:

    • 这是我保存按钮的代码.. SaveToLocalStorage("imageFolder", App.imagePath);这是我检索按钮的代码.. LoadFromLocalStorage("imageFolder", imageFileName);没有其他代码..我应该如何进行循环并获取目录中所有文件的列表?没有错误。你能给我举个例子吗??或者可以只编辑代码..谢谢..将不胜感激..
    • @Evon Chong:“App.imagePath”听起来像一个静态字符串或 URI 变量,因此它只包含一个图像路径。难怪你只得到一张图像。您需要一个字符串或 URI 集合来迭代和检索每张图片。
    • 我的 App.imagePath 是 App.imagePath = String.Format("PhotoNote_{0:yyyy-MM-dd_hh-mm-ss-tt}.jpg", DateTime.Now);
    • hmm.. 我应该如何修改我的代码?你能帮我吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多