locked
How to insert Image object as picture in Word document RRS feed

  • Question

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

    Edit:

    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;
            }

    Tuesday, May 14, 2013 8:43 AM

Answers

  • Hi,

     I don`t know if this will help or not but, here i have created a bitmap from a picturebox and then copied the bitmap to the clipboard. Then added a paragraph to the document, selected the paragraph and pasted the picture. I am not great at working with word documents so maybe someone else here can correct this if they see a better way of doing it.  :)

    Imports Word = Microsoft.Office.Interop.Word
    
    Public Class Form1
    
        'Add Reference to (Microsoft Word Object Library) from the Add Reference (Com) tab.
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim oWord As Word.Application = CType(CreateObject("Word.Application"), Word.Application)
            Dim oDoc As Word.Document = oWord.Documents.Add()
            Dim oPara1 As Word.Paragraph
            oWord.Visible = True
    
            oPara1 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
            oPara1.Range.Text = "This is the first paragraph before the picture if wanted."
            oPara1.Format.SpaceAfter = 6
            oPara1.Range.InsertParagraphAfter()
    
            '-----------------------------------------------------------------
            Dim pic As New Bitmap(PictureBox1.Image) 'The bitmap you created
            Clipboard.SetImage(pic) 'Copy your bitmap to the clipboard
    
            'Add a paragraph
            oPara1.Range.InsertParagraphAfter()
    
            'select paragrapgh and paste your picture from clipboard
            oPara1.Range.Select()
            oWord.Selection.Paste()
            '-----------------------------------------------------------------
    
            oDoc.SaveAs("C:\Test\MyNewDucument.doc", True)
    
            oDoc.Close()
            oDoc = Nothing
            oWord.Application.Quit()
            oWord = Nothing
    
        End Sub
    End Class

    • Marked as answer by Youen Zen Tuesday, May 28, 2013 6:15 AM
    Tuesday, May 14, 2013 11:33 AM

All replies

  • HI .

    I HOPE THIS LINK WILL HELP YOU

    http://msdn.microsoft.com/en-us/library/office/bb497430.aspx

    THANKS


    aan

    Tuesday, May 14, 2013 9:09 AM
  • Hi,

     I don`t know if this will help or not but, here i have created a bitmap from a picturebox and then copied the bitmap to the clipboard. Then added a paragraph to the document, selected the paragraph and pasted the picture. I am not great at working with word documents so maybe someone else here can correct this if they see a better way of doing it.  :)

    Imports Word = Microsoft.Office.Interop.Word
    
    Public Class Form1
    
        'Add Reference to (Microsoft Word Object Library) from the Add Reference (Com) tab.
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim oWord As Word.Application = CType(CreateObject("Word.Application"), Word.Application)
            Dim oDoc As Word.Document = oWord.Documents.Add()
            Dim oPara1 As Word.Paragraph
            oWord.Visible = True
    
            oPara1 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
            oPara1.Range.Text = "This is the first paragraph before the picture if wanted."
            oPara1.Format.SpaceAfter = 6
            oPara1.Range.InsertParagraphAfter()
    
            '-----------------------------------------------------------------
            Dim pic As New Bitmap(PictureBox1.Image) 'The bitmap you created
            Clipboard.SetImage(pic) 'Copy your bitmap to the clipboard
    
            'Add a paragraph
            oPara1.Range.InsertParagraphAfter()
    
            'select paragrapgh and paste your picture from clipboard
            oPara1.Range.Select()
            oWord.Selection.Paste()
            '-----------------------------------------------------------------
    
            oDoc.SaveAs("C:\Test\MyNewDucument.doc", True)
    
            oDoc.Close()
            oDoc = Nothing
            oWord.Application.Quit()
            oWord = Nothing
    
        End Sub
    End Class

    • Marked as answer by Youen Zen Tuesday, May 28, 2013 6:15 AM
    Tuesday, May 14, 2013 11:33 AM