Set Image or PDF Colors in C#

The color property is one of the most frequently used parameters in 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.

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);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging()
    Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg", True)
    Dim borderHeight = 10
    ' Set the bottom border color to aquamarine.
    Dim borderColor As Color = Color.Aquamarine
    gdpictureImaging.AddBorderBottom(imageID, borderHeight, borderColor)
    gdpictureImaging.SaveAsPNG(imageID, "C:\temp\output.png")
    gdpictureImaging.ReleaseGdPictureImage(imageID)
End Using
Used Methods

Related Topics

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);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging()
    Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg", True)
    Dim borderHeight = 10
    ' Set the right border color to dark violet with 80 percent opacity.
    Dim borderColor As Color = gdpictureImaging.ARGB(204, 148, 0, 211)
    gdpictureImaging.AddBorderRight(imageID, borderHeight, borderColor)
    gdpictureImaging.SaveAsPNG(imageID, "C:\temp\output.png")
    gdpictureImaging.ReleaseGdPictureImage(imageID)
End Using
Used Methods

Related Topics

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));
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging()
    Dim bitColor As Integer = gdpictureImaging.ARGBI(100, 255, 87, 51)
End Using

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);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging()
    Dim red = 0, green = 0, blue = 0
    ' Get the RGB code from CMYK for dark violet.
    gdpictureImaging.ColorCMYKtoRGB(30, 100, 0, 17, red, green, blue)
    Console.WriteLine(red & ", " & green & ", " & blue)
End Using

Below is the list of all possible conversion methods: