GcImaging v3

Render HTML Content to Images

Render HTML Content to Images

Saving web pages directly as HTML may not give the same rendering results as the original HTML, especially when viewing the page on different devices. When browsing the web, some content may need to be used in other formats, (Word, Excel, PowerPoint, or another documents). It's best to have the content as images, so you make edits like re-sizing, formatting or annotating through standard image editors.

GrapeCity Documents for HTML

The new GrapeCity Documents for Html package will help you render HTML content to Images. It is a utility package available with the GcPdf/GcImaging packages. A platform independent package, GcHtml can convert HTML content to images (PNG, JPEG, and PDF documents). To learn more, see what's New in GrapeCity Documents for PDF.

It also provides a set of convenient extension methods - DrawHtml() to render HTML content on the graphics. HTML rendering is done using the industry standard Chromium web browser engine working in headless mode, offering the advantage of rendering HTML to images on any platform (Windows, Linux, and macOS).

Users can additionally set various image settings while converting HTML to images. You do not need an additional license required to run GcHtml.

The package, GrapeCity.Documents.Html, includes the GcHtmlRenderer class, the main class that can render HTML content to Images. Internally, it uses one of three system-dependent HTML engine packages:

  • GrapeCity.Documents.Html.Windows.X64
  • GrapeCity.Documents.Html.Mac.X64
  • GrapeCity.Documents.Html.Linux.X64

GcHtml will automatically select the correct system-dependent engine package at runtime. That package must be referenced by your project so that GcHtml can find it. You can add references to all three platform packages to your project, or if you only target one or two platforms. Just add the relevant packages for the target platforms.

Key Features
  • Multi-platform package to render HTML content on Windows, macOS or Linux
  • Render HTML string/page/URI source to JPEG, PNG Images
  • Supports:
    • Images
    • CSS
    • JavaScript
    • HTML5
    • SVG
    • HTML Tables
  • Split long HTML content into multiple pages
  • Specify Page Margins
  • Specify JPEG Settings, PNG settings
    • DefaultBackgroundColor
    • WindowSize
    • Viewport
  • Specify JPEG Compression Quality (0%-100%)
  • Specify HtmlToImageFormat - formatting attributes for rendering HTML on GcGraphics

Read more about how to convert HTML content to images using GcImaging.

Help | GcHtml Architecture | Demo

Bitmap Glyphs Support

GcImaging now supports glyph bitmaps specified by the EBDT (Embedded bitmap data) table. With the new release, two new properties -AllowFontSbits and UseBitmapCache are added for rendering small text with low resolution in images.

Sbits are embedded bitmaps present in OpenType fonts for rendering complex glyphs at very small sizes. Such bitmaps usually appear in CJK fonts. If such bitmaps are provided in the font, the Font use these bitmaps when rendering the glyphs. Enlarge the two images below. The first one is before applying the bitmaps and the second one is after applying the AllowFontSBits and UseBitmapCache properties which looks much sharper.

Before applying bitmaps:

After applying bitmaps:

After applying bitmaps:

Documentation

GcImaging v2

The main highlight of the new GcImaging v2 sp2 is the addition of advanced algorithms to reduce color palettes in large images. This maintains the quality of the images. In addition, more features are added to the API that can help work with and combine images of multiple formats.

Read and Write Multi-Frame GIF Files

The GIF file format is the most popular graphic format that supports animation. As multiple frames contribute to an animated GIF File, it will now be possible to load, modify, and save such multi-frame GIF images in GcImaging. The GcGifReader and GcGifWriter classes have been added to the API that can be used for creating multi-frame GIF files and reading separate frames from existing GIF files.

You can control following features of GIF images:

  • Height and width of Logical Screen where the frames will be rendered
  • PixelAspectRatio
  • Number of iterations the animated GIF should be executed
  • Encoding for the comment string
  • Set optional GIF comment
  • Gets or sets the palette with opaque colors
  • IccProfileData
  • Add transparent color to global/local color tables
  • Get index of the next frame
  • Set horizontal/vertical resolution
  • Append frame with various settings

Help | Demo

Combine Two Bitmaps into a Single Image

Combine two bitmaps into a single image, with addition of Porter Duff compositing and blending algorithms which compute the color result from the composite source and destination images. The CompositeMode enumeration provides options to determine the final color while combining images, while BlendingMode provides the methods of mixing the colors with the backdrop.

