Category / Section
How is custom resolution supported in Word document to image conversion in .NET MVC ?
2 mins read
No. Currently, DocIO doesn’t support converting Word documents into images with custom resolution. However, this can be achieved by changing the resolution of an image before saving it. The following code snippets show how to change the resolution of an image.
C#
// Loads an existing Word document
WordDocument wordDocument = new WordDocument("Template.docx", FormatType.Docx);
// Converts Word document to image
Image[] images = wordDocument.RenderAsImages(ImageType.Bitmap);
int imageIndex = 0;
int customResolution = 500;
int defaultImageResolution = 96;
foreach (Image image in images)
{
// Create a bitmap of specific width (500) and height (500)
Bitmap bitmap = new Bitmap((int)((image.Width * customResolution) / defaultImageResolution), (int)((image.Height * customResolution) / defaultImageResolution), PixelFormat.Format32bppPArgb);
// Set the custom resolution (500)
bitmap.SetResolution(customResolution, customResolution);
// Get graphics from the custom-sized bitmap image
Graphics graphics = Graphics.FromImage(bitmap);
// Recreate the image from stream using the specified width and height
graphics.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
// Save the bitmap
bitmap.Save("WordToImage_" + imageIndex + ".png", ImageFormat.Png);
imageIndex++;
}
// Closes the document
wordDocument.Close();
VB
' Loads an existing Word document
Dim wordDocument As New WordDocument("Template.docx", FormatType.Docx)
' Converts Word document to image
Dim images As Drawing.Image() = wordDocument.RenderAsImages(ImageType.Bitmap)
Dim imageIndex As Integer = 0
Dim customResolution As Integer = 500
Dim defaultImageResolution As Integer = 96
For Each image As Drawing.Image In images
' Create a bitmap of specific width (500) and height (500)
Dim bitmap As New Bitmap(CInt((image.Width * customResolution) / defaultImageResolution), CInt((image.Height * customResolution) / defaultImageResolution), PixelFormat.Format32bppPArgb)
' Set the custom resolution (500)
bitmap.SetResolution(customResolution, customResolution)
' Get graphics from the custom-sized bitmap image
Dim graphics As Graphics = Graphics.FromImage(bitmap)
' Recreate the image from stream using the specified width and height
graphics.DrawImage(image, New Rectangle(0, 0, bitmap.Width, bitmap.Height))
' Save the bitmap
bitmap.Save("WordToImage_" + imageIndex.ToString() + ".png", ImageFormat.Png)
imageIndex += 1
Next
' Closes the document
wordDocument.Close()