4

質問は明らかです。「System.IO.IsolatedStorage.IsolatedStorageFileStream」をImageSourceに変換しようとしていますが、これを行う方法がわかりません。バイトの配列をImagesourceに変換することについて説明している記事をいくつか見ましたが、ISFileStreamsについては何もありません。誰かが解決策や進め方の例を持っているなら、私に知らせてください。

私のコード:

private void Files_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (store.FileExists(Path.Combine("wallpaper", Files_List.SelectedValue.ToString())))
            {
                using (var isoStream = store.OpenFile(Path.Combine("wallpaper", Files_List.SelectedValue.ToString()), FileMode.Open))
                {
                    //Here is where I want to set an ImageSource from isoStream!
                }
            }
        }
    }

ありがとうございました。

4

1 に答える 1

7

以下は、ロードでコードを使用した完全に機能するアプリケーションです。

PNG ファイルを選択して分離ストレージに保存し、そのファイルを表示中の画像に再読み込みできます。私が気づいたことの 1 つは、保存ストリームが閉じていることと、PNG に互換性があることに注意する必要があることです。

Xaml:

<UserControl x:Class="IsoStorageSilverlightApplication.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <StackPanel x:Name="LayoutRoot" Background="White">
        <Button Content="Save to Iso" Width="100" Name="saveButton" Click="saveButton_Click" Margin="10"/>
        <Button Content="Load from Iso" Width="100" Name="loadButton" Click="loadButton_Click" />
        <Image Name="image1" Stretch="Fill" Margin="10"/>
    </StackPanel>
</UserControl>

コードビハインド:

using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace IsoStorageSilverlightApplication
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "PNG Files (.png)|*.png|All Files (*.*)|*.*";
            dialog.FilterIndex = 1;
            if (dialog.ShowDialog() == true)
            {
                System.IO.Stream fileStream = dialog.File.OpenRead();

                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    // Create a directory at the root of the store.
                    if (!store.DirectoryExists("Images"))
                    {
                        store.CreateDirectory("Images");
                    }

                    using (IsolatedStorageFileStream isoStream = store.OpenFile(@"Images\UserImageFile.png", FileMode.OpenOrCreate))
                    {
                        byte[] bytes = new byte[fileStream.Length];
                        fileStream.Read(bytes, 0, (int)fileStream.Length);
                        isoStream.Write(bytes, 0, (int)fileStream.Length);
                    }
                }
            }
        }

        private void loadButton_Click(object sender, RoutedEventArgs e)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (store.FileExists(@"Images\UserImageFile.png"))
                {
                    using (var isoStream = store.OpenFile(@"Images\UserImageFile.png", FileMode.Open, FileAccess.Read))
                    {
                        var len = isoStream.Length;
                        BitmapImage b = new BitmapImage();
                        b.SetSource(isoStream);
                        image1.Source = b;
                    }
                }
            }
        }
    }
}
于 2010-09-24T15:57:59.957 に答える