How to Retrieve/Read Image binary data from ImagePart RRS feed

  • Question

  • Hi,

    I have a situation where that I need to extract added Images from Word 2010 files. Basically what I have tried was,

    1. Find 'Blip' object from word document using SDK (This step is must acording to the programm).
    2. Get the Id of Blip.
    3. Then, MainDocumentPart.GetPartById(id) to get the relevant ImagePart.

    I can get the ImagePart but after that I need to get the (Binary??) stream of the Image from that part where now I am stuck. Can anyone give me some light on this. This should be done using SDK only.

    Note: Same thing I need to done for embedded objects too :)

    Thanks in advance,


    Friday, December 17, 2010 5:00 PM


All replies

  • Hi Vins007,

    Thank you for posting and we are glad to help with you.

    After reading your post, I knew your problem that you want to convert image to the binary stream. If I have misunderstood you, please let me know.

    As far as I know, you just need to call the method GetStream() and use BinaryWriter Class to write binary data.

    Here are the articles for you to references: 

    Replace image in word doc using OpenXML


    BinaryWriter Class


    I hope these can help you and feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Vins007 Thursday, December 23, 2010 6:07 AM
    Monday, December 20, 2010 6:08 AM
  • Hi Bruce,

    Thank you for your answer. Its correct. Here I'll explain what was root of my question if its benefit for anyone.

    I was trying to extract images from word file's ImagePart and I wrote a code stuff like below. NOTE : obj is a custom object which accept any 'object' to its 'Content' property.


    string objVal = blip.Embed.Value;
    obj.Content = wordprocessingDocument.MainDocumentPart.GetPartById(objVal).GetStream();


    Then when I want to write unit tests, I create a stream of same image as below as to treat as Expected object.

    // Create expected Image stream
    string ImagePart1Data = "/9j/4AAQSkZJRgA....[shorted]....PNpmc5nPN/9k=";
    Stream data = new MemoryStream(Convert.FromBase64String(ImagePart1Data));
    ExpectedObj.Content = data;


    Then the issue was, in debug mode,

    obj.Content shows it type as

    base {System.IO.Stream} = {MS.Internal.IO.Zip.ZipIOModeEnforcingStream}

    and ExpectedObj.Content shows it type as,

    base {System.IO.Stream} = {System.IO.Stream}

    So my test case fails when compare types of Actual obj vs Expected obj. Then I guess there will be wrong output i am getting from  "GetStream()".

    Finally I got confirm that the stream I got as result using "GetStream()" is correctly represent the actual image in word file by visually viewing the image which result from below code.

    Stream stream = (Stream)resultDocxObject.Content;
    Image img = Image.FromStream(stream);

    But yet I couldn't complete a test case to compare those streams are equal or not. I tried Stream length as below

    ((Stream)expected.Content).Length != ((Stream)actual.Content).Length

    But it always true, may be "Convert.FromBase64String()" conversion, and only hope is doing a byte-by-byte comparison or checksum.



    - Vins
    • Edited by Vins007 Thursday, December 23, 2010 6:48 AM line spaces
    Thursday, December 23, 2010 6:47 AM
  • Hi,
    There is a sample called CS/VBManipulateImagesInWordDocument that could

    1.List all images in a document.

    2.Extract an image from a document

    3.Delete an image in the document

    4.Replace an image.

    You can download it from (http://1code.codeplex.com/ or use our sample browser tool http://1code.codeplex.com/releases/view/64539) and have a try.
    If there is any feedback, feel free to tell me.

    Ruiz Yi [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, August 11, 2011 3:36 AM
  • 9 years ago this question was asked. Today I want to reply. Lol

    ImagePart imagePart = (ImagePart)document.MainDocumentPart.GetPartById(your_id);
                    Stream stream = imagePart.GetStream();
                    Image image = Image.FromStream(stream);

    • Proposed as answer by nfgallimore Wednesday, October 9, 2019 2:53 PM
    Thursday, September 5, 2019 6:00 PM
  • And it helped me today. You have to use the System.Drawing package for the Image class.
    • Edited by nfgallimore Wednesday, October 9, 2019 2:56 PM
    Wednesday, October 9, 2019 2:52 PM
  • and 1 year later it does the same to me. That's the hint i needed, thx :-)
    Saturday, December 5, 2020 7:43 PM