How to convert RTF to Image and reset image resolution

Spire.Doc has a powerful ability to operate RTF file formats in C# and VB.NET. By using Spire.Doc, developers can convert RTF to PDF, HTML and word documents in .doc, .docx. This article will show you how to convert RTF into image and then reset the image resolution.

Download and install Spire.Doc for .NET and then add Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll". Here comes to the details of how to convert RTF into PNG and reset image resolution in C#.

Step 1: Create a new document and load from file.

Document doc = new Document();
doc.LoadFromFile("sample.rtf", FileFormat.Rtf);

Step 2: Save the RTF to image

Image[] images = doc.SaveToImages(Spire.Doc.Documents.ImageType.Metafile);

Step 3: Traverse the elements in the list of images and save them into .Png format.

for (int i = 0; i < images.Length; i++)
{
    Metafile mf = images[i] as Metafile;
    Image newimage = ResetResolution(mf, 200);
    string outputfile = String.Format("image-{0}.png", i);
    newimage.Save(outputfile, System.Drawing.Imaging.ImageFormat.Png);
}

Step 4: Set the image resolution call the method: ResetResolution.

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;
}

Effective screenshot of the image before reset the image resolution:

How to convert RTF to Image and reset image resolution

The image after reset the image resolution:

How to convert RTF to Image and reset image resolution

Full codes:

using Spire.Doc;
using System.Drawing;
using System.Drawing.Imaging;

namespace RTFtoImage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a new document and load from file.
            Document doc = new Document();
            doc.LoadFromFile("sample.rtf", FileFormat.Rtf);
            // save the RTF to image 
            Image[] images = doc.SaveToImages(Spire.Doc.Documents.ImageType.Metafile);
            for (int i = 0; i < images.Length; i++)
            {
                Metafile mf = images[i] as Metafile;
                Image newimage = ResetResolution(mf, 200);
                string outputfile = String.Format("image-{0}.png", i);
                newimage.Save(outputfile, System.Drawing.Imaging.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;
        }
    }
}