ComponentOne Word for UWP
Working with Word for UWP / Basic Level Working / Adding Images
In This Topic
    Adding Images
    In This Topic

    You might need to insert images in your word document along with the text to enhance the overall appearance of your document.

    Note that a class named WordUtils is used in the code given below. It is available in the product sample located at the following location on your system:
    Documents\ComponentOne Samples\UWP\WordSamples
    You can use these classes in your application from the mentioned location.

    To add an image in your document, use the following code that loads an image and sketches it in the document:

    ' calculate page rect (discounting margins)
    Dim rcPage As Rect = WordUtils.PageRectangle(word)
    Dim ras As New InMemoryRandomAccessStream()
    ' load image into writeable bitmap
    Dim wb As New WriteableBitmap(880, 660)
    Dim file = Await StorageFile.GetFileFromApplicationUriAsync(New Uri("ms-appx:///WordSamplesLib/Assets/pic.jpg"))
    
    wb.SetSource(Await file.OpenReadAsync())
    Dim rcPic As New Rect(New Point(0, 0), New Point(word.PageSize.Width, word.PageSize.Height))
    
    ' draw on page preserving aspect ratio
    word.DrawImage(wb, rcPic)
    WordUtils.Save(word)
    
    // calculate page rect (discounting margins)
    Rect rcPage = WordUtils.PageRectangle(word);
    InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
    // load image into writeable bitmap
    WriteableBitmap wb = new WriteableBitmap(880, 660);
    var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///WordSamplesLib/Assets/pic.jpg"));
                
    wb.SetSource(await file.OpenReadAsync());
    Rect rcPic = new Rect(new Point(0, 0), new Point(word.PageSize.Width, word.PageSize.Height));
    
    // draw on page preserving aspect ratio
    word.DrawImage(wb, rcPic);
    WordUtils.Save(word);
    

    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: