Convert to Image with advanced options
Leave feedback
GroupDocs.Conversion provides ImageConvertOptions to give you control over conversion result when convert to image. Along with common convert options from the base class, ImageConvertOptions has the following additional options:
- Format specifies desired result document type. Available options are: Tiff, Tif, Jpg, Jpeg, Png, Gig, Bmp, Ico, Psd, Wmf, Emf, Dcm, Webp, Dng, Svg, Jp2, Odg, J2c, J2k, Jpx, Jpf, Jpm, Eps, Cgm, Cdr, Cmx, Dib, Jpc, Jls, DjVu.
- Width specifies desired image width after conversion.
- Height specifies desired image height after conversion.
- HorizontalResolution specifies desired image horizontal resolution after conversion.
- VerticalResolution specifies desired image vertical resolution after conversion.
- Grayscale specifies if true converted image will be grayscaled.
- RotateAngle specifies image rotation angle.
- FlipMode specifies image flip mode. Available options are: None, FlipX, FlipY, FlipXY.
- Brightness adjusts image brightness.
- Contrast adjusts image contrast.
- Gamma adjusts image gamma.
- BackgroundColor sets the background color where supported by the source format.
- CropArea crops a rectangular area from the raster image after conversion. Specify as Rectangle(x, y, width, height).
- JpegOptions contains JPEG specific convert options.
- TiffOptions contains TIFF specific convert options.
- PsdOptions contains PSD specific convert options.
- WebpOptions contains WebP specific convert options.
- UsePdf. Sometimes, for better rendering and elements positioning the source document should be converted to PDF first. If this property is set to true, the input firstly is converted to PDF and after that to desired format.
The following code snippet shows how to convert to image with advanced options:
With v24.10 and later:
string outputFileTemplate = Path.Combine(@"c:\output", "converted-page-{0}.png");
Func<SavePageContext, Stream> getPageStream = saveContext => new FileStream(string.Format(outputFileTemplate, saveContext.Page), FileMode.Create);
using (Converter converter = new Converter("sample.pdf"))
{
ImageConvertOptions options = new ImageConvertOptions
{
Format = ImageFileType.Png,
FlipMode = ImageFlipModes.FlipY,
Brightness = 50,
Contrast = 50,
Gamma = 0.5F,
Grayscale = true,
HorizontalResolution = 300,
VerticalResolution = 100
};
converter.Convert(getPageStream, options);
}
Before v24.10:
string outputFileTemplate = Path.Combine(@"c:\output", "converted-page-{0}.png");
Func<int, Stream> getPageStream = page => new FileStream(string.Format(outputFileTemplate, page), FileMode.Create);
using (Converter converter = new Converter("sample.pdf"))
{
ImageConvertOptions options = new ImageConvertOptions
{
Format = ImageFileType.Png,
FlipMode = ImageFlipModes.FlipY,
Brightness = 50,
Contrast = 50,
Gamma = 0.5F,
Grayscale = true,
HorizontalResolution = 300,
VerticalResolution = 100
};
converter.Convert(getPageStream, options);
}
Set a background color for the converted image where supported by the source format:
using System.Drawing;
using (Converter converter = new Converter("sample.pdf"))
{
ImageConvertOptions options = new ImageConvertOptions
{
Format = ImageFileType.Png,
BackgroundColor = Color.White // Use System.Drawing.Color
};
converter.Convert("white-background.png", options);
}
You can use any color from System.Drawing.Color or create custom colors:
using System.Drawing;
using (Converter converter = new Converter("sample.pdf"))
{
ImageConvertOptions options = new ImageConvertOptions
{
Format = ImageFileType.Png,
BackgroundColor = Color.FromArgb(173, 216, 230) // Light blue (RGB)
};
converter.Convert("custom-background.png", options);
}
Crop a specific rectangular area from the converted image. The CropArea property takes a Rectangle with X, Y coordinates for the top-left corner, and Width, Height for the dimensions:
using GroupDocs.Conversion.Contracts;
using (Converter converter = new Converter("sample.pdf"))
{
ImageConvertOptions options = new ImageConvertOptions
{
Format = ImageFileType.Png,
CropArea = new Rectangle(100, 100, 400, 300) // X, Y, Width, Height
};
converter.Convert("cropped-image.png", options);
}
You can combine BackgroundColor and CropArea:
using System.Drawing;
using GroupDocs.Conversion.Contracts;
using (Converter converter = new Converter("sample.pdf"))
{
ImageConvertOptions options = new ImageConvertOptions
{
Format = ImageFileType.Png,
BackgroundColor = Color.LightGray,
CropArea = new Rectangle(50, 50, 512, 512)
};
converter.Convert("background-and-crop.png", options);
}
JpegOptions is subset of ImageConvertOptions which allow enhanced control over conversions to JPEG format.
The following options are available:
- Quality specifies desired image quality.
- ColorMode sets JPEG color mode. Available options are: Rgb, YCbCr, Cmyk, Ycck, Grayscale.
- Compression sets JPEG compression methods. Available options are: Baseline, Progressive, Lossless, JpegLs.
TiffOptions is subset of ImageConvertOptions which allow enhanced control over conversions to TIFF format.
The following options are available:
- Compression sets TIFF compression method. Available options are: None, Lzw, Ccitt3, Ccitt4, Rle.
PsdOptions is subset of ImageConvertOptions which allow enhanced control over conversions to PSD format.
The following options are available:
- ChannelBitsCount sets bits count per channel.
- ChannelsCount sets color channels count.
- ColorMode sets PSD color mode. Available options are: Bitmap, Grayscale, Indexed, Rgb, Cmyk, Multichannel, Duotone, Lab.
- Compression sets PSD compression method. Available options are: Raw, Rle, ZipWithoutPrediction, ZipWithPrediction.
- Version sets desired PSD version.
WebpOptions is subset of ImageConvertOptions which allow enhanced control over conversions to WebP format.
The following options are available:
- Lossless sets the compression of the converted image will be lossless.
- Quality sets set the quality of converted image.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.