Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert Image object as picture in Word document

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;
        }
like image 292
Syspect Avatar asked May 14 '13 08:05

Syspect


2 Answers

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

like image 128
Milkncookiez Avatar answered Sep 28 '22 08:09

Milkncookiez


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!

like image 29
gumuruh Avatar answered Sep 28 '22 09:09

gumuruh