Convert TXT to PNG

A file with .TXT extension represents a text document that contains plain text in the form of lines. Paragraphs in a text document are recognized by carriage returns and are used for better arrangement of file contents. A standard text document can be opened in any text editor or word processing application on different operating systems. All the text contained in such a file is in human-readable format and represented by sequence of characters.

Steps to convert TXT to PNG in C#

GroupDocs.Conversion allows developers to convert the TXT file to PNG format in an easy and intuitive way just using a few lines of code as described below:

  • Create an instance of Converter class and pass source TXT file path as a constructor parameter. You may specify absolute or relative file path as per your requirements.
  • Declare SavePageStream delegate, which should provide a stream to store converted page of TXT document.
  • Create an instance of ImageConvertOptions class and set Format property to GroupDocs.Conversion.FileTypes.ImageFileType.Png.
  • Call Converter class Convert method and pass the filename for the converted PNG file and the ImageConvertOptions object from the previous step as parameters.
// Load the source TXT file
using (Converter converter = new Converter("sample.txt"))
{
    string outputFileTemplate = Path.Combine(outputFolder, "converted-page-{0}.png");
    GroupDocs.Conversion.Contracts.SavePageStream getPageStream = page => new FileStream(string.Format(outputFileTemplate, page), FileMode.Create);

    // Set the convert options for PNG format
    var options = new ImageConvertOptions { Format = GroupDocs.Conversion.FileTypes.ImageFileType.Png };   
    // Convert to PNG format
    converter.Convert(getPageStream, options);
}

Code Examples

Please find more use-cases and complete C# sources of our backend and frontend examples and try them for free!

TXT to PNG Live Demo

GroupDocs.Conversion for .NET provides an online TXT to PNG converter, which allows you to try it for free and check conversion quality and accuracy.

“Convert TXT to PNG”