views:

76

answers:

1

The question is clear: I am trying to Convert an "System.IO.IsolatedStorage.IsolatedStorageFileStream" to an ImageSource but have no clue of how I could do this. I've seen severals articles that talk about converting arrays of bytes to Imagesource, but nothing about ISFileStreams. If someone has a solution or an example on how to proceed please let me know.

My code:

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!
                }
            }
        }
    }

Thank you.

+3  A: 

Below is a complete working application using your code in the load.

You can select PNG files to save to isolated storage then reload the file to the image on display. One thing I noticed is that you must be careful that the saving stream closes and that the PNG is compatible:

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>

Code behind:

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;
                    }
                }
            }
        }
    }
}
Enough already
I had tried that code before and it was giving the following Error: "Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))"
Ephismen
Enough already
@HiTech Magic: First of all thank you for the code you provided, I copied it exactly as it was and adapted my XAML to it. But I am still getting the same error whatever I do. It's still the b.SetSource(isoStream) parts that is problematic. At this point I have no idea on what to do.
Ephismen
@Ephismen: Have you tried my example as a standalone SL application first, to see if there is a problem with the specific images you are trying to show? I had errors only with specific PNG files, so it may be fussy about the exact format used. If you can post any images (or contact us through our website) we can try them here. Cheers.
Enough already
You were right. I copied all of your code in a Standalone application and it worked well with *.PNG files. But on some BMP files I checked the length when I saved them into the store and the length when I was loading them, I was surprised to see that their size had sometimes doubled. I'm going to review the code in my application. I'll let you know when it works anyway thanks for your observations.
Ephismen
@Ephismen: Silverlight does not support BMP files (JPG and PNG only). Were you converting them?
Enough already
I was converting them, but I guess that the problem wasn't coming from that anyway, it was more with how I was saving them into the IsolatedStorage. With the example you provided everything works perfectly with PNG and JPEG. Thanks again!
Ephismen