2
votes

I m loading an image path from isolated storage settings in my application.

    [DataMember]
    public string entryImage = "";

    [DataMember]
    public string EntryImage
    {
        get { return entryImage; }
        set { entryImage = value; }
    }

using a helper class to store the image into isolated storage file.

    public static void SaveImage(Stream imageStream, string directory, string filename)
    {
        try
        {
            string path = System.IO.Path.Combine(directory, filename);

            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!isoStore.DirectoryExists(directory)) isoStore.CreateDirectory(directory);

                using (var writeStream = isoStore.CreateFile(path))
                {
                    byte[] buffer = new byte[32768];
                    while (true)
                    {
                        int read = imageStream.Read(buffer, 0, buffer.Length);

                        if (read <= 0)
                            return;
                        writeStream.Write(buffer, 0, read);
                    }
                }
            }

        }
        // Catch exception if unable to save the image
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

Here is the part where I store the imagepath to observablecollection

        MyDiaryItem _saveItems = new MyDiaryItem();
        _saveItems.EntryNotes = InputText.Text;
        _saveItems.EntryDate = date.ToString();
        _saveItems.EntryImage = AppHelper.ImageDirectory + AppSettings.ImageFilename;

Where MyDiaryItem is the observable collection

 public ObservableCollection<MyDiaryItem> diaryItems = null;

Isolated storage save and load

         void LoadSettings()
    {
        if (settings.Contains("DiaryItems"))
        {
            diaryItems = new ObservableCollection<MyDiaryItem>((List<MyDiaryItem>)settings["DiaryItems"]);
        }
    }

    void SaveSettings()
    {
        //settings["DiaryItems"] = diaryItems.ToList();
        if (diaryItems.ToList() != null)
        {
            settings.Clear();
            settings.Add("DiaryItems", diaryItems.ToList());
            settings.Save();
        }
    }

Here is the xaml code for image source

            <ListBox toolkit:TiltEffect.IsTiltEnabled="true" Name="AllEntriesList"
                     Margin="0,0,-12,0"
                     SelectionChanged="AllEntriesList_SelectionChanged">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                            <Image Source="{Binding EntryImage}" Height="100" Width="100" Stretch="Fill" Margin="12,0,9,0" />
                            <StackPanel Margin="0,0,0,17" Width="350" Height="Auto">
                                <TextBlock Text="{Binding EntryLocation}" TextWrapping="Wrap" Style="{StaticResource PhoneTextLargeStyle}" />
                                <TextBlock Text="{Binding EntryNotes}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}" />
                                <TextBlock Text="{Binding EntryDate}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

I want to some how use the imagepath retrived from isolated storage to display the image in the diaryitems list.

I m loading all the diaryitems in my OnNavigatedTo function like this.

AllEntriesList.ItemsSource = app.diaryItems;

I can see the image name populated properly in the diaryItems list. I want to display the image in the diaryItems list. How to do that ?

1
@MyKuLLSKI - Please do not edit the question to say it might be a duplicate. That is what the close or comments are for.Emond Erno
@Erno - please show me a link where this rule is quoted. ThanksMyKuLLSKI
Moderator Note Comments under this question have been removed because they deteriorated into more noise than signal. If you feel a question is a duplicate, flag it or vote to close it as such, or leave a comment pointing to the possible duplicate if unsure. Do not edit the original post to suggest the duplicate.Tim Post
if the question is a duplicate could you please give me the link to the existing question so that I can find answers.Mugu

1 Answers

1
votes
 <Image Source="{Binding EntryImage}" Height="100" Width="100" Stretch="Fill" Margin="12,0,9,0" />

you are binding a string to the image source. try to bind it to BitmapSource
you can easily get the BitmapSource from a stream. For example:

 BitmapSource CreateSource(Stream stream)
 { 
     return source = PictureDecoder.DecodeJpeg(stream);
 }