How to save a document in Image format using C# and .NET



Let's save each page of the document into image format, like a shown on the picture:

Save as Image

Code explanations

Here we create DocumentPaginator:

 DocumentPaginator dp =  dc.GetPaginator();
Pagination is the process of dividing a document into discrete pages.

Complete code

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using SautinSoft.Document;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            SaveToImage();
        }
        /// <summary>
        /// Loads a document and saves all pages as images.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/save-document-as-image-net-csharp-vb.php
        /// </remarks>
        static void SaveToImage()
        {
            string filePath = @"..\..\..\example.docx";
            DocumentCore dc = DocumentCore.Load(filePath);
            string folderPath = Path.GetFullPath(@"Result-files");

            DocumentPaginator dp =  dc.GetPaginator();

            for (int i = 0; i < dp.Pages.Count; i++)
            {
                DocumentPage page = dp.Pages[i];
                // For example, set DPI: 72, Background: White.
                Bitmap image = page.Rasterize(72, SautinSoft.Document.Color.White);
                Directory.CreateDirectory(folderPath);
                image.Save(folderPath+@"\Page - "+i.ToString()+".png", ImageFormat.Png);
            }
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(folderPath) { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports System.IO
Imports SautinSoft.Document
Imports System.Drawing
Imports System.Drawing.Imaging

Module Sample
    Sub Main()
        SaveToImage()
    End Sub

    ''' <summary>
    ''' Loads a document and saves all pages as images.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/save-document-as-image-net-csharp-vb.php
    ''' </remarks>
    Sub SaveToImage()
        Dim filePath As String = "..\..\..\example.docx"
        Dim dc As DocumentCore = DocumentCore.Load(filePath)
        Dim folderPath As String = Path.GetFullPath("Result-files")

        Dim dp As DocumentPaginator = dc.GetPaginator()

        For i As Integer = 0 To dp.Pages.Count - 1
            Dim page As DocumentPage = dp.Pages(i)
            ' For example, set DPI: 72, Background: White.
            Dim image As Bitmap = page.Rasterize(72, SautinSoft.Document.Color.White)
            Directory.CreateDirectory(folderPath)
            image.Save(folderPath & "\Page - " & i.ToString() & ".png", ImageFormat.Png)
        Next i
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(folderPath) With {.UseShellExecute = True})
    End Sub
End Module

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.