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 GdPictureColor object to specify the color you want to use. It’s part of the GdPicture14.Imaging namespace. It provides named colors such as GdPictureColor.Red and GdPictureColor.Teal. For other colors, use the GdPictureColor.FromArgb method.
To set the bottom border color of an image to aquamarine, use the following code:
Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
using GdPicture14;using GdPicture14.Imaging;
LicenseManager license = new LicenseManager();license.RegisterKEY("");{using GdPictureImaging gdpictureImaging = new GdPictureImaging();int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"input.jpg", true);int borderHeight = 10;// Set the bottom border color to aquamarine.GdPictureColor borderColor = GdPictureColor.FromArgb(127, 255, 212);gdpictureImaging.AddBorderBottom(imageID, borderHeight, borderColor);gdpictureImaging.SaveAsPNG(imageID, @"output.png");gdpictureImaging.ReleaseGdPictureImage(imageID);}Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
Imports GdPicture14Imports GdPicture14.Imaging
Dim license As LicenseManager = New LicenseManager()license.RegisterKEY("")Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("input.jpg", True) Dim borderHeight = 10 ' Set the bottom border color to aquamarine. Dim borderColor As GdPictureColor = GdPictureColor.FromArgb(127, 255, 212) gdpictureImaging.AddBorderBottom(imageID, borderHeight, borderColor) gdpictureImaging.SaveAsPNG(imageID, "output.png") gdpictureImaging.ReleaseGdPictureImage(imageID)End UsingARGB method
The ARGB method returns a GdPictureColor 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(@"input.jpg", true);int borderHeight = 10;// Set the right border color to dark violet with 80 percent opacity.GdPictureColor borderColor = gdpictureImaging.ARGB(204, 148, 0, 211);gdpictureImaging.AddBorderRight(imageID, borderHeight, borderColor);gdpictureImaging.SaveAsPNG(imageID, @"output.png");gdpictureImaging.ReleaseGdPictureImage(imageID);}Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("input.jpg", True) Dim borderHeight = 10 ' Set the right border color to dark violet with 80 percent opacity. Dim borderColor As GdPictureColor = gdpictureImaging.ARGB(204, 148, 0, 211) gdpictureImaging.AddBorderRight(imageID, borderHeight, borderColor) gdpictureImaging.SaveAsPNG(imageID, "output.png") gdpictureImaging.ReleaseGdPictureImage(imageID)End UsingColor 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);}Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim bitColor As Integer = gdpictureImaging.ARGBI(100, 255, 87, 51)End UsingOther conversion methods
Using other conversion methods results in receiving reference parameters depending on the method used. Each CMYK component ranges from 0 to 255. The following code gives you the red, green, and blue parameters of the dark violet RGB code (148, 0, 211):
{using GdPictureImaging gdpictureImaging = new GdPictureImaging();int red = 0, green = 0, blue = 0;// Get the RGB code from CMYK for dark violet.gdpictureImaging.ColorCMYKtoRGB(77, 255, 0, 44, ref red, ref green, ref blue);Console.WriteLine(red + ", " + green + ", " + blue);}Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim red = 0, green = 0, blue = 0 ' Get the RGB code from CMYK for dark violet. gdpictureImaging.ColorCMYKtoRGB(77, 255, 0, 44, red, green, blue) Console.WriteLine(red & ", " & green & ", " & blue)End UsingBelow is the list of all possible conversion methods:
ColorCMYKtoRGB– CMYK to RGBColorCMYtoRGB– CMY to RGBColorHSLtoRGB– HSL to RGBColorRGBtoCMYK– RGB to CMYKColorRGBtoCMY– RGB to CMYColorRGBtoHSL– RGB to HSL