我有10 System.Drawing.Image
。我需要将它们添加到FixedDocument
。我尝试了下面的代码,并且固定的文档被处理,所有10个页面只包含第一个图像。对于CreateOneFixedPage将System.Drawing.Image添加到FixedDocument
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
到本地磁盘
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);
}
方法,所有的10张图像的正确保存。 我的代码在这里有什么错误?
如果在BitmapDecoder.Create()中将BitmapCacheOption更改为BitmapCacheOption.OnLoad,会发生什么? – Clemens
而不需要在Image控件上调用BeginInit()和EndInit()。 – Clemens
@Clemens:尝试了两种,,,,但仍然存在问题持续 –