none
Add image from word document to MSSQL database table RRS feed

  • Question

  • Hi all.

    I need to get a JPG image that is in a .docx word file to a MSSQL database. On my database, i have an entity called Question with a column called Diagram of type Binary.

    So far, i am able to get the image in the with the following snippet:

    foreach (Microsoft.Office.Interop.Word.InlineShape ils in paragraph.Range.InlineShapes)
                                    {
                                        // validate the object
                                        if (ils != null)
                                        {
                                            // validate this is a picture
                                            if (ils.Type == Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture)
                                            {
                                                // now we have a shape in that paragraph, we can do what we want to it
                                                MessageBox.Show("Haha! I found a picture!! "+ ils.Type.ToString());
                                               
                                                
                                            }
                                        }
                                    }

    From this step, i think that i should convert the InlineImages gotten to Binary form so that i am able to add it to the database. If this is the right direction, please assist on how i would go about this. If not, still please advice on the best way to add the word image to the MSSQL database.

    Thank you.

    Thursday, July 24, 2014 7:39 AM

Answers

  • Hi,

    According to your description, you want to convert the InlineImage object to Binary in Word document and then save the Binary into database.

    In fact, there is no method or property to convert the InlineImage to Binary directly in Word Object Model.

    For a workaround, after we access to the InlineImage object, we can select to copy it to clipboard. Then get data from the clipboard and convert it to be a Bitmap object. After that, you could save the Bitmap object to memory with memory stream or to a local folder. At last, you could read bytes from stream and save that into the database.

    Here is a sample to copy the InlineImage object to clipboard for your reference.

    for (int i = 1; i <= document.InlineShapes.Count; i++)
    {
        // select every inlineshapes(it takes images)
        document.InlineShapes[i].Select();
        // then we need copy selected object as a Picture
        document.ActiveWindow.Selection.CopyAsPicture();
        // call the clipboard object to keep in momery.
        System.Windows.Forms.IDataObject data = Clipboard.GetDataObject();
        // access data from the clipboard which is BitMap object
        if (data.GetDataPresent(typeof(System.Drawing.Bitmap)))
        {
            Bitmap bmp;
            bmp = (Bitmap)data.GetData(typeof(System.Drawing.Bitmap));
        }
    }

    About getting data from the clipboard, converting the data to Binary and saving to database, since that is not related to Office development, you could post in C# forum for more effective responses.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, July 25, 2014 6:51 AM
    Moderator

All replies

  • Hi,

    According to your description, you want to convert the InlineImage object to Binary in Word document and then save the Binary into database.

    In fact, there is no method or property to convert the InlineImage to Binary directly in Word Object Model.

    For a workaround, after we access to the InlineImage object, we can select to copy it to clipboard. Then get data from the clipboard and convert it to be a Bitmap object. After that, you could save the Bitmap object to memory with memory stream or to a local folder. At last, you could read bytes from stream and save that into the database.

    Here is a sample to copy the InlineImage object to clipboard for your reference.

    for (int i = 1; i <= document.InlineShapes.Count; i++)
    {
        // select every inlineshapes(it takes images)
        document.InlineShapes[i].Select();
        // then we need copy selected object as a Picture
        document.ActiveWindow.Selection.CopyAsPicture();
        // call the clipboard object to keep in momery.
        System.Windows.Forms.IDataObject data = Clipboard.GetDataObject();
        // access data from the clipboard which is BitMap object
        if (data.GetDataPresent(typeof(System.Drawing.Bitmap)))
        {
            Bitmap bmp;
            bmp = (Bitmap)data.GetData(typeof(System.Drawing.Bitmap));
        }
    }

    About getting data from the clipboard, converting the data to Binary and saving to database, since that is not related to Office development, you could post in C# forum for more effective responses.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, July 25, 2014 6:51 AM
    Moderator
  • Thanks. It worked after following your code.
    Sunday, August 3, 2014 9:02 AM