DEV Community

Atir Tahir
Atir Tahir

Posted on

A generic code to convert a file to image format

You can convert any document to Image using following piece of code:

string outputFileTemplate = Path.Combine(outputFolder, "converted-page-{0}.jpg");
SavePageStream getPageStream = page => new FileStream(string.Format(outputFileTemplate, page), FileMode.Create);
using (Converter converter = new Converter("sample.pdf"))
{
    ImageConvertOptions options = new ImageConvertOptions
    {
        Format = ImageFileType.Jpg
    };

    converter.Convert(getPageStream, options);
}
Enter fullscreen mode Exit fullscreen mode

Quite simple, isn't it? Let's see what conversion options API facilitates.

  • Brightness
  • Contrast
  • FlipMode
  • Gamma
  • Grayscale

and a lot others. ImageFileType defines the output image file type (e.g. PNG, JPG, SVG).
Get more details or post any API related issue here.

Top comments (0)