Set output image size limits
GroupDocs.Viewer also provides the feature to set limits for width/height for the output image. Follow the below steps to achieve this functionality. If you set MaxWidth/MaxHeight options, if the image exceeds one of these limits - it will be resized proportionally.
Instantiate the Viewer object;
Instantiate the PngViewOptions or JpgViewOptions;
Set MaxWidth and/or MaxHeight values;
Call View method.
The following code sample shows how to set the output image size limits when rendering the document.
using (Viewer viewer = new Viewer("sample.jpg"))
{
JpgViewOptions viewOptions = new JpgViewOptions("result_{0}.jpg");
//PngViewOptions viewOptions = new PngViewOptions("result_{0}.png");
viewOptions.MaxWidth = 800;
viewOptions.MaxHeight = 600;
viewer.View(viewOptions);
}
PngViewOptions and JpgViewOptions implement special interface IMaxSizeOptions, which contain properties for size limits.
/// <summary>
/// Limits of image size options interface.
/// </summary>
public interface IMaxSizeOptions
{
/// <summary>
/// Max width of an output image in pixels.
/// </summary>
int MaxWidth { get; set; }
/// <summary>
/// Max height of an output image in pixels.
/// </summary>
int MaxHeight { get; set; }
}
Note: If you set Width/Height in options, MaxWidth/MaxHeight options will be ignored.
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples:
- GroupDocs.Viewer for .NET examples, plugins, and showcase
- GroupDocs.Viewer for Java examples, plugins, and showcase
- Document Viewer for .NET MVC UI Example
- Document Viewer for .NET App WebForms UI Modern Example
- Document Viewer for Java App Dropwizard UI Modern Example
- Document Viewer for Java Spring UI Example
Free Online App
Along with full-featured .NET library we provide simple but powerful free Apps. You are welcome to view Word, PDF, Excel, PowerPoint documents with free to use online GroupDocs Viewer App.