Skip to content

Instantly share code, notes, and snippets.

@thewindev
Last active Sep 27, 2021
Embed
What would you like to do?
C# Console application that writes a text onto an image
using System.Drawing; //install the System.Drawing.Common Nuget package
using System.IO;
namespace TestApp
{
class Program
{
static void Main(string[] args)
{
var originalImagePath = "d:\\banner.png";
var dNewBannerPng = "d:\\new-banner.png";
var textToWrite = "100.000 views";
UpdateImage(originalImagePath, dNewBannerPng, textToWrite);
}
private static void UpdateImage(string originalImagePath, string newImagePath, string text)
{
using var ms = new MemoryStream();
using Bitmap bitmap = new Bitmap(Image.FromFile(originalImagePath));
using Graphics graphics = Graphics.FromImage(bitmap);
using (Font font = new Font(new FontFamily("Segoe UI"), 32, FontStyle.Bold, GraphicsUnit.Pixel))
{
graphics.DrawString(text, font, new SolidBrush(Color.White), 995, 295);
}
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
File.WriteAllBytes(newImagePath, ms.ToArray());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment