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

Problem with document.AddImage()

Sep 4, 2014 at 3:45 PM
Edited Sep 4, 2014 at 3:50 PM
When I try to add an image to my document I receive this error:

"Sequence contains no elements".

That's why my image is already in the document, but, how can I know it?

My code:
public static  void ReplaceTextWithImage( DocX document, string textToReplace, byte[] replacement )
{
   using( var image = new MemoryStream( replacement ) )
   {
                var img = document.AddImage( image );
                var picture = img.CreatePicture( );
       ...
   }
}
Sep 9, 2014 at 4:40 PM
Edited Sep 9, 2014 at 4:41 PM
Hi !

I'm not sure of what you want, but it seems to me that your problem is that you don't actually save your image in your stream. Thus you have an empty MemoryStream.

If you want, here's some code I use to insert an existing image into a table :
using (MemoryStream ms = new MemoryStream())
                {
                    float Hscale = ((float)96 / SynoptiqueHT.HorizontalResolution);//Calculate Horizontal and Vertical scale
                    float Vscale = ((float)96 / SynoptiqueHT.VerticalResolution);

                    myImage.Save(ms, tmyImage.RawFormat);  // Save your picture in a memory stream.
                    ms.Seek(0, SeekOrigin.Begin);

                    Novacode.Image img = doc.AddImage(ms); // Create image.

                    Picture pic1 = img.CreatePicture((int)(myImg.Height * Hscale), (int)(myImg.Width * Vscale));// Create picture.

                    myTable.Paragraphs.First().InsertPicture(pic1, 0); // Insert picture into table.
                }