C# 使用PngBitmapDecoder、MemoryStream、FlowDocument、XPSDocument预览图像
我不太清楚发生了什么,所以如果标题不具体,我很抱歉。我在下面提供了代码和xaml,它们演示了我的问题。我调用静态方法将位图转换为C# 使用PngBitmapDecoder、MemoryStream、FlowDocument、XPSDocument预览图像,c#,wpf,xaml,C#,Wpf,Xaml,我不太清楚发生了什么,所以如果标题不具体,我很抱歉。我在下面提供了代码和xaml,它们演示了我的问题。我调用静态方法将位图转换为字节[],反之亦然。这些方法用于将源代码绑定到图像控件时效果良好。然而,当我使用它们将源代码分配给BlockUIContainer的子图像时,正如代码所演示的。。。我在第二次调用ByteArrayToBitmapSource时得到了与上一次相同的图像 我不懂。然而,对我来说显而易见的是,第二个图像具有我期望它显示的图像的属性,但显然是错误的图像 C#main windo
字节[]
,反之亦然。这些方法用于将源代码绑定到图像控件时效果良好。然而,当我使用它们将源代码分配给BlockUIContainer的子图像时,正如代码所演示的。。。我在第二次调用ByteArrayToBitmapSource时得到了与上一次相同的图像
我不懂。然而,对我来说显而易见的是,第二个图像具有我期望它显示的图像的属性,但显然是错误的图像
C#main window.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);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.IO;
使用系统、绘图、成像;
使用系统图;
使用System.Windows.Xps.Packaging;
使用System.Windows.Xps;
命名空间流文档测试
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口,IDisposable
{
私人XpsDocument XpsDocument;
私有字符串随机文件名;
公共主窗口()
{
初始化组件();
this.DataContext=this;
FlowDocument文档=新的FlowDocument();
文件块添加(新段落(新运行(“测试”));
第1节=新节();
BlockUIContainer blockUIContainer1=新的BlockUIContainer();
blockUIContainer1.Child=new System.Windows.Controls.Image{Source=Source1};
第2节=新节();
BlockUIContainer blockUIContainer2=新的BlockUIContainer();
blockUIContainer2.Child=new System.Windows.Controls.Image{Source=Source2};
单据Blocks.Add(blockUIContainer1);
单据Blocks.Add(blockUIContainer2);
randomFileName=System.IO.Path.GetRandomFileName();
this.xpsDocument=新的xpsDocument(随机文件名,System.IO.FileAccess.ReadWrite);
XpsDocumentWriter=XpsDocument.CreateXpsDocumentWriter(XpsDocument);
writer.Write((IDocumentPaginatorSource)doc.DocumentPaginator);
this.Viewer.Document=xpsDocument.GetFixedDocumentSequence();
}
公共位图源1
{
得到
{
字节[]tmp=BitmapSourceToByteArray(GetBitmapImage(新位图(@“source1.jpg”));
返回ByteArrayToBitmapSource(tmp);
}
}
公共位图源代码2
{
得到
{
字节[]tmp=BitmapSourceToByteArray(GetBitmapImage(新位图(@“source2.bmp”));
返回ByteArrayToBitmapSource(tmp);
}
}
公共静态位图图像GetBitmapImage(位图)
{
BitmapImage BitmapImage=新的BitmapImage();
使用(MemoryStream stream=new MemoryStream())
{
保存(流,ImageFormat.Png);
流位置=0;
bitmapImage.BeginInit();
bitmapImage.StreamSource=流;
bitmapImage.CacheOption=BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
}
返回位图图像;
}
公共静态字节[]BitmapSourceToByteArray(BitmapSource BitmapSource)
{
字节[]数据;
PngBitmapEncoder编码器=新的PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
使用(MemoryStream ms=new MemoryStream())
{
编码器保存(ms);
data=ms.ToArray();
}
返回数据;
}
公共静态位图源ByteArrayToBitmapSource(字节[]数据)
{
位图源结果;
使用(MemoryStream ms=新的MemoryStream(数据))
{
PngBitmapDecoder解码器=新的PngBitmapDecoder(ms,BitmapCreateOptions.PreservePixelFormat,BitmapCacheOption.OnLoad);
结果=解码器。帧[0];
}
返回结果;
}
受保护的覆盖无效(System.ComponentModel.CancelEventArgs e)
{
基数(e);
这个。Dispose();
}
公共空间处置()
{
这个.xpsDocument.Close();
if(System.IO.File.Exists(randomFileName))
System.IO.File.Delete(随机文件名);
}
}
}
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>
我想这与返回位图源代码有关,而不是谨慎的位图图像。创建了这个方法,并调用了这个方法,它按照我的预期工作 仍然不知道这是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;
}