using System.Drawing; // Load the image var image = Image.FromFile("image.jpg"); // Create font and brush objects var font = new Font("Arial", 16); var brush = new SolidBrush(Color.Black); // Draw text onto the image using (var graphics = Graphics.FromImage(image)) { graphics.DrawString("Hello World", font, brush, new Point(10, 10)); } // Save the image with the text added image.Save("image_with_text.jpg");
using System.Drawing; using System.Drawing.Imaging; // Create a new bitmap image var bitmap = new Bitmap(200, 100); // Create font and brush objects var font = new Font("Calibri", 12); var brush = new SolidBrush(Color.Red); // Draw text onto the bitmap using (var graphics = Graphics.FromImage(bitmap)) { graphics.Clear(Color.White); graphics.DrawString("Welcome!", font, brush, new PointF(50, 30)); } // Save the bitmap as a PNG file var encoderParameters = new EncoderParameters(1); var encoderParameter = new EncoderParameter(Encoder.Quality, 100L); encoderParameters.Param[0] = encoderParameter; bitmap.Save("text_image.png", ImageFormat.Png);In this example, we create a new bitmap image with a width of 200 pixels and a height of 100 pixels. We then create font and brush objects, and use the DrawString() method to draw the text "Welcome!" onto the bitmap with a red color and a font size of 12. We then save the bitmap as a PNG file with a high quality setting. The package library for these examples is the System.Drawing assembly which is part of the .NET Framework.