Novacode Docx 创建图像:从位图创建

5

背景

我的项目非常紧急,需要迭代一个大型的XML文件并返回Base64编码的图像。

每个图像都必须插入到MS Word文档中,我正在使用DocX库进行操作。

我已经成功地将Base64字符串转换为位图。

问题

但是,我无论如何似乎都无法将位图转换为Novacode.Image对象,以便可以将其插入到文档中。注意:我已经知道如何转换为System.Drawing.Image格式,但是转换为Novacode.Image格式(在DocX中)却让我很困扰。

如果我尝试这样转换:(Novacode.Image)somebitmap;,我会得到 Can not cast expression of type Image to Bitmap 的错误。如果我尝试初始化新的Novacode.Image对象,我会得到 Can not access internal constructor Image here 的错误。

使用C#,.NET 4,Forms应用程序,大量的咖啡。

问题

只有使用该库时才能将Novacode.Image对象插入到MS Word文档中,那么我该如何将我的位图插入其中呢?

我已经到了神经衰弱的地步,所以可能只是错过了一些简单的东西。

3个回答

15

您需要使用DocX.AddImage()方法来创建一个Novacode.Image对象。

按照以下5个步骤将图片添加到Word文档中:

  1. 将图片保存到内存流中。
  2. 通过调用AddImage()方法创建Novacode.Image对象。
  3. 通过在步骤2中创建的Novacode.Image对象上调用CreatePicture()创建图片。
  4. 设置图片的形状(如果需要)。
  5. 将图片插入段落。

下面的示例显示了如何将图像插入到Word文档中:

using (DocX doc = DocX.Create(@"Example.docx"))
{
  using (MemoryStream ms = new MemoryStream())
  {
    System.Drawing.Image myImg = System.Drawing.Image.FromFile(@"test.jpg");

    myImg.Save(ms, myImg.RawFormat);  // Save your picture in a memory stream.
    ms.Seek(0, SeekOrigin.Begin);                    

    Novacode.Image img = doc.AddImage(ms); // Create image.

    Paragraph p = doc.InsertParagraph("Hello", false);

    Picture pic1 = img.CreatePicture();     // Create picture.
    pic1.SetPictureShape(BasicShapes.cube); // Set picture shape (if needed)

    p.InsertPicture(pic1, 0); // Insert picture into paragraph.

    doc.Save();
  }
}

希望这可以帮到你。


Hans,感谢你的帮助,但这仍然给我带来了麻烦。特别是,ms.Seek(0, SeekOrigin.Begin);引发了一个错误,指出流已关闭。此外,我在.createPicture方法上遇到了错误。我想我要么有一个坏的DLL,要么有一个外部代码中的错误。无论哪种方式,我通过使用常规的Word Interop方法解决了我的问题。我想花一些时间来确定错误是否在我的一边,以便我不会误导任何人选择您的答案作为正确答案。谢谢! - Matt Cashatt

3
感谢Hans和Martin,我能够将此作为基础,确保大图像文件(照片)总是调整大小以适合页面。最大宽度和最大高度可以根据您的页面大小进行更改。
using (MemoryStream ms = new MemoryStream())
{
    System.Drawing.Image myImg = System.Drawing.Image.FromFile(imageDirectory + i.FileName);

    double xScale = 1;
    double yScale = 1;

    double maxWidthInches = 6.1; // Max width to fit on a page
    double maxHeightInches = 8.66; // Max height to fit on a page

    // Normalise the Horizontal and Vertical scale for different resolutions
    double hScale = ((double)96 / myImg.HorizontalResolution);
    double vScale = ((double)96 / myImg.VerticalResolution);

    // Scaling required to fit in x direction
    double imageWidthInches = myImg.Width / myImg.HorizontalResolution; // in inches using DPI
    if (imageWidthInches > maxWidthInches)
        xScale = maxWidthInches / imageWidthInches * hScale;

    // Scaling required to fit in y direction
    double imageHeightInches = myImg.Height / myImg.VerticalResolution;
    if (imageHeightInches > maxHeightInches)
        yScale = maxHeightInches / imageHeightInches * vScale;

    double finalScale = Math.Min(xScale, yScale); // Use the smallest of the two scales to ensure the picture will fit in both directions

    myImg.Save(ms, myImg.RawFormat); // Save your picture in a memory stream.
    ms.Seek(0, SeekOrigin.Begin);

    Novacode.Image img = document.AddImage(ms); // Create image.
    Paragraph p = document.InsertParagraph();
    Picture pic = img.CreatePicture(); // Create picture.

    //Apply final scale to height & width
    double width = Math.Round((double)myImg.Width * finalScale);
    double height = Math.Round((double)myImg.Height * finalScale);

    pic.Width = (int)(width);
    pic.Height = (int)(height);

    p.InsertPicture(pic); // Insert picture into paragraph.
}

出于好奇,使用doc.PageWidth和doc.PageHeight不是更容易吗?代码如下: float scale = new[] { 1, picture.Width / doc.PageWidth, picture.Height / doc.PageHeight }.Max(); picture.Height = (int)(picture.Height / scale); picture.Width = (int)(picture.Width / scale); - ChrisFox

0
谢谢Hans。我遇到了一个问题,图像的大小基于DPI插入错误,所以我使用这个来根据DPI缩放图像,96 dpi似乎是Word中缩放图像的基础:
using (MemoryStream ms = new MemoryStream())
                {
                    System.Drawing.Image myImg = System.Drawing.Image.FromFile(path);

                    //Calculate Horizontal and Vertical scale
                    float Hscale = ((float)96 / myImg.HorizontalResolution); 
                    float Vscale = ((float)96 / myImg.VerticalResolution );

                    myImg.Save(ms, myImg.RawFormat);  // Save your picture in a memory stream.
                    ms.Seek(0, SeekOrigin.Begin);

                    Novacode.Image img = proposal.AddImage(ms); // Create image.
                    Picture pic1 = img.CreatePicture();     // Create picture.

                    //Apply scale to height & width
                    pic1.Height = (int)(myImg.Height * Hscale);
                    pic1.Width = (int)(myImg.Width * Vscale);

                    a.InsertPicture(pic1, 0); // Insert picture into paragraph.
                }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接