You can read all the details at: Add, Insert, or Draw Text on PNG, JPEG, TIFF, Icon, GIF Image using C#
Last active
December 23, 2021 07:22
-
-
Save aspose-com-gists/b07fbc1311415962779e252ddb7437d8 to your computer and use it in GitHub Desktop.
Add, Insert, or Draw Text on PNG, JPEG, TIFF, Icon, GIF Images or Pictures using C# | Watermark Text 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
// Load existing input image | |
Bitmap bitmap = new Bitmap(dataDir + @"Test.png"); | |
// Initialize Graphics class object | |
Graphics graphics = Graphics.FromImage(bitmap); | |
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; | |
Brush brush = new SolidBrush(Color.FromKnownColor(KnownColor.Black)); | |
// Declare brush while specifying color | |
Pen pen = new Pen(Color.FromKnownColor(KnownColor.Blue), 1); | |
// Set text font | |
Font arial = new Font("Arial", 20, FontStyle.Regular); | |
// Set text | |
string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sapien tellus, mattis et condimentum eget, commodo ut ipsum. Maecenas elit sapien, tempus sit amet mauris sit amet, hendrerit laoreet nisi. Nulla facilisi. Sed commodo, mauris eget porta commodo, nunc tellus volutpat mi, eu auctor diam libero vel neque. Vestibulum nec mattis dui, nec molestie nisl. Etiam in magna felis. Praesent non nulla tortor. Integer nec convallis purus. Fusce vitae mollis mauris. Cras efficitur dui at mi viverra scelerisque. Morbi quis magna elit. Nulla facilisis id ante sit amet fringilla. Sed iaculis consectetur lectus a interdum. Etiam ut sollicitudin lectus, et congue lectus."; | |
Rectangle rectangle = new Rectangle(100, 100, 800, 600); | |
graphics.DrawRectangle(pen, rectangle); | |
// Draw text | |
graphics.DrawString(text, arial, brush, rectangle); | |
// Save output image | |
bitmap.Save(RunExamples.GetDataDir() + @"TextFonts\DrawText_out.png"); |
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
// Initialize new image from scratch | |
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); | |
Graphics graphics = Graphics.FromImage(bitmap); | |
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; | |
graphics.Clear(Color.FromKnownColor(KnownColor.White)); | |
// Initialize Brush class object | |
Brush brush = new SolidBrush(Color.FromKnownColor(KnownColor.Black)); | |
Pen pen = new Pen(Color.FromKnownColor(KnownColor.Blue), 1); | |
// Set font style, size, etc. | |
Font arial = new Font("Arial", 20, FontStyle.Regular); | |
string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sapien tellus, mattis et condimentum eget, commodo ut ipsum. Maecenas elit sapien, tempus sit amet mauris sit amet, hendrerit laoreet nisi. Nulla facilisi. Sed commodo, mauris eget porta commodo, nunc tellus volutpat mi, eu auctor diam libero vel neque. Vestibulum nec mattis dui, nec molestie nisl. Etiam in magna felis. Praesent non nulla tortor. Integer nec convallis purus. Fusce vitae mollis mauris. Cras efficitur dui at mi viverra scelerisque. Morbi quis magna elit. Nulla facilisis id ante sit amet fringilla. Sed iaculis consectetur lectus a interdum. Etiam ut sollicitudin lectus, et congue lectus."; | |
Rectangle rectangle = new Rectangle(100, 100, 800, 600); | |
graphics.DrawRectangle(pen, rectangle); | |
// Draw text on the image | |
graphics.DrawString(text, arial, brush, rectangle); | |
// Save output image | |
bitmap.Save(RunExamples.GetDataDir() + @"TextFonts\DrawText_out.png"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment