新星$ C C DOCX $创建图像从位图新星、位图、图像、DOCX

2023-09-03 05:20:21 作者:心中有苦不能说ち

我的项目是紧迫的,需要我遍历一个大的XML文件,并返回Base64的EN codeD映像。

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

每个图像必须被插入到一个MS Word文档,我现在用的是 DOCX 库为

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

我在转换的Base64字符串为位图,没有问题。

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

有关我的生活,我似乎无法得到位图成的新星code.Image 的对象,然后它可以插入到文档中。注:我已经知道如何转换为System.Drawing.Image对象格式。这是新星code.Image格式(DOCX),它给我的悲伤。

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.

如果我尝试转换一拉(新星code.Image)somebitmap; 我得到无法施展EX pression类型图像为位图。如果我尝试初始化一个新的新星code.Image 的对象,我得到不能访问内部构造的形象在这里

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.

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

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

只有新星code.Image对象可以被插入到通过库中的MS Word文档,因此到底如何让我的位图在那里?

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.

推荐答案

您必须使用 DocX.AddImage()方法来创建一个新星code.Image 对象。

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

按照以下5个步骤为图像添加到一个Word文档:

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

在保存图片到一个内存流。 致电 AddImage()方法创建新星code.Image 对象。 通过调用创建图片 CreatePicture()步骤创建的新星code.Image 对象2。 设置画面的形状(如果需要的话)。 将你的照片变成pragraph。 Save your picture into a memory stream. Create the Novacode.Image object by calling AddImage() method. Create a picture by calling CreatePicture() on the Novacode.Image object created in step 2. Set the shape of the picture (if needed). Insert your picture into a pragraph.

下面的示例演示如何将一个图像插入到Word文档:

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.