【问题标题】:How to assign StreamSource to BitmapImage in codebehind?如何在代码隐藏中将 StreamSource 分配给 BitmapImage?
【发布时间】:2019-04-01 14:00:47
【问题描述】:

我在其中存储 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 嵌入图像,例如复制和粘贴?我尝试使用 Clipboard 并使用 MemoryStream。但这没有帮助。

【问题讨论】:

  • 建议的解决方案有效吗?
  • @Clemens 不。但我知道我试图重新发明轮子。 XamlPackage 完美地完成了这项工作。我之前知道 XamlPackage,但最近我发现它只是一个 zip 文件。我应该以某种方式结束这个问题吗?也许您对此有所了解:stackoverflow.com/questions/55441272/…??

标签: c# wpf xaml


【解决方案1】:

您应该在复制位图数据后回退 MemoryStream,方法是设置其Position 属性或调用其Seek() 方法。

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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多