【发布时间】:2014-06-23 07:12:43
【问题描述】:
我有 10 个System.Drawing.Image。我需要将它们添加到FixedDocument。我尝试了下面的代码,并生成了固定文档,所有 10 页仅包含第一张图片。
FixedDocument doc = new FixedDocument();
BitmapSource[] bmaps = new BitmapSource[10];
System.Drawing.Image[] drawingimages = //I have System.Drawing.Image in a array
for (int i = 0; i < 10; i++)
{
Page currentPage = this.Pages[i];
System.Drawing.Image im = drawingimages[i];
im.Save(i + ".png");
Stream ms = new MemoryStream();
im.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
var decoder = BitmapDecoder.Create(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnDemand);
ImageSource imgsource = decoder.Frames[0];
bmaps[i] = imgsource as BitmapSource;
}
foreach (BitmapSource b in bmaps)
{
PageContent page = new PageContent();
FixedPage fixedPage = CreateOneFixedPage(b);
((IAddChild)page).AddChild(fixedPage);
doc.Pages.Add(page);
}
CreateOneFixedPage 的方法
private FixedPage CreateOneFixedPage(BitmapSource img)
{
FixedPage f = new FixedPage();
Image anImage = new Image();
anImage.BeginInit();
anImage.Source = img;
anImage.EndInit();
f.Children.Add(anImage);
return f;
}
当我尝试将System.Drawing.Image 保存到本地磁盘时,所有 10 个图像都正确保存。
我的代码有什么错误?
【问题讨论】:
-
如果在 BitmapDecoder.Create() 中将 BitmapCacheOption 更改为
BitmapCacheOption.OnLoad会发生什么? -
并且不需要在 Image 控件上调用 BeginInit() 和 EndInit()。
-
@Clemens: 都试过了,,, 但问题仍然存在
标签: c# wpf image fixeddocument