How to insert an Image object as an image in a Word document

So, I have this function in which I generate and return an image (.bmp format). I want to write it to a text document. I looked at InlineShapes.AddPicture , but it only takes a string argument, which requires me to physically save the image, and then pass the image path as the AddPicture parameter that I don't want. I want to create a pic and save it right away, whereas I need a method that takes an Image parameter.

PS creating a Word document, tables, making a decision about which cell to insert pic and all this happens, I only need to insert the image.

And this is the code for creating the image, so you can see that I have it only as an object, but do not store it anywhere physically. This is in C #, but where I want to work with a Word document, I write in VB.NET.

 Bitmap picture = new Bitmap(100, 100); // generates a QRcode image and returns it public Image generateQRcodeImage(string textValue) { QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.M); QrCode qrCode; encoder.TryEncode(textValue, out qrCode); using (Graphics graph = Graphics.FromImage(picture)) { new GraphicsRenderer(new FixedCodeSize(100, QuietZoneModules.Two)).Draw(graph, qrCode.Matrix); } return picture; } 
+4
source share
3 answers

If you installed and opened a Word document, and in accordance with the function you provided, I suppose you only have to:

  Dim rng As Word.Range = oDoc.Range(int1, int2) Dim img As Image = qrGen.generateQRcodeImage("desiredInfoToEncloseInQRcode") Clipboard.SetImage(img) rng.Paste() 

where qrGen is, of course, an object of your class that implements the generateQRcodeImage() function. And you also have to put this code somewhere where you want to place it in the word document (table / cell / etc.).

+3
source

This code will help you insert the image into the word ms via vb.net:

 Dim word_app As Word._Application = New _ Word.ApplicationClass() ' Create the Word document. Dim word_doc As Word._Document = _ word_app.Documents.Add() Dim para As Word.Paragraph = word_doc.Paragraphs.Add() para.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter para.Range.InlineShapes.AddPicture(YOURPATHPICTURE) para.Range.InsertParagraphAfter() 

and don't forget to import the libraries.

 Imports Microsoft.Office.Interop 

Good luck

+1
source

I use the following variables:

 Public oDoct As Microsoft.Office.Interop.Word.Document Public oTable As Microsoft.Office.Interop.Word.Table 

I have done this:

1) I have the image / image that I want in PictureBox (pict1) in Form1

2) Since I want to put it in a table, I create a table

 oDoct.Sections(1).Headers(1).Range.Bookmarks.Add("mHeader", ) oTable = oDoct.Tables.Add(oDoct.Sections(1).Headers(1).Range.Bookmarks.Item("mheader").Range, 2, 3) 

Note that the table will be included in the header, and I added a bookmark ("mHeader"), but this is not necessary. I did it this way because I want my image to be a caption.

3) I added a bookmark to the table for the image

 oTable.Cell(1, 1).Range.Bookmarks.Add("hPicture_c11") 

4) Then the image is copied to the clipboard

 Clipboard.SetImage(Form1.pict1.Image) 

5) Finally, the image is inserted into the table

 oTable.Cell(1, 1).Range.Bookmarks.Item("hPicture_c11").Range.Paste() 

The hPicture_c11 tab is optional. If you just want to insert an image, use the following code:

 oDoct.Range.Bookmarks.Item("\endofdoc").Range.Paste() 

Last: check the size of your image. Despite the fact that once it is inserted into a document, it can be considered like any image, if it is too large, you may need to resize it in Word

0
source

Source: https://habr.com/ru/post/1480657/


All Articles