如何在代码隐藏中将StreamSource分配给BitmapImage?

时间:2019-03-30 00:25:49

标签: c# wpf xaml

我在存储 FlowDocument Card.xaml )和包含图像的文件夹( Media )时有一个zip文件。我的 FlowDocument 中的图像具有 Tag ,其中存储了相对于FlowDocument的路径。对于使用FlowDocument( FindImages 方法)的图像搜索:Finding all images in a FlowDocument

我如何在 RichTextBox 中打开此zip。请注意我如何创建此图像(位图),也许是那里的问题,但我不明白这是怎么回事:

string nameOfXamlCardDefault = "Card.xaml";
    private void Open_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        OpenFileDialog dlg = new OpenFileDialog();
        if (dlg.ShowDialog() == true)
        {
            //Open zip file
            using (FileStream fullCardZipFile = File.Open(dlg.FileName, FileMode.Open, FileAccess.ReadWrite))
            {
                //Open zip by ZipArchive
                using (ZipArchive archive = new ZipArchive(fullCardZipFile, ZipArchiveMode.Update))
                {
                    //Get entry for xaml (FlowDocument)
                    ZipArchiveEntry xamlFileEntry = archive.GetEntry(nameOfXamlCardDefault);
                    //Open xaml
                    using (Stream xamlFileStreamInZip = xamlFileEntry.Open())
                    {
                        //Load FlowDocument into rtbEditor.Document
                        rtbEditor.Document = XamlReader.Load(xamlFileStreamInZip) as FlowDocument;
                        //Searching images
                        List<Image> images = FindImages(rtbEditor.Document).ToList();
                        foreach (var image in images)
                        {
                            var imageFileEntry = archive.GetEntry(image.Tag.ToString());
                            var bitmap = new BitmapImage();
                            using (Stream imageFileStream = imageFileEntry.Open())
                            {
                                var memoryStream = new MemoryStream();
                                imageFileStream.CopyTo(memoryStream);
                                bitmap.BeginInit();
                                bitmap.CacheOption = BitmapCacheOption.OnLoad;
                                bitmap.StreamSource = memoryStream;
                                bitmap.EndInit();
                                image.Source = bitmap;
                            }
                        }
                    }
                }
            }
        }
        return;
    }

RichTextBox 中的所有图像都能很好地显示,但是 BitmapImage 中没有 StreamSource 。以后会导致错误:

<FlowDocument xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" NumberSubstitution.CultureSource="User" AllowDrop="True" PagePadding="5,0,5,0">
<Paragraph>
    <Image Tag="Media/image0.png">
        <Image.Source>
            <BitmapImage CacheOption="OnLoad" BaseUri="{x:Null}"/>
        </Image.Source>
    </Image>
    <Image Tag="Media/image1.png">
        <Image.Source>
            <BitmapImage CacheOption="OnLoad" BaseUri="{x:Null}"/>
        </Image.Source>
    </Image>
</Paragraph>

如果仅复制图像并粘贴到 RichTextBox 中,则看起来像这样,这很好:

<Image Height="400" Width="600">
<Image.Source>
    <BitmapImage CacheOption="OnLoad" UriSource="./Image1.bmp" 
        BaseUri="pack://payload:,,wpf1,/Xaml/Document.xaml"/>
</Image.Source>

是否可以嵌入 zip 中的图像,例如复制并粘贴?我尝试使用剪贴板并与 MemoryStream 一起使用。但这没有帮助。

1 个答案:

答案 0 :(得分:1)

复制位图数据后,应通过设置其Position属性或调用其Seek()方法来倒回MemoryStream。

var imageFileEntry = archive.GetEntry(image.Tag.ToString());

if (imageFileEntry != null)
{
    using (var imageFileStream = imageFileEntry.Open())
    using (var memoryStream = new MemoryStream())
    {
        imageFileStream.CopyTo(memoryStream);
        memoryStream.Position = 0; // here

        var bitmap = new BitmapImage();
        bitmap.BeginInit();
        bitmap.CacheOption = BitmapCacheOption.OnLoad;
        bitmap.StreamSource = memoryStream;
        bitmap.EndInit();

        image.Source = bitmap;
    }
}

您也可以从流中解码BitmapImage来代替BitmapFrame

var imageFileEntry = archive.GetEntry(image.Tag.ToString());

if (imageFileEntry != null)
{
    using (var imageFileStream = imageFileEntry.Open())
    using (var memoryStream = new MemoryStream())
    {
        imageFileStream.CopyTo(memoryStream);
        memoryStream.Position = 0;

        image.Source = BitmapFrame.Create(
            memoryStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
    }
}