GetTextWidth Method (GdPictureImaging)
In This Topic
Calculate the width of the specified text, based on the specified font, font size and font style. The
result is measured in units specified by the FontSetUnit() method.
Syntax
'Declaration
Public Function GetTextWidth( _
ByVal As Integer, _
ByVal As String, _
ByVal As String, _
ByVal As Single, _
ByVal As GdPictureFontStyle _
) As Single
public float GetTextWidth(
int ,
string ,
string ,
float ,
GdPictureFontStyle
)
public function GetTextWidth(
: Integer;
: String;
: String;
: Single;
: GdPictureFontStyle
): Single;
public function GetTextWidth(
: int,
: String,
: String,
: float,
: GdPictureFontStyle
) : float;
public: float GetTextWidth(
int ,
string* ,
string* ,
float ,
GdPictureFontStyle
)
public:
float GetTextWidth(
int ,
String^ ,
String^ ,
float ,
GdPictureFontStyle
)
Parameters
- ImageID
- GdPicture image identifier.
- Text
- Text to draw.
- FontName
- The name of the font. IE: "Arial".
- FontSize
- The font size in units specified by the FontSetUnit() method.
- FontStyle
- A member of the FontStyle enumeration.
Return Value
The width of the text measured in units specified by the FontSetUnit() method.
Example
Drawing the red text and the black text border based on the text width and height on jpeg image.
using (GdPictureImaging gdpictureImaging = new GdPictureImaging())
{
int imageID = gdpictureImaging.CreateGdPictureImageFromFile("input.jpg");
string text = "GdPicture.PDF";
// Set font unit to Pixel
gdpictureImaging.FontSetUnit(UnitMode.UnitPixel);
float width = gdpictureImaging.GetTextWidth(imageID, text, "Arial", 24, GdPicture14.FontStyle.FontStyleRegular);
float height = gdpictureImaging.GetTextHeight(imageID, text, "Arial", 24, GdPicture14.FontStyle.FontStyleRegular);
// Draw the text.
gdpictureImaging.DrawText(imageID, text, 10, 10, 24, GdPicture14.FontStyle.FontStyleRegular, Color.Red, "Arial", true);
// Draw the border rectangle.
gdpictureImaging.DrawRectangle(imageID, 10, 10, (int)width, (int)height, 1, Color.Black, true);
gdpictureImaging.SaveAsJPEG(imageID, "output.jpg");
gdpictureImaging.ReleaseGdPictureImage(imageID);
}
Example
Drawing the red text and the black text border based on the text width and height on jpeg image.
using (GdPictureImaging gdpictureImaging = new GdPictureImaging())
{
int imageID = gdpictureImaging.CreateGdPictureImageFromFile("input.jpg");
string text = "GdPicture.PDF";
// Set font unit to Pixel
gdpictureImaging.FontSetUnit(UnitMode.UnitPixel);
float width = gdpictureImaging.GetTextWidth(imageID, text, "Arial", 24, GdPicture14.FontStyle.FontStyleRegular);
float height = gdpictureImaging.GetTextHeight(imageID, text, "Arial", 24, GdPicture14.FontStyle.FontStyleRegular);
// Draw the text.
gdpictureImaging.DrawText(imageID, text, 10, 10, 24, GdPicture14.FontStyle.FontStyleRegular, Color.Red, "Arial", true);
// Draw the border rectangle.
gdpictureImaging.DrawRectangle(imageID, 10, 10, (int)width, (int)height, 1, Color.Black, true);
gdpictureImaging.SaveAsJPEG(imageID, "output.jpg");
gdpictureImaging.ReleaseGdPictureImage(imageID);
}
See Also