gpt4 book ai didi

c# - 如何将 'System.IO.IsolatedStorage.IsolatedStorageFileStream' 转换为 ImageSource?

转载 作者:太空狗 更新时间:2023-10-30 00:46:15 25 4
gpt4 key购买 nike

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

谢谢。

最佳答案

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

您可以选择 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;
}
}
}
}
}
}

关于c# - 如何将 'System.IO.IsolatedStorage.IsolatedStorageFileStream' 转换为 ImageSource?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3788610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com