Compared with Word document format, pictures are more convenient to share and preview across platforms, because they do not require MS Word to be installed on machines. Moreover, converting Word to images can preserve the original appearance of the document, which is useful when further modifications are not desired. In this article, you will learn how to convert Word documents to images in C# and VB.NET using Spire.Doc for .NET.
- Convert Word to JPG in C#, VB.NET
- Convert Word to SVG in C#, VB.NET
- Convert Word to PNG with Customized Resolution in C#, VB.NET
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Convert Word to JPG in C#, VB.NET
Spire.Doc for .NET offers the Document.SaveToImages() method to convert a whole Word document into individual Bitmap or Metafile images. Then, a Bitmap or Metafile image can be saved as a BMP, EMF, JPEG, PNG, GIF, or WMF format file. The following are the steps to convert a Word document to JPG images using this library.
- Create a Document object.
- Load a Word document using Document.LoadFromFile() method.
- Convert the document to Bitmap images using Document.SaveToImages() method.
- Loop through the image collection to get the specific one and save it as a JPG file.
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace ConvertWordToJPG
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document doc = new Document();
//Load a Word document
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Template.docx");
//Convert the whole document into individual images
Image[] images = doc.SaveToImages(ImageType.Bitmap);
//Loop through the image collection
for (int i = 0; i < images.Length; i++)
{
//Save the image to a JPEG format file
string outputfile = String.Format("Image-{0}.jpg", i);
images[i].Save("C:\\Users\\Administrator\\Desktop\\Images\\" + outputfile, ImageFormat.Jpeg);
}
}
}
}
Convert Word to SVG in C#, VB.NET
Using Spire.Doc for .NET, you can save a Word document as a queue of byte arrays. Each byte array can then be written as a SVG file. The detailed steps to convert Word to SVG are as follows.
- Create a Document object.
- Load a Word file using Document.LoadFromFile() method.
- Save the document as a queue of byte arrays using Document.SaveToSVG() method.
- Loop through the items in the queue to get a specific byte array.
- Write the byte array to a SVG file.
- C#
- VB.NET
using Spire.Doc;
using System;
using System.Collections.Generic;
using System.IO;
namespace CovnertWordToSVG
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document doc = new Document();
//Load a Word document
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Template.docx");
//Save the document as a queue of byte arrays
Queue<byte[]> svgBytes = doc.SaveToSVG();
//Loop through the items in the queue
for (int i = 0; i < svgBytes.Count; i++)
{
//Convert the queue to an array
byte[][] bytes = svgBytes.ToArray();
//Specify the output file name
string outputfile = String.Format("Image-{0}.svg", i);
//Write the byte[] in a SVG format file
FileStream fs = new FileStream("C:\\Users\\Administrator\\Desktop\\Images\\" + outputfile, FileMode.Create);
fs.Write(bytes[i], 0, bytes[i].Length);
fs.Close();
}
}
}
}
Convert Word to PNG with Customized Resolution in C#, VB.NET
An image with higher resolution is generally more clear. You can customize the image resolution while converting Word to PNG by following the following steps.
- Create a Document object.
- Load a Word file using Document.LoadFromFile() method.
- Convert the document to Bitmap images using Document.SaveToImages() method.
- Loop through the image collection to get the specific one.
- Call the custom method ResetResolution() to reset the image resolution.
- Save the image as a PNG file.
- C#
- VB.NET
using Spire.Doc;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using Spire.Doc.Documents;
namespace ConvertWordToPng
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document doc = new Document();
//Load a Word document
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Template.docx");
//Convert the whole document into individual images
Image[] images = doc.SaveToImages(ImageType.Metafile);
//Loop through the image collection
for (int i = 0; i < images.Length; i++)
{
//Reset the resolution of a specific image
Image newimage = ResetResolution(images[i] as Metafile, 150);
//Save the image to a PNG format file
string outputfile = String.Format("Image-{0}.png", i);
newimage.Save("C:\\Users\\Administrator\\Desktop\\Images\\" + outputfile, ImageFormat.Png);
}
}
//Set the image resolution by the ResetResolution() method
public static Image ResetResolution(Metafile mf, float resolution)
{
int width = (int)(mf.Width * resolution / mf.HorizontalResolution);
int height = (int)(mf.Height * resolution / mf.VerticalResolution);
Bitmap bmp = new Bitmap(width, height);
bmp.SetResolution(resolution, resolution);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(mf, Point.Empty);
}
return bmp;
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
