DsImaging is a collection of cross-platform .NET class libraries written in C#, providing an API that allows to create image from scratch and to load, analyze and modify existing images.
DsImaging is compatible with .NET Core 2.x/3.x, .NET Standard 2.x, .NET Framework 4.6.2 or higher, and .NET 6 or higher.
DsImaging and supporting packages are available on nuget.org:
To use DsImaging in an application, you need to reference just the DS.Documents.Imaging package. It pulls in the required infrastructure packages.
On a Windows system, you can optionally install DS.Documents.Imaging.Windows. It contains GcWicBitmap class that works efficiently with various image formats and allows drawing text and graphics using DirectWrite/Direct2D-based functionality. Also, it provides support for font linking specified in the Windows registry. This library can be referenced on a non-Windows system, but does nothing.
Namespaces | Description |
---|---|
GrapeCity.Documents.Drawing | Framework for drawing on the abstract GcGraphics surface. |
GrapeCity.Documents.Imaging | Types used to create, process and modify images. Nested namespaces contain types supporting specific image spec areas: |
GrapeCity.Documents.Text | Text processing sub-system. |
Grapecity.Documents.Imaging.Skia | Types used for drawing text and graphics using a highly optimized library SKIA. |
DsImaging provides classes for three main purposes.
DsImaging classes can be used efficiently in a multi-threaded environment. They don't depend on system handles or UI threads.
There are several containers in the DsImaging package (DS.Documents.Imaging) and in the related Windows specific package (DS.Documents.Imaging.Windows).
GrapeCity.Documents.Drawing.GcGraphics is an abstract base class for implementing graphics functionality for different targets. It allows to draw graphics primitives and text on various media, including GcBitmap, GcWicBitmap, and GcPdfDocument. The GcGraphics class offers roughly the same functionality as System.Drawing.Graphics class in WinForms but is platform-independent and provides implementations for different targets.
The GcBitmapGraphics class is derived from GcGraphics and allows to draw on a GcBitmap. Use GcBitmap.CreateGraphics() method to create an instance of GcBitmapGraphics. Likewise, GcWicBitmap.CreateGraphics() method creates an instance of GcWicBitmapGraphics that can be used to draw on a GcWicBitmap. Please note that you need to dispose the graphics objects after use.
Classes like GcBitmapGraphics and GcWicBitmapGraphics obey the universal object model for drawing with GcGraphics. Internally, both classes are based on more specific implementations targeting the actual media, such as GcBitmap or GcWicBitmap.
The target-specific renderer classes like BitmapRenderer for GcBitmap and GrapeCity.Documents.DX.Direct2D.RenderTarget for GcWicBitmap provide access to various fine-tuning settings and to methods not supported by the universal GcGraphics abstract class.
For example, you must work with BitmapRenderer to update anti aliasing setting or to enable multi threading during the rendering phase. An instance of BitmapRenderer is available through the GcBitmap.Renderer and GcBitmapGraphics.Renderer properties. An important feature provided by BitmapRenderer is the capability to work with lightweight objects called regions, that can be created from simple figures and graphics paths. Regions can be combined using various logical operations, then filled with brushes or used for clipping.
DsImaging has special support for multi page TIFF format. GcTiffReader allows to read individual images from a multi page TIFF file or stream. After the proper initialization, the user can access GcTiffReader.Frames property, which is a list of TiffFrame class instances. TiffFrame is a lightweight reference to the actual image data. It allows to read the frame image into one of the container classes, such as BilevelBitmap or GcBitmap. GcTiffReader works on any platform but has some limitations. For example, it does not currently support TIFF-JPEG compression scheme.
The GcWicTiffReader from GrapeCity.Documents.Imaging.Windows namespace in DS.Documents.Imaging.Windows package is a platform-dependent counterpart for GcTiffReader. It is based on the Windows Imaging Component subsystem and supports a few color spaces and compression schemes which are currently not available with platform-independent GcTiffReader. The Frames collection in GcWicTiffReader contains instances of the WicTiffFrame class. It allows to read frame images into the GcWicBitmap image container.
GcTiffWriter is a platform-independent class making it possible to create a multi page TIFF file or stream from a set of individual images. You can append images, such as GrayscaleBitmap, Indexed8bppBitmap and so on, to a GcTiffWriter and specify the detailed settings controlling the frame storage format and metadata using the TiffFrameSettings class. GcTiffWriter fully supports the Baseline TIFF specification and several TIFF extensions, such as tiled images, the Deflate compression scheme, associated and unassociated Alpha and other features.
GcWicTiffWriter is a Windows-specific WIC-based class that allows to write GcWicBitmaps to TIFF as separate frames. It does not offer much functionality beyond GcTiffWriter, but may be handy when drawing images to GcWicBitmap and saving them as a bunch.
DsHtml is a utility library that renders HTML to PDF file or an image in PNG, JPEG, and WebP format. DsHtml uses a Chrome or Edge browser (already installed in the current system, or downloaded from a public web site) in headless mode. Also, it doesn’t matter whether your .NET application is built for x64, x86 or AnyCPU platform target. The browser is continuously working in a separate process.
The DS.Documents.Html library consists of a platform-independent main package that exposes the HTML rendering functionality. The main package contains the following namespaces:
Namespaces | Description |
---|---|
GrapeCity.Documents.Drawing |
It provides extension methods and formatting attributes for rendering HTML to image. The namespace comprises the following classes: |
GrapeCity.Documents.Html |
It provides methods for converting HTML to PDF or images and defines parameters for the PDF or image. The namespace comprises the following classes: |
GrapeCity.Documents.Pdf |
It provides the extension methods for rendering HTML to image and represents the formatting attributes for rendering HTML to image. The namespace comprises the following classes: |
The BrowserFetcher class has two static methods: GetSystemChromePath() and GetSystemEdgePath(). The methods return the path to an executable file of Chrome or Edge browsers correspondingly. Another option is to download and install Chromium into a local folder. You can create an instance of BrowserFetcher and pass the information such as host, platform, revision, and the destination folder, if needed. Then, execute the BrowserFetcher.GetDownloadedPath() method which downloads Chromium, if required, and returns the path to an executable file for running the Chromium.
The GcHtmlBrowser class provides methods for converting HTML to PDF and images. With path to executable file for running the Chromium fetched using BrowserFetcher class, we can create an instance of GcHtmlBrowser class which effectively runs the browser process in the background. GcHtmlBrowser also accepts another parameter of LaunchOptions type. The LaunchOptions class provides various settings specific to launching the browser.
The class has two important methods: NewPage(Uri uri) and NewPage(string html). Both methods return an instance of HtmlPage class which represents a browser tab after navigating to the specified web address, file, or the arbitrary HTML content. The second parameter of PageOptions type provides various properties to be applied to the new browser page such as username, password for HTTP authentication, disabling JavaScript, lazy loading etc.
Note:
The HtmlPage class represents a browser tab after navigating to the specified web address, file, or the arbitrary HTML content. The class has methods such as SaveAsPng, SaveAsJpeg, and SaveAsWebp to save the current page as a raster image of PNG, JPEG, or WebP formats respectively. The first parameter of these methods specifies the destination file or stream. The second parameter passes the additional options for rendering HTML page as single image, scaling or cropping the image, or setting the image quality.
The HtmlPage class contains the additional methods that help to interact with HTML page content. For example, you can obtain the full HTML content of the page using the GetContent method. The SetContent method updates the HTML markup. You can reload the web page with the Reload method or even execute a script in the browser context using the EvaluateExpression method. The WaitForNetworkIdle method helps with loading asynchronous web content.
ImageOptions is the base abstract class for three specific classes: PngOptions, WebpOptions and JpegOptions. As compared to PngOptions and WebpOptions classes, the JpegOptions class has an additional property(CompressionQuality) for providing the JPEG compression quality (from 0% to 100%).
The FullPage property allows to capture the whole scrollable page. While the Clip property specifies the region to capture (if FullPage is false). Clip and Scale properties work with the result of layout. They allow to extract and scale some rectangular area and are applied before the rasterization stage. So, any graphics remains crisp with any scale factor. When exporting HTML to images the Dots Per Inch (DPI) is not set in the resulting image file. It requires some post-processing in order to set DPI.
The HtmlToImageFormat class represents the formatting attributes for rendering HTML to GcGraphics as an image. Also, it helps converting HTML to a GcBitmap.
MaxTopMargin, MaxBottomMargin, MaxLeftMargin, MaxRightMargin properties specify the maximum allowable margins of the resulting image (larger margins will be trimmed), in pixels. Setting these properties to a negative value prevents trimming the margins. All those properties are equal to 0 by default which means no margins.
Other properties of HtmlToImageFormat are mapped to the corresponding properties of the ImageOptions/PageOptions class:
HtmlToImageFormat Property | ImageOptions/PageOptions Property |
DefaultBackgroundColor | PageOptions.DefaultBackgroundColor |
WindowSize | PageOptions.WindowSize |
MaxWindowWidth | PageOptions.WindowSize.Width |
MaxWindowHeight | PageOptions.WindowSize.Height |
FullPage | ImageOptions.FullPage |
Scale | ImageOptions.Scale |
Clip | ImageOption.Clip |
DsHtml provides 2 methods that extend GcBitmapGraphics and allow to render an HTML text or page as an image:
Namespaces | Description |
---|---|
Grapecity.Documents.Imaging.Skia | Types used for drawing text and graphics using a highly optimized library SKIA. |
Skia comprises the following main classes:
For more information about Skia library, see Render using Skia Library.