Set image or PDF colors in C#

The color property is one of the most frequently used parameters in Nutrient .NET SDK (formerly GdPicture.NET) methods. It’s used when:

  • Drawing shapes
  • Adding annotations
  • Creating images
  • Setting up borders

There are two main ways to set a color:

Color object

Use the Color object to specify the color you want to use. It’s part of the System.Drawing namespace. For more information about available properties, refer to the Microsoft documentation(opens in a new tab).

To set the bottom border color of an image to aquamarine, use the following code:

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg", true);
int borderHeight = 10;
// Set the bottom border color to aquamarine.
Color borderColor = Color.Aquamarine;
gdpictureImaging.AddBorderBottom(imageID, borderHeight, borderColor);
gdpictureImaging.SaveAsPNG(imageID, @"C:\temp\output.png");
gdpictureImaging.ReleaseGdPictureImage(imageID);

ARGB method

The ARGB method returns a Color object and accepts one of the following:

  • 32-bit color value (0x78FF0000)
  • RGB code (255, 87, 51)
  • Alpha value (opacity in range between 0 and 255) and RGB code (100, 255, 87, 51)

To set the right border color of an image to dark violet with 80 percent opacity, use the following code:

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg", true);
int borderHeight = 10;
// Set the right border color to dark violet with 80 percent opacity.
Color borderColor = gdpictureImaging.ARGB(204, 148, 0, 211);
gdpictureImaging.AddBorderRight(imageID, borderHeight, borderColor);
gdpictureImaging.SaveAsPNG(imageID, @"C:\temp\output.png");
gdpictureImaging.ReleaseGdPictureImage(imageID);

Color conversion

This section outlines ways to convert colors.

From RGB to 32-bit

Use the ARGBI method to convert a color from RGB to a 32-bit value:

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
int bitColor = gdpictureImaging.ARGBI(100, 255, 87, 51));

Other conversion methods

Using other conversion methods results in receiving reference parameters depending on the method used. The following code gives you the red, green, and blue parameters of the dark violet RGB code:

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
int red = 0, green = 0, blue = 0;
// Get the RGB code from CMYK for dark violet.
gdpictureImaging.ColorCMYKtoRGB(30, 100, 0, 17, ref red, ref green, ref blue);
Console.WriteLine(red + ", " + green + ", " + blue);

Below is the list of all possible conversion methods: