This project has moved and is read-only. For the latest updates, please go here.

Problem CreatePicture() with image from Code Behind

May 22, 2014 at 8:45 AM
Hi ,
Currently , I'm working on a project that use this awesome library. I'm facing a problem that I can't deal with.
I would like to add an image in a DocX using code ( as BitmapSource)

I made this exemple :
public MainWindow()
            using (DocX doc = DocX.Load(@"input.docx")) // input.docx is an empty docx created with Word 2010
                Novacode.Paragraph paragraphFirst = doc.Paragraphs.First();
                Novacode.Image image;
                BitmapSource bitmapSource = new BitmapImage(new Uri(@"image.bmp"));
                MemoryStream memoryStream = GetStreamFromBitmapSource(bitmapSource);

                image = doc.AddImage(memoryStream);
                Picture picture = image.CreatePicture();



        public static MemoryStream GetStreamFromBitmapSource(BitmapSource bitmapSource)
            BitmapEncoder encoder = new PngBitmapEncoder();
            MemoryStream ms = new MemoryStream();
            return ms;
The error is located in the method : CreatePicture()

What is wrong in this code ?
Thanks for any help that you can provide.
May 23, 2014 at 9:47 AM
I have solved my problem !

I needed to put the position of the stream at its beginning before passing it in parameter to the function AddImage().

To do this :
memoryStream.Seek(0, SeekOrigin.Begin);
I hope it could help !
Marked as answer by Soyer2511 on 5/23/2014 at 1:47 AM
May 23, 2014 at 11:45 AM
Edited May 23, 2014 at 11:46 AM
Seems you already found the answer, which is great of course! :)
I tried some things myself and I was able to insert the picture when I changed BitmapSource to BitmapImage:
BitmapSource bitmapSource = new BitmapImage(new Uri(@"image.bmp"));
BitmapImage bitmapSource = new BitmapImage(new Uri(@"image.bmp"));
Though I just used some simple code, might be this won't work in your project.