Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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.

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

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


Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:
asp.net ,c#

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

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}