简体   繁体   English

将 System.Drawing.Image 添加到 FixedDocument

[英]Adding System.Drawing.Image to FixedDocument

I have 10 System.Drawing.Image .我有 10 个System.Drawing.Image I need to add them to a FixedDocument .我需要将它们添加到FixedDocument I tried the below code and the fixed document is procuded with all the 10 pages consists of only first image.我尝试了下面的代码,并且所有 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);
}

Method for CreateOneFixedPage 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;
}

When I try saving the System.Drawing.Image to the local disk, all the 10 images are saved correctly.当我尝试将System.Drawing.Image保存到本地磁盘时,所有 10 个图像都被正确保存。 What is the mistake in my code here?我这里的代码有什么错误?

Maybe not an answer to the question, but at least the code below shows a minimal working example.也许不是问题的答案,但至少下面的代码显示了一个最小的工作示例。 It loads all images from the Sample Pictures folder into a list of System.Drawing.Bitmap objects.它将 Sample Pictures 文件夹中的所有图像加载到System.Drawing.Bitmap对象列表中。 Then it converts all list elements to ImageSource and adds each to a FixedDocment page.然后它将所有列表元素转换为ImageSource并将每个元素添加到 FixedDocment 页面。

Please not that it does not call BeginInit() and EndInit() on the Image controls.请注意,它不会在 Image 控件上调用BeginInit()EndInit() Also it sets the PageContent's Child property, instead of calling IAddChild.AddChild() .它还设置 PageContent 的Child属性,而不是调用IAddChild.AddChild()

var bitmaps = new List<System.Drawing.Bitmap>();

foreach (var file in Directory.EnumerateFiles(
    @"C:\Users\Public\Pictures\Sample Pictures", "*.jpg"))
{
    bitmaps.Add(new System.Drawing.Bitmap(file));
}

foreach (var bitmap in bitmaps)
{
    ImageSource imageSource;

    using (var stream = new MemoryStream())
    {
        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        stream.Position = 0;
        imageSource = BitmapFrame.Create(stream,
            BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
    }

    var page = new FixedPage();
    page.Children.Add(new Image { Source = imageSource });

    doc.Pages.Add(new PageContent { Child = page });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM