Novacode Docx 从位图创建图像

发布于 2024-12-26 01:08:46 字数 718 浏览 0 评论 0原文

背景

我的项目很紧急,需要迭代一个大型 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 inside constructor Image here

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

问题

只有 Novacode.Image 对象可以通过库插入到 MS Word 文档中,那么我到底如何在那里获取我的位图?

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

Background

My project is urgent and requires that I iterate a large XML file and return Base64 encoded images.

Each image must be inserted into an MS Word doc, and I am using the DocX library for that.

I am converting the Base64 strings to bitmap with no problem.

Problem

For the life of me, I can't seem to get the bitmaps into a Novacode.Image object which can then be inserted to the document. NOTE: I already know how to convert to System.Drawing.Image format. It is Novacode.Image format (DocX) that is giving me grief.

If I try to convert a la (Novacode.Image)somebitmap; I get Can not cast expression of type Image to Bitmap. If I try to initialize a new Novacode.Image object I get Can not access internal constructor Image here.

Using C#, .NET 4, Forms App, lots of coffee.

Question

Only Novacode.Image objects can be inserted into the MS Word doc via the library, so how the heck do I get my bitmap in there??

I am bleary-eyed at this point so perhaps I am just missing something simple.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

囍孤女 2025-01-02 01:08:46

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

希望这会有所帮助。

You have to use the DocX.AddImage() method to create a Novacode.Image object.

Follow these 5 steps to add a image to a word document:

  1. Save your picture into a memory stream.
  2. Create the Novacode.Image object by calling AddImage() method.
  3. Create a picture by calling CreatePicture() on the Novacode.Image object created in step 2.
  4. Set the shape of the picture (if needed).
  5. Insert your picture into a pragraph.

The sample below shows how to insert a image into a word document:

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();
  }
}

Hope, this helps.

东走西顾 2025-01-02 01:08:46

感谢汉斯和马丁,我能够以此为基础来确保大图像文件(照片)的大小始终适合页面。最大宽度和最大高度可以根据页面大小进行更改。

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.
}

Thanks Hans and Martin, I was able to use this as a basis for ensuring large image files (photos) are always sized to fit on the page. The max width and max height can be changed depending on your page size.

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.
}
夏夜暖风 2025-01-02 01:08:46

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

Thanks Hans. I had an Issue where the Image is inserted at the wrong size based on the DPI so I used this to scale the image based on DPI, 96 dpi seemed to be the basis of the scaled image in 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.
                }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文