【问题标题】:Novacode Docx Create Image from BitmapNovacode Docx 从位图创建图像
【发布时间】:2017-11-01 04:44:13
【问题描述】:

背景

我的项目很紧急,需要我迭代一个大型 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 文档中,那么我到底要如何在其中获取我的位图??

在这一点上我睡眼惺忪,所以也许我只是错过了一些简单的事情。

【问题讨论】:

    标签: c# .net ms-office docx


    【解决方案1】:

    您必须使用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();
      }
    }
    

    希望,这会有所帮助。

    【讨论】:

    • 汉斯,感谢您的帮助,但这仍然给我带来了麻烦。特别是 ms.Seek(0, SeekOrigin.Begin);,抛出了一个错误,指出流已关闭。另外,我在 .createPicture 方法上遇到了错误。我在想我要么有一个坏的 DLL,要么是外部代码中的错误。无论哪种方式,我只使用常规的 Word 互操作方法就解决了我的问题。我想在选择您的答案正确之前花一些时间,以确保错误在我这边,这样我就不会误导任何人。谢谢!
    【解决方案2】:

    感谢 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);
    【解决方案3】:

    谢谢汉斯。我遇到了一个问题,即根据 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.
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-14
      • 1970-01-01
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多