首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PngBitmapDecoder、MemoryStream、FlowDocument、XPSDocument预览图像

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

Stack Overflow用户
提问于 2014-05-01 20:25:37
回答 1查看 1.4K关注 0票数 0

我不太清楚发生了什么,所以如果标题不明确,我很抱歉。我提供了下面的代码和xaml,说明了我的问题。我调用静态方法将位图转换为byte [],反之亦然。当使用这些方法将源绑定到图像控制时,这些方法工作得很好。但是,当我使用它们为作为代码演示的BlockUIContainer的子元素的图像分配一个源时.在第二次调用ByteArrayToBitmapSource时,我得到了与前一次相同的图像。

我太笨了。然而,对我来说,显而易见的是,第二个图像具有我希望它显示的图像的属性,但显然是错误的图像。

C# MainWindow.xaml.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
 <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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-01 23:33:43

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

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

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23415984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档