Add text to an image using C#
Adding text to images
To add simple, one-line text to an image file, use the DrawText
method. It requires the parameters outlined below.
ImageID
— The GdPicture image ID of the loaded image.Text
— The body of the text.DstLeft
— The horizontal distance between the coordinate origin and the upper-left corner of the text (in pixels).DstTop
— The vertical distance between the coordinate origin and the upper-left corner of the text (in pixels).FontSize
— The font size in units specified by theFontSetUnit
method. The possible values are the following:UnitPoint
UnitInch
UnitPixel
UnitDocument
(1/300 inch)
FontStyle
— The font size set by theFontStyle
enumeration. The possible values are the following:Regular
Bold
Italic
Underline
Strikeout
TextColor
— The text color specified by theARGB
method or theColor
object.FontName
—The name of the font used.AntiAlias
— Applies the antialiasing algorithm if set totrue
.
To add text to an image, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging();int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");// Set the font size unit to points.gdpictureImaging.FontSetUnit(UnitMode.UnitPoint);// Add the text to the image.gdpictureImaging.DrawText(imageID, "GdPicture.NET", 5, 5, 40, FontStyle.Regular, gdpictureImaging.ARGB(100, 128, 128, 128), "Arial", true);gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\output.jpg");gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Set the font size unit to points. gdpictureImaging.FontSetUnit(UnitMode.UnitPoint) ' Add the text to the image. gdpictureImaging.DrawText(imageID, "GdPicture.NET", 5, 5, 40, FontStyle.Regular, gdpictureImaging.ARGB(100, 128, 128, 128), "Arial", True) gdpictureImaging.SaveAsJPEG(imageID, "C:\temp\output.jpg") gdpictureImaging.ReleaseGdPictureImage(imageID)End Using
Adding gradient text to images
To add text with a linear gradient to an image, use the DrawTextGradient
method. This method uses the same parameters as the DrawText
method, but the TextColor
parameter is replaced by the StartColor
and EndColor
parameters. These parameters accept either the ARGB
method or the Color
object.
To add gradient text to an image, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging();int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");// Set the font size unit to points.gdpictureImaging.FontSetUnit(UnitMode.UnitPoint);// Add the text with a black-to-white gradient to the image.gdpictureImaging.DrawTextGradient(imageID, "GdPicture.NET", 100, 50, gdpictureImaging.ARGB(255, 0, 0, 0), gdpictureImaging.ARGB(255, 255, 255, 255), 40, FontStyle.Regular, "Arial", true);gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\output.jpg");gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Set the font size unit to points. gdpictureImaging.FontSetUnit(UnitMode.UnitPoint) ' Add the text with a black-to-white gradient to the image. gdpictureImaging.DrawTextGradient(imageID, "GdPicture.NET", 100, 50, gdpictureImaging.ARGB(255, 0, 0, 0), gdpictureImaging.ARGB(255, 255, 255, 255), 40, FontStyle.Regular, "Arial", True) gdpictureImaging.SaveAsJPEG(imageID, "C:\temp\output.jpg") gdpictureImaging.ReleaseGdPictureImage(imageID)End Using
Adding textured text
To add textured text to an image, use one of the following methods:
DrawTextTextureFromFile
— Texture source as a file.DrawTextTextureFromGdPictureImage
— Texture source as a GdPicture image.
These methods use the same parameters as the DrawText
method, but the TextColor
parameter is replaced by the following:
TextureFilePath
in theDrawTextTextureFromFile
. This parameter specifies the path to the file that’s used as the texture of the text.ImageTexture
in theDrawTextTextureFromGdPictureImage
. This parameter specifies the image ID of the file loaded in your project.
To add textured text to an image, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging();int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");// Set the font size unit to points.gdpictureImaging.FontSetUnit(UnitMode.UnitPoint);// Add the text with a black-to-white gradient to the image.gdpictureImaging.DrawTextTextureFromFile(imageID, @"C:\temp\texture.jpg", 100, 50, 40, FontStyle.Regular, "Arial", true);gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\output.jpg");gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Set the font size unit to points. gdpictureImaging.FontSetUnit(UnitMode.UnitPoint) ' Add the text with a black-to-white gradient to the image. gdpictureImaging.DrawTextTextureFromFile(imageID, "C:\temp\texture.jpg", 100, 50, 40, FontStyle.Regular, "Arial", true) gdpictureImaging.SaveAsJPEG(imageID, "C:\temp\output.jpg") gdpictureImaging.ReleaseGdPictureImage(imageID)End Using
Used methods
Related topics
Adding rotated text to images
To add text rotated at an angle to an image, use the DrawRotatedText
method. It requires the parameters outlined below.
ImageID
— The GdPicture image ID of the loaded image.Angle
— The clockwise angle at which the text is rotated (in degrees).Text
— The body of the text.DstLeft
— The horizontal distance between the coordinate origin and the upper-left corner of the text (in pixels).DstTop
— The vertical distance between the coordinate origin and the upper-left corner of the text (in pixels).FontSize
— The font size in units specified by theFontSetUnit
method. The possible values are the following:UnitPoint
UnitInch
UnitPixel
UnitDocument
(1/300 inch)
FontStyle
— The font size set by theFontStyle
enumeration. The possible values are the following:Regular
Bold
Italic
Underline
Strikeout
TextColor
— The text color specified by theARGB
method or theColor
object.FontName
— The name of the font used.AntiAlias
— Applies the antialiasing algorithm if set totrue
.
To add text to an image rotated at a 90-degree angle, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging();int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");// Set the font size unit to points.gdpictureImaging.FontSetUnit(UnitMode.UnitPoint);// Add the text to the image rotated at 90 degrees clockwise.gdpictureImaging.DrawRotatedText(imageID, 90, "GdPicture.NET", 100, 50, 40, FontStyle.Regular, gdpictureImaging.ARGB(100, 128, 128, 128), "Arial", true);gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\output.jpg");gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Set the font size unit to points. gdpictureImaging.FontSetUnit(UnitMode.UnitPoint) ' Add the text to the image rotated at 90 degrees clockwise. gdpictureImaging.DrawRotatedText(imageID, 90, "GdPicture.NET", 100, 50, 40, FontStyle.Regular, gdpictureImaging.ARGB(100, 128, 128, 128), "Arial", True) gdpictureImaging.SaveAsJPEG(imageID, "C:\temp\output.jpg") gdpictureImaging.ReleaseGdPictureImage(imageID)End Using
Used methods
Related topics
Adding text with a background to images
To add text with a background to an image, use the following methods:
DrawTextBackColor
adds simple text with a background.DrawRotatedTextBackColor
adds simple text with a background at a specified clockwise angle (in degrees).
Both of these methods accept the same parameters as the DrawText
and DrawRotatedText
methods, respectively. They also accept the BackColor
parameter, which specifies the color of the background. This parameter is declared after the TextColor
parameter.
To add white text with a black background (semitransparent) rotated at a 90-degree angle, use the following method:
using GdPictureImaging gdpictureImaging = new GdPictureImaging();int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");// Set the font size unit to points.gdpictureImaging.FontSetUnit(UnitMode.UnitPoint);// Declare a `Color` object for the text font.Color textColor = Color.White;// Add the text to the image rotated at 90 degrees clockwise with a semitransparent black background.gdpictureImaging.DrawRotatedTextBackColor(imageID, 90, "GdPicture.NET", 100, 50, 40, FontStyle.Regular, textColor, gdpictureImaging.ARGB(100, 0, 0, 0), "Arial", true);gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\output.jpg");gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Set the font size unit to points. gdpictureImaging.FontSetUnit(UnitMode.UnitPoint) ' Declare a `Color` object for the text font. Dim textColor As Color = Color.White ' Add the text to the image rotated at 90 degrees clockwise with a semitransparent black background. gdpictureImaging.DrawRotatedTextBackColor(imageID, 90, "GdPicture.NET", 100, 50, 40, FontStyle.Regular, textColor, gdpictureImaging.ARGB(100, 0, 0, 0), "Arial", True) gdpictureImaging.SaveAsJPEG(imageID, "C:\temp\output.jpg") gdpictureImaging.ReleaseGdPictureImage(imageID)End Using
Used methods
Related topics
Adding text boxes to images
To add a text box to an image, use the DrawTextBox
method. It requires the parameters outlined below.
ImageID
— The GdPicture image ID of the loaded image.Text
— The body of the text box.Left
— The horizontal distance between the coordinate origin and the upper-left corner of the text box (in pixels).Top
— The vertical distance between the coordinate origin and the upper-left corner of the text box (in pixels).Width
— The width of the text box (in pixels).Height
— The height of the text box (in pixels).FontSize
— The font size in units specified by theFontSetUnit
method. The possible values are the following:UnitPoint
UnitInch
UnitPixel
UnitDocument
(1/300 inch)
Alignment
— The horizontal text alignment specified by theTextAlignment
enumeration. The possible values are the following:TextAlignmentCenter
aligns text to the center.TextAlignmentNear
aligns text to the left side in left-to-right writing systems. In right-to-left systems, it’s aligned to the right.TextAlignmentFar
aligns text to the right side in left-to-right writing systems. In right-to-left systems, it’s aligned to the left.
FontStyle
— The font size set by theFontStyle
enumeration. The possible values are the following:Regular
Bold
Italic
Underline
Strikeout
TextColor
— The text color specified by theARGB
method or theColor
object.FontName
— The name of the font used.DrawBox
— The Boolean value for drawing the borders of the text box.AntiAlias
— The Boolean value for applying the antialiasing algorithm.
To add a text box to an image, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging();int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");// Set the font size unit to points.gdpictureImaging.FontSetUnit(UnitMode.UnitPoint);// Add the text to the image rotated at 90 degrees clockwise.gdpictureImaging.DrawTextBox(imageID, "GdPicture.NET", 100, 50, 300, 200, 40, TextAlignment.TextAlignmentCenter, FontStyle.Regular, gdImage.ARGB(100, 128, 128, 128), "Arial", true, true);gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\output.jpg");gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Set the font size unit to points. gdpictureImaging.FontSetUnit(UnitMode.UnitPoint) ' Add the text to the image rotated at 90 degrees clockwise. gdpictureImaging.DrawTextBox(imageID, "GdPicture.NET", 100, 50, 300, 200, 40, TextAlignment.TextAlignmentCenter, FontStyle.Regular, gdImage.ARGB(100, 128, 128, 128), "Arial", True, True) gdpictureImaging.SaveAsJPEG(imageID, "C:\temp\output.jpg") gdpictureImaging.ReleaseGdPictureImage(imageID)End Using
Related topics