Category / Section
Is custom resolution supported in Word document to image conversion?
2 mins read
No. Currently DocIO doesn’t have support to convert the Word document into image with custom resolution. However, we can achieve this by changing the resolution of an image before saving it. The following code snippets shows 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 customReslution = 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 * customReslution) / defaultImageResolution), (int)((image.Height * customReslution) / defaultImageResolution), PixelFormat.Format32bppPArgb); //Set the custom resolution (500) bitmap.SetResolution(customReslution, customReslution); //Get graphics from the custom size bitmap image Graphics graphics = Graphics.FromImage(bitmap); //Recreate the image from stream using 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 size bitmap image Dim graphics As Graphics = Graphics.FromImage(bitmap) 'Recreate the image from stream using 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()