来自base64和分配源的WPF转换文件-c#

来自base64和分配源的WPF转换文件-c#,c#,json,wpf,file,base64,C#,Json,Wpf,File,Base64,我从用户点击wpf列表(c#)的json文件中收集信息。 单击以调用json并将Base64中的文件内容传递给我。我会将其转换并立即显示,而无需将其写入用户的硬盘 以及“是否可以将其转换并保留在ram上,然后可以立即查看,并将其分配给对象的源imageViewer?” 多谢各位 <Grid> <Grid.RowDefinitions> <RowDefinition Height="40"></

我从用户点击wpf列表(c#)的json文件中收集信息。 单击以调用json并将Base64中的文件内容传递给我。我会将其转换并立即显示,而无需将其写入用户的硬盘

以及“是否可以将其转换并保留在ram上,然后可以立即查看,并将其分配给对象的源imageViewer?”

多谢各位

   <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="40"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <TextBlock FontFamily="{StaticResource Lato Thin}" Margin="10,0,0,0" Foreground="#007AFF" Text="{x:Static res:strings.indietroPage}" MouseDown="GoBackFrame_MouseDown" FontSize="20" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Center"/>

            <Image Grid.Row="1" Source="{Binding uriImageSource}" Name="imageViewer"></Image>
        </Grid>
我试过这个

fileSourceBytes = Convert.FromBase64String(attachmentDownload.B64Content);
                    BitmapImage bi = new BitmapImage();
                    bi.BeginInit();
                    bi.StreamSource = new MemoryStream(fileSourceBytes);
                    bi.EndInit();

                    imageViewer.Source = bi;
但收到一个错误:

对象引用未设置为对象的实例

解决方案: 代码隐藏

public Byte[] fileSourceBytes { get; set; }    
fileSourceBytes = Convert.FromBase64String(attachmentDownload.B64Content);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.StreamSource = new MemoryStream(fileSourceBytes);
bi.EndInit();

uriImageSource = bi;
xaml:


最好的解决方案可能是将
位图图像
绑定到
图像查看器的源属性

视图模型:

BitmapImage _imageViewerSource;

public BitmapImage ImageViewerSource {
    get { return _imageViewerSource; }
    private set
    { 
        _imageViewerSource = value;
        OnPropertyChanged("ImageViewerSource"); // or OnPropertyChanged(nameof(ImageViewerSource)); if you are using VS2015+
    }
}
Xaml:


这应该可以解决您的问题

using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
{
    using (DataWriter writer = new DataWriter(stream.GetOutputStreamAt(0)))
    {
        writer.WriteBytes(Convert.FromBase64String(base64););
        writer.StoreAsync().GetResults();
    }

    BitmapImage image = new BitmapImage();
    image.SetSource(stream);
}

当您收到错误时,哪个对象实例为空?imageViewer为空,但处于XAML中您的imageViewer控件尚未初始化。在尝试设置“
Source
属性之前,请确保正在调用窗口/UserControl的codebehind中的
InitializeComponent()
方法。(并确保具有
“imageViewer”
名称属性的控件存在。)
BitmapImage _imageViewerSource;

public BitmapImage ImageViewerSource {
    get { return _imageViewerSource; }
    private set
    { 
        _imageViewerSource = value;
        OnPropertyChanged("ImageViewerSource"); // or OnPropertyChanged(nameof(ImageViewerSource)); if you are using VS2015+
    }
}
<Image Source="{Binding ImageViewerSource, Mode=OneWay}"/>
using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
{
    using (DataWriter writer = new DataWriter(stream.GetOutputStreamAt(0)))
    {
        writer.WriteBytes(Convert.FromBase64String(base64););
        writer.StoreAsync().GetResults();
    }

    BitmapImage image = new BitmapImage();
    image.SetSource(stream);
}