Export Word to ImageSource in WPF
Besides converting a document to a different file format (like PDF, HTML, and image), GemBox.Document also supports converting a document's pages to ImageSource
objects with the DocumentModel.ConvertToImageSource
method.
If you require more than displaying a file's content, for instance if you want to be able to modify it with some Graphical User Interface (GUI) control, take a look at this Word Editor in WPF example.
The following example shows how you can convert a page from a Word file to an image and attach it to WPF's Image
control.
<Window x:Class="ExportToImageSource.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Export to ImageSource / Image Control"
SizeToContent="WidthAndHeight">
<Border Margin="10" BorderBrush="Black" BorderThickness="1">
<Image x:Name="ImageControl"/>
</Border>
</Window>
using GemBox.Document;
using System.Windows;
namespace ExportToImageSource
{
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
// If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
this.SetImageControl("%#Reading.docx%", 0);
}
private void SetImageControl(string path, int pageIndex)
{
var document = DocumentModel.Load(path);
var imageOptions = new ImageSaveOptions();
imageOptions.PageNumber = pageIndex;
var imageSource = document.ConvertToImageSource(imageOptions);
this.ImageControl.Source = imageSource;
}
}
}
Imports GemBox.Document
Imports System.Windows
Namespace ExportToImageSource
Partial Public Class MainWindow
Inherits Window
Public Sub New()
Me.InitializeComponent()
' If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Me.SetImageControl("%#Reading.docx%", 0)
End Sub
Private Sub SetImageControl(path As String, pageIndex As Integer)
Dim document = DocumentModel.Load(path)
Dim imageOptions As New ImageSaveOptions()
imageOptions.PageNumber = pageIndex
Dim imageSource = document.ConvertToImageSource(imageOptions)
Me.ImageControl.Source = imageSource
End Sub
End Class
End Namespace