Last active
September 27, 2021 20:23
-
-
Save bogdanbujdea/ea526cf4181b30c1bf9ed6d9a35e3134 to your computer and use it in GitHub Desktop.
C# Console application that writes a text onto an image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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