So I have this function where I am generating and returning my image (a .bmp format). I want to put it into a word document. I looked at InlineShapes.AddPicture
but it only takes a string
argument, which requires me to save the picture physically and then give the path of the picture as parameter to the AddPicture, which I don't want. I want to generate the pic and directly store it, whereas I need a method that takes Image
parameter.
P.S. the creation of Word document, tables, deciding which cell to put the pic into and all that stuff is done, I need only the insertion of the picture.
And this is the code for generating the picture, so you can see that I have it only as an object, but don't store it anywhere physically. This is in C#, but where I want to operate with the Word document, I am writing 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;
}
If you have set your Word document creation and opening, and according to the function that you've provided, I suppose the only thing left for you to do will be:
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 object of your class that implements the generateQRcodeImage()
function.
And you will also have to put this code somewhere where you want to arrange it in the word document (a table/cell/etc.)
This code help you to insert picture into the ms word 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 dont forget to import the libraries.
Imports Microsoft.Office.Interop
good luck!
I use the following variables:
Public oDoct As Microsoft.Office.Interop.Word.Document
Public oTable As Microsoft.Office.Interop.Word.Table
What I did was this:
1) I have the picture/image that I want in a PictureBox (pict1) on Form1
2) Since I want to put it in a table, I create the 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)
Please note that the table will be included in the header, and I've added a bookmark ("mHeader"), but this is not necessary. I did it like this because I want my image as a header.
3) I added a bookmark, within the table, for the picture
oTable.Cell(1, 1).Range.Bookmarks.Add("hPicture_c11")
4) The picture is then copied in the Clipboard
Clipboard.SetImage(Form1.pict1.Image)
5) Finally, the picture is pasted within the table
oTable.Cell(1, 1).Range.Bookmarks.Item("hPicture_c11").Range.Paste()
The "hPicture_c11" Bookmark is not mandatory. If you want to simply insert the picture use the following code:
oDoct.Range.Bookmarks.Item("\endofdoc").Range.Paste()
One last thing: check the dimensions of your image. Even though once it is inserted into the document it can be treated as any image, if it's too big you might have to resize it in Word