DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Role of C# in Building Dynamic and Secure Web Applications
  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Zero to AI Hero, Part 3: Unleashing the Power of Agents in Semantic Kernel

Trending

  • Simplify Authorization in Ruby on Rails With the Power of Pundit Gem
  • Chaos Engineering for Microservices
  • Scaling InfluxDB for High-Volume Reporting With Continuous Queries (CQs)
  • SQL Server Index Optimization Strategies: Best Practices with Ola Hallengren’s Scripts
  1. DZone
  2. Coding
  3. Frameworks
  4. Picture That: How to Write Text on an Image Using ASP.NET & C#

Picture That: How to Write Text on an Image Using ASP.NET & C#

You can write text on an image with ASP.NET and C#. Learn how.

By 
Rajeesh Menoth user avatar
Rajeesh Menoth
·
Sep. 26, 15 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
26.4K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

This article explains how to Write a Text On Image Using Asp.Net & C#. In this article i am using some common library functions and methods.

Image Class & Bitmap Class

  • The Image class is one of the example of Abstract Class
  • The Bitmap class is an implementation of the Image class and it inherits from the Abstract Image class
  • After implementation we can get the Image Class functionality in Bitmap Class.

C# Code

The following code is using for Write a Text On Image. We can change the below functionalities based on our requirement.

//creating a image object
System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromFile(Server.MapPath("onam.jpg")); // set image 
//draw the image object using a Graphics object
Graphics graphicsImage = Graphics.FromImage(bitmap);

//Set the alignment based on the coordinates   
StringFormat stringformat = new StringFormat();
stringformat.Alignment = StringAlignment.Far;
stringformat.LineAlignment = StringAlignment.Far;

StringFormat stringformat2 = new StringFormat();
stringformat2.Alignment = StringAlignment.Center;
stringformat2.LineAlignment = StringAlignment.Center;

//Set the font color/format/size etc..  
Color StringColor = System.Drawing.ColorTranslator.FromHtml("#933eea");//direct color adding
Color StringColor2 = System.Drawing.ColorTranslator.FromHtml("#e80c88");//customise color adding
string Str_TextOnImage = "Happy";//Your Text On Image
string Str_TextOnImage2 = "Onam";//Your Text On Image

graphicsImage.DrawString(Str_TextOnImage, new Font("arial", 40,
FontStyle.Regular), new SolidBrush(StringColor), new Point(268, 245),
stringformat); Response.ContentType = "image/jpeg";

graphicsImage.DrawString(Str_TextOnImage2, new Font("Edwardian Script ITC", 111,
FontStyle.Bold), new SolidBrush(StringColor2), new Point(145, 255),
stringformat2); Response.ContentType = "image/jpeg";

bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);

Graphics & Bitmap

A Bitmap is an object used to work with images defined by pixel data and you can draw the image object using a Graphics object. Check the below code.

//creating a image object
System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromFile(Server.MapPath("onam.jpg")); // set image 
//draw the image object using a Graphics object
Graphics graphicsImage = Graphics.FromImage(bitmap);

Formatting the string

Change Text Alignment based on the coordinates.

//Set the alignment based on the coordinates   
StringFormat stringformat = new StringFormat();
stringformat.Alignment = StringAlignment.Far;
stringformat.LineAlignment = StringAlignment.Far;

StringFormat stringformat2 = new StringFormat();
stringformat2.Alignment = StringAlignment.Center;
stringformat2.LineAlignment = StringAlignment.Center;

Text On Image

Set the font color,size,format.

string Str_TextOnImage = "Happy";//Your Text On Image
string Str_TextOnImage2 = "Onam";//Your Text On Image

graphicsImage.DrawString(Str_TextOnImage, new Font("arial", 40,
FontStyle.Regular), new SolidBrush(StringColor), new Point(268, 245),
stringformat); Response.ContentType = "image/jpeg";

graphicsImage.DrawString(Str_TextOnImage2, new Font("Edwardian Script ITC", 111,
FontStyle.Bold), new SolidBrush(StringColor2), new Point(145, 255),
stringformat2); Response.ContentType = "image/jpeg";

String Color

Adding the string color in two way...

Color StringColor = System.Drawing.Color.Red;//direct color adding  
Color StringColor = System.Drawing.ColorTranslator.FromHtml("#933eea");//customise color adding 

Important Section

Namespace

The following namespace is contain Graphics, Bitmaps, Image Editing, and Alignment, etc libraries.

using System.Drawing;
using System.Drawing.Imaging;

OutPut

Write Text On Image


csharp ASP.NET

Published at DZone with permission of Rajeesh Menoth, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Role of C# in Building Dynamic and Secure Web Applications
  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Zero to AI Hero, Part 3: Unleashing the Power of Agents in Semantic Kernel

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!