我有 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 张图像均已正确保存。我的代码中有什么错误?