如何转换“System.IO.IsolatedStorage.IsolatedStorageFileStream”到图像源?

发布于 2024-09-25 06:24:53 字数 798 浏览 3 评论 0原文

问题很清楚:我正在尝试将“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!
                }
            }
        }
    }

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無心 2024-10-02 06:24:53

下面是在加载中使用您的代码的完整工作应用程序。

您可以选择 PNG 文件保存到独立存储,然后将文件重新加载到显示的图像。我注意到的一件事是,您必须小心保存流关闭并且 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;
                    }
                }
            }
        }
    }
}

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;
                    }
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文