使用PngBitmapDecoder,MemoryStream,FlowDocument,XPSDocument来预览图像

时间:2014-05-01 20:25:37

标签: c# wpf xaml

我不太确定发生了什么,所以如果标题不具体,我会道歉。我已经提供了下面的代码和xaml来演示我的问题。我有静态方法,我调用将位图转换为byte [],反之亦然。当用于将源绑定到图像控件时,这些方法可以正常工作。但是,当我使用它们为像Block DeICrates的BlockUIContainer的子图像分配源时...我在第二次调用ByteArrayToBitmapSource时获得与之前相同的图像。

我无能为力。然而,对我来说显而易见的是,第二张图像具有我期望它显示的图像属性,但显然是错误的图像。

C#MainWindow.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 MainWindow.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>

1 个答案:

答案 0 :(得分:1)

我想这与返回一个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;
}