Download

In Visual Studio

The preferred way is by the NuGet Package Manager in Visual Studio

  • You can use either the Package Manager Console:
    PM> Install-Package KGySoft.Drawing
  • Or the Package Manger GUI by the Manage NuGet Packages… context menu item of the project.

Direct Download

Alternatively, you can download the package directly from nuget.org.

If you prefer a .zip file containing the binaries see the releases on GitHub.

Source Code

The source is available on GitHub.

You can find both the source code and the binaries as .zip files among the releases.

Demo Applications and Debugger Visualizers

KGySoft.Drawing.Tools is another GitHub repository, which contains a couple of applications built on KGy SOFT Drawing Libraries along with debugger visualizers for several System.Drawing types including Bitmap, Metafile, Icon, Graphics and more.

If you use Visual Studio 2013 or newer the simplest way is to download the installer package from the VisualStudio Marketplace.

Otherwise, you can download the binaries for other versions from the GitHub repo.

Help

Browse the online documentation for examples and detailed descriptions.

Examples

Icon Manipulation

Icon images of different resolutions and color depth can be extracted from an Icon, whereas Bitmap and Icon instances can be combined into a new Icon. PNG compressed icons are also supported.

// extracting the 256x256 image from an icon:
Bitmap bmp = Icons.ExtractBitmap(new Size(256, 256));

// combining an existing icon with a bitmap:
Icon combined = myIcon.Combine(bmp);

Tip: See more details at the Icons and IconExtensions classes.


Fast Bitmap Manipulation

As it is well known, Bitmap.SetPixel/GetPixel methods are very slow. Additionally, they do not support every pixel format. A typical solution can be to obtain a BitmapData by the LockBits method, which has further drawbacks: you need to use unsafe code and pointers and the way you need to access the bitmap data depends on the actual PixelFormat of the bitmap.

KGy SOFT Drawing Libraries offer very fast and convenient way to overcome these issues. A managed accessor can be obtained by the GetReadableBitmapData, GetWritableBitmapData and GetReadWriteBitmapData methods.

var targetFormat = PixelFormat.Format8bppIndexed; // feel free to try other formats as well
using (Bitmap bmpSrc = Icons.Shield.ExtractBitmap(new Size(256, 256)))
using (Bitmap bmpDst = new Bitmap(256, 256, targetFormat))
{
    using (IReadableBitmapData dataSrc = bmpSrc.GetReadableBitmapData())
    using (IWritableBitmapData dataDst = bmpDst.GetWritableBitmapData())
    {
        IReadableBitmapDataRow rowSrc = dataSrc.FirstRow;
        IWritableBitmapDataRow rowDst = dataDst.FirstRow;
        do
        {
            for (int x = 0; x < dataSrc.Width; x++)
                rowDst[x] = rowSrc[x]; // works also between different pixel formats

        } while (rowSrc.MoveNextRow() && rowDst.MoveNextRow());
    }

    bmpSrc.SaveAsPng(@"c:\temp\bmpSrc.png");
    bmpDst.SaveAsPng(@"c:\temp\bmpDst.png"); // or saveAsGif/SaveAsTiff to preserve the indexed format
}

Tip: See more examples with image examples at the GetReadWriteBitmapData extension method.

If you know the actual pixel format you can also access the raw data in a managed way. See the IReadableBitmapDataRow.ReadRaw and IWritableBitmapDataRow.WriteRaw method for details and examples.


Quantizing and Dithering

KGy SOFT Drawing Libraries offer quantizing (reducing the number of colors on an image) and dithering (techniques for preserving the details of a quantized image) in several ways:

  • The ConvertPixelFormat extension method returns a new Bitmap as the result of the quantizing/dithering.
  • The Quantize and Dither extension methods modify the original Bitmap.
  • The DrawInto overloads can use dithering when drawing an Image into a Bitmap if they have different pixel formats.
  • Several further extension methods in the BitmapExtensions class have an IDitherer parameter.

Tip:

See the following examples for the possible quantization results (click the images for displaying in full size):

Color hues with alpha gradient
Original image: Color hues with alpha gradient
Color hues quantized with custom 8 color palette and silver background
Color hues quantized with custom 8 color palette and silver background, no dithering. The bottom part turns white because white is the nearest color to silver.
Color hues quantized with custom 8 color palette and silver background, using Bayer 8x8 dithering
Color hues quantized with custom 8 color palette and silver background, using Bayer 8×8 dithering
Grayscale color shades
Original image: Grayscale color shades
Grayscale color shades quantized with black and white palette
Grayscale color shades quantized with black and white palette, no dithering
Grayscale color shades quantized with black and white palette, using blue noise dithering
Grayscale color shades quantized with black and white palette, using blue noise dithering
Test image "Lena"
Original test image “Lena”
Test image "Lena" quantized with system default 8 BPP palette
Test image “Lena” quantized with system default 8 BPP palette, no dithering
Test image "Lena" quantized with system default 8 BPP palette using Bayer 8x8 dithering
Test image “Lena” quantized with system default 8 BPP palette using Bayer 8×8 dithering
Test image "Lena" quantized with system default 8 BPP palette using Floyd-Steinberg dithering
Test image “Lena” quantized with system default 8 BPP palette using Floyd-Steinberg dithering
Test image "Cameraman"
Original test image “Cameraman”
Test image "Cameraman" quantized with black and white palette
Test image “Cameraman” quantized with black and white palette, no dithering
Test image "Cameraman" quantized with black and white palette using Floyd-Steinberg dithering
Test image “Cameraman” quantized with black and white palette using Floyd-Steinberg dithering

License

This repository is under the KGy SOFT License 1.0, which is a permissive GPL-like license. It allows you to copy and redistribute the material in any medium or format for any purpose, even commercially. The only thing is not allowed is to distribute a modified material as yours: though you are free to change and re-use anything, do that by giving appropriate credit. See the LICENSE file for details.

Scroll to Top