我的项目很紧急,需要我迭代一个大的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文档中,那么我怎么能在那里得到我的位图?
在这一点上,我很沮丧,所以也许我只是错过了一些简单的事情。
答案 0 :(得分:15)
您必须使用DocX.AddImage()
方法创建Novacode.Image
对象。
按照以下5个步骤将图像添加到Word文档:
Novacode.Image
方法创建AddImage()
对象。CreatePicture()
对象上的Novacode.Image
来创建图片。下面的示例显示了如何将图像插入到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();
}
}
希望,这有帮助。
答案 1 :(得分: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.
}
答案 2 :(得分:0)
谢谢汉斯。我有一个问题,根据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.
}