Novacode Docx从位图创建图像

背景

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

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

我正在将Base64字符串转换为位图,没有任何问题。

问题

对于我的生活,我似乎无法将位图获取到Novacode.Image对象,然后可以将其插入到文档中。 注意:我已经知道如何转换为System.Drawing.Image格式。 它是Novacode.Image格式(DocX)让我感到悲伤。

如果我尝试转换la (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 App,很多咖啡。

只有Novacode.Image对象可以通过库插入到MS Word doc中,那么我怎么能在那里得到我的位图?

在这一点上我很沮丧,所以也许我只是缺少一些简单的东西。

您必须使用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和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. } 

谢谢汉斯。 我有一个问题,根据DPI以错误的大小插入图像,所以我用它来根据DPI缩放图像,96 dpi似乎是单词中缩放图像的基础:

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