These are used as one of the parameters in the new GcBitmap.CompositeAndBlend(..) method, which works as a scheme for blending the overlapping parts.

Help | Demo

Convert Images to Indexed Bitmap Images with Custom Palette and New Quantiser Algorithm

GcImaging now adds another method GenerateOctreePalette that can be used together with dithering methods. This can help reduce the number of unique colors in an image while keeping the general look of the image. This is particularly helpful if image contains large number of palette colors and the size becomes larger.

The Octree Quantizer algorithm used in GcImaging API can load images of any type, regardless of the number of different colors in the image, and then generate a good quality image with little degradation when converting from a high number of colors to an image with lesser size and lower number of palette colors.

Help | Demo

New Methods to Export/Import Color Channels

Color channels of images can be modified to make creative color adjustments to an image. It would be helpful to extract the color channel, apply modifications, and then use it in single or multiple images, which need to be similarly processed. With GcImaging, you will be able to export color channels from images, apply modification for example adjust the brightness or contrast levels, and import the color channel back into the image. This method can also be used to apply similar color channel to thousands of images together.

using (var bmp = new GcBitmap())
{
    bmp.Load(TestUtil.ImagePath + "tudor.jpg");
    using (var gb = new GrayscaleBitmap(bmp.PixelWidth, bmp.PixelHeight))
    {
        bmp.ExportColorChannel(gb, ColorChannel.Red);
        gb.AdjustLevels(0, 100, 150, 250);
        bmp.ImportColorChannel(gb, ColorChannel.Red);
    }
    bmp.SaveAsJpeg(outputStream);
}

Help

ICCProfile Support for Image Types

When images are transferred from one device to another, it is necessary that images follow the ICC profile specific to the device, or in other words, use the right color to display on any device. With GcImaging, you can now embed and extract ICC Profile data of/from an image. The support has been added to JPEG, GIF, PNG, and TIFF image formats. The property exists in -Indexed4bppBitmap, Indexed8bppBitmap, GrayscaleBitmap, and BilevelBitmap type of images.

using (var bmp = new GcBitmap())
{
    // load a PNG image with ICC profile
    bmp.Load(imagePath + "eggs1cHRM.png");

    Debug.WriteLine($"ICC profile consists of {bmp.IccProfileData.Length} bytes");

    // save the image with ICC profile as Jpeg
    bmp.SaveAsJpeg(outputStream);

Help

Lightweight Image Class

The Image class will now work with images with more efficiency and less memory usage. The Image class is now lightweight and loads pixels on demand. If you need to extract the size, format or Exif metadata from an image, the Image class will be more handier and performant than GcBitmap. The image can be associated with a single frame or a multi-frame GIF or TIFF file. The ToStream() method saves the whole source (such as a multi-frame GIF or TIFF) to the output stream.

This improvement can help load multiple images of different formats from files and streams, draw images on graphics or save them as is with the Image.ToStream() method, with very less memory consumption.

New Methods for Improving the Image Histogram (AutoLevel, AdjustLevels, AutoContrast)

The new GcBitmap.AutoLevel(), GcBitmap.AdjustLevel(), and GcBitmap.AutoContrast() methods can automatically adjust the right color intensities needed for the images and distribute the tones uniformly on the image histogram, in minimal lines of code.

Before AutoLevel

 After AutoLevel

See more details here:

var bmp = new GcBitmap("BeforeAutoLevel.jpg");
bmp.AutoLevel();
bmp.SaveAsJpeg("AfterAutoLevel.jpg");

Help | Demo

Support for all MS Excel Pattern Fills in the HatchStyle Enumeration

GcImaging has been supporting certain HatchStyle patterns for creating a HatchBrush and filling regions. It now supports total 48 Hatch Styles that fill image regions.

using (var bmp = new GcBitmap(300, 200, true))
{
    using (var g = bmp.CreateGraphics(Color.White))
    {
        var b = new HatchBrush(HatchStyle.Sphere)
        {
            ForeColor = Color.Navy,
            BackColor = Color.White
        };
        g.FillRectangle(new RectangleF(0, 0, 300, 200), b);
    }
    bmp.SaveAsPng(outputStream);
}

Demo