Want to keep file name of ImagePart RRS feed

  • Question

  • I use following code to add ImagePart to my document.

    using (WordprocessingDocument package = WordprocessingDocument.Create("MyDoc.docx", WordprocessingDocumentType.Document))


        MainDocumentPart docMain = package.AddMainDocumentPart();

        docMain.Document = new Document();

        ImagePart imgPart1 = docMain.AddImagePart(ImagePartType.Png);

        Image img = Image.FromFile("IO5A.PNG");

        using (Stream partStream = imgPart1.GetStream(FileMode.Create, FileAccess.ReadWrite))


            img.Save(partStream, System.Drawing.Imaging.ImageFormat.Png);




    The problem here is the image is stored in docx file with name is image1.png. By some reasons, I want to keep its original file name IO5A.png. Are there any ways to do that?

    Thanks for any helps.


    Tuesday, March 22, 2011 10:12 AM


  • Hi Ttvu,

    You can use the following code to get the name of images:

                    List<NonVisualDrawingProperties> lstNames =
                        new List<NonVisualDrawingProperties>(document.MainDocumentPart.Document.Descendants<NonVisualDrawingProperties>());
                    foreach (NonVisualDrawingProperties p in lstNames)

    The code snippet can retrive all the image names and store them in the list, you can apply to your program.

    I hope this 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 Bruce Song Monday, April 4, 2011 1:27 AM
    Friday, March 25, 2011 10:12 AM