Background
My project is urgent and requires that I iterate a large XML file and return Base64 encoded images.
Each image must be inserted into an MS Word doc, and I am using the DocX library for that.
I am converting the Base64 strings to bitmap with no problem.
Problem
For the life of me, I can't seem to get the bitmaps into a Novacode.Image object which can then be inserted to the document. NOTE: I already know how to convert to System.Drawing.Image format. It is Novacode.Image format (DocX) that is giving me grief.
If I try to convert a la (Novacode.Image)somebitmap;
I get Can not cast expression of type Image to Bitmap
. If I try to initialize a new Novacode.Image
object I get Can not access internal constructor Image here
.
Using C#, .NET 4, Forms App, lots of coffee.
Question
Only Novacode.Image objects can be inserted into the MS Word doc via the library, so how the heck do I get my bitmap in there??
I am bleary-eyed at this point so perhaps I am just missing something simple.
You have to use the
DocX.AddImage()
method to create aNovacode.Image
object.Follow these 5 steps to add a image to a word document:
Novacode.Image
object by callingAddImage()
method.CreatePicture()
on theNovacode.Image
object created in step 2.The sample below shows how to insert a image into a word document:
Hope, this helps.
Thanks Hans and Martin, I was able to use this as a basis for ensuring large image files (photos) are always sized to fit on the page. The max width and max height can be changed depending on your page size.
Thanks Hans. I had an Issue where the Image is inserted at the wrong size based on the DPI so I used this to scale the image based on DPI, 96 dpi seemed to be the basis of the scaled image in word: