Working with Word for WPF > Basic Level Working > Adding Images |
You might need to insert images in your word document along with the text to enhance the overall appearance of your document. To add an image in your document, use the following code that loads an image and sketches it in the document:
Note that two classes named WordUtils and DataAccess are used in the code given below. These classes are available in the product sample located at the following location on your system:
Documents\ComponentOne Samples\WPF\WordCreator
You can use these classes in your application from the mentioned location.
' calculate page rect (discounting margins) Dim rcPage As Rect = WordUtils.PageRectangle(word) ' load image into writeable bitmap Dim bi As New BitmapImage() bi.BeginInit() bi.StreamSource = DataAccess.GetStream("borabora.jpg") bi.EndInit() Dim wb = New WriteableBitmap(bi) ' center image on page preserving aspect ratio word.DrawImage(wb, rcPage)
// calculate page rect (discounting margins) Rect rcPage = WordUtils.PageRectangle(word); // load image into writeable bitmap BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = DataAccess.GetStream("borabora.jpg"); bi.EndInit(); var wb = new WriteableBitmap(bi); // center image on page preserving aspect ratio word.DrawImage(wb, rcPage);
The image is loaded into writeable bitmap and DrawImage method is used to draw the image in the above code.
The output of the above code will look similar to the image given below: