Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Print with your own font using C#
To print with your own font in C#, you need to construct two essential objects: a FontFamily object and a Font object. The FontFamily object sets the typeface like Arial, Times New Roman, etc., whereas the Font object defines the size, style, and unit of measurement for the font.
Syntax
Following is the syntax for creating a FontFamily object −
FontFamily fontFamily = new FontFamily("FontName");
Following is the syntax for creating a Font object −
Font font = new Font(fontFamily, size, style, unit);
Parameters
The Font constructor accepts the following parameters −
- fontFamily − The FontFamily object that specifies the typeface
- size − The size of the font as a float value
- style − FontStyle enumeration (Bold, Italic, Regular, etc.)
- unit − GraphicsUnit enumeration (Pixel, Point, Inch, etc.)
Using Custom Font for Drawing Text
Here's how to create and use a custom Arial font for drawing text −
using System;
using System.Drawing;
using System.Drawing.Imaging;
class Program {
public static void Main() {
// Create a bitmap to draw on
Bitmap bitmap = new Bitmap(400, 200);
Graphics graphics = Graphics.FromImage(bitmap);
// Set background color
graphics.Clear(Color.White);
// Create FontFamily and Font objects
FontFamily myFontFamily = new FontFamily("Arial");
Font myFont = new Font(
myFontFamily,
20,
FontStyle.Bold,
GraphicsUnit.Pixel
);
// Create a brush for text color
SolidBrush brush = new SolidBrush(Color.Blue);
// Draw text using the custom font
graphics.DrawString("Hello, Custom Font!", myFont, brush, 50, 50);
// Save the image
bitmap.Save("output.png", ImageFormat.Png);
Console.WriteLine("Text drawn with custom font and saved as output.png");
// Clean up resources
myFont.Dispose();
brush.Dispose();
graphics.Dispose();
bitmap.Dispose();
}
}
The output of the above code is −
Text drawn with custom font and saved as output.png
Using Different Font Styles
You can create fonts with various styles using the FontStyle enumeration −
using System;
using System.Drawing;
using System.Drawing.Imaging;
class Program {
public static void Main() {
Bitmap bitmap = new Bitmap(500, 300);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.White);
FontFamily fontFamily = new FontFamily("Times New Roman");
SolidBrush brush = new SolidBrush(Color.Black);
// Regular font
Font regularFont = new Font(fontFamily, 14, FontStyle.Regular, GraphicsUnit.Point);
graphics.DrawString("Regular Text", regularFont, brush, 20, 20);
// Bold font
Font boldFont = new Font(fontFamily, 14, FontStyle.Bold, GraphicsUnit.Point);
graphics.DrawString("Bold Text", boldFont, brush, 20, 50);
// Italic font
Font italicFont = new Font(fontFamily, 14, FontStyle.Italic, GraphicsUnit.Point);
graphics.DrawString("Italic Text", italicFont, brush, 20, 80);
// Bold and Italic combined
Font boldItalicFont = new Font(fontFamily, 14, FontStyle.Bold | FontStyle.Italic, GraphicsUnit.Point);
graphics.DrawString("Bold Italic Text", boldItalicFont, brush, 20, 110);
bitmap.Save("font_styles.png", ImageFormat.Png);
Console.WriteLine("Different font styles saved as font_styles.png");
// Clean up
regularFont.Dispose();
boldFont.Dispose();
italicFont.Dispose();
boldItalicFont.Dispose();
brush.Dispose();
graphics.Dispose();
bitmap.Dispose();
}
}
The output of the above code is −
Different font styles saved as font_styles.png
Font Size Units
The GraphicsUnit enumeration provides different units for font sizing −
| GraphicsUnit | Description |
|---|---|
| Point | 1/72 of an inch (standard printing unit) |
| Pixel | Device-dependent pixel unit |
| Inch | Inch measurement |
| Millimeter | Millimeter measurement |
Conclusion
Creating custom fonts in C# involves using FontFamily to specify the typeface and Font to define size, style, and units. This approach gives you complete control over text appearance when drawing graphics or creating custom print layouts in your applications.
