Skip to content

Instantly share code, notes, and snippets.

@bogdanbujdea
Last active September 27, 2021 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bogdanbujdea/ea526cf4181b30c1bf9ed6d9a35e3134 to your computer and use it in GitHub Desktop.
Save bogdanbujdea/ea526cf4181b30c1bf9ed6d9a35e3134 to your computer and use it in GitHub Desktop.
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