使用PngBitmapDecoder、MemoryStream、FlowDocument、XPSDocument预览图像

本文关键字:XPSDocument 图像 FlowDocument PngBitmapDecoder MemoryStream 使用 | 更新日期: 2023-09-27 18:01:06

我不太确定发生了什么,所以如果标题不具体,我很抱歉。我已经提供了下面的代码和xaml,这说明了我的问题。我有一些静态方法,可以调用这些方法将位图转换为byte [],反之亦然。当使用这些方法将源绑定到图像控件时,效果良好。然而,当我使用它们为BlockUIContainer的子映像分配源时,代码演示。。。我在第二次调用ByteArrayToBitmapSource时得到了与上一次相同的图像。

我一无所知。然而,对我来说显而易见的是,第二个图像具有我期望它显示的图像的属性,但显然是错误的图像。

C#主窗口.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
using System.Windows.Xps.Packaging;
using System.Windows.Xps;
namespace FlowDocumentTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, IDisposable
    {
        private XpsDocument xpsDocument;
        private String randomFileName;
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            FlowDocument doc = new FlowDocument();
            doc.Blocks.Add(new Paragraph(new Run("Test")));
            Section section1 = new Section();
            BlockUIContainer blockUIContainer1 = new BlockUIContainer();
            blockUIContainer1.Child = new System.Windows.Controls.Image { Source = Source1 };
            Section section2 = new Section();
            BlockUIContainer blockUIContainer2 = new BlockUIContainer();
            blockUIContainer2.Child = new System.Windows.Controls.Image { Source = Source2 };
            doc.Blocks.Add(blockUIContainer1);
            doc.Blocks.Add(blockUIContainer2);
            randomFileName = System.IO.Path.GetRandomFileName();
            this.xpsDocument = new XpsDocument(randomFileName, System.IO.FileAccess.ReadWrite);
            XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
            writer.Write(((IDocumentPaginatorSource)doc).DocumentPaginator);
            this.Viewer.Document = xpsDocument.GetFixedDocumentSequence();
        }
        public BitmapSource Source1
        {
            get
            {
                byte[] tmp = BitmapSourceToByteArray(GetBitmapImage(new Bitmap(@"source1.jpg")));
                return ByteArrayToBitmapSource(tmp);
            }
        }
        public BitmapSource Source2
        {
            get
            {
                byte[] tmp = BitmapSourceToByteArray(GetBitmapImage(new Bitmap(@"source2.bmp")));
                return ByteArrayToBitmapSource(tmp);
            }
        }
        public static BitmapImage GetBitmapImage(Bitmap bitmap)
        {
            BitmapImage bitmapImage = new BitmapImage();
            using (MemoryStream stream = new MemoryStream())
            {
                bitmap.Save(stream, ImageFormat.Png);
                stream.Position = 0;
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = stream;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
            }
            return bitmapImage;
        }
        public static byte[] BitmapSourceToByteArray(BitmapSource bitmapSource)
        {
            byte[] data;
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
            using (MemoryStream ms = new MemoryStream())
            {
                encoder.Save(ms);
                data = ms.ToArray();
            }
            return data;
        }
        public static BitmapSource ByteArrayToBitmapSource(byte[] data)
        {
            BitmapSource result;
            using (MemoryStream ms = new MemoryStream(data))
            {
                PngBitmapDecoder decoder = new PngBitmapDecoder(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                result = decoder.Frames[0];
            }
            return result;
        }
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
            this.Dispose();
        }
        public void Dispose()
        {
            this.xpsDocument.Close();
            if (System.IO.File.Exists(randomFileName))
                System.IO.File.Delete(randomFileName);  
        }
    }
}

XAML主窗口.XAML

 <Window x:Class="FlowDocumentTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DocumentViewer Name="Viewer" />
    </Grid>
 </Window>

使用PngBitmapDecoder、MemoryStream、FlowDocument、XPSDocument预览图像

我想这与返回一个BitmapSource有关,而不是一个谨慎的BitmapImage。创建了这个方法并调用了这个方法,它正如我所期望的那样工作。

仍然不知道这是FlowDocument还是XPSDocument相关的问题。

public static BitmapSource GetBitmapImage(Bitmap bitmap)
{
    BitmapImage bitmapImage = new BitmapImage();
    using (MemoryStream stream = new MemoryStream())
    {
        bitmap.Save(stream, ImageFormat.Png);
        stream.Position = 0;
        bitmapImage.BeginInit();
        bitmapImage.StreamSource = stream;
        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapImage.EndInit();
    }
    return bitmapImage;
}