Showing posts with label text on image. Show all posts
Showing posts with label text on image. Show all posts

Friday, January 1, 2016

Write Dynamically Text on Image in ASP.NET C#

In this article, I am going to explain how to write some text on a image in ASP. net c#.

Follow given below step to write text on an image in asp.net c#

Step 1

Go to Menu File->New-Project


Step 2

After clicking on project a new window will be open

select ASP.NET Web Application and write the name "DynamicTextOnImage" and click on "OK" button.


Code that write text on image

string name = "Dear Reader,";
 
                Bitmap bitMapImage = new System.Drawing.Bitmap(Server.MapPath(@"happynewyear.jpg"));
                Graphics graphicImage = Graphics.FromImage(bitMapImage);
 
                Color colorStringColor = System.Drawing.ColorTranslator.FromHtml("#ffffff");
                graphicImage.DrawString(name, new Font("Arial", 15, FontStyle.Bold), new SolidBrush(colorStringColor), new Point(100, 8));
 
                string stringOutPutFileName = Server.MapPath("Image\\happynewyear.jpg");
 
                using (MemoryStream memory = new MemoryStream())
                {
                    using (FileStream fs = new FileStream(stringOutPutFileName, FileMode.Create, FileAccess.ReadWrite))
                    {
                        bitMapImage.Save(memory, ImageFormat.Jpeg);
                        byte[] bytes = memory.ToArray();
                        fs.Write(bytes, 0, bytes.Length);
                    }
                }
                graphicImage.Dispose();
                bitMapImage.Dispose();
                bitMapImage.Dispose();

aspx.cs code

using System;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
 
namespace DynamicTextOnImage
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
            try
            {
                string name = "Dear Reader,";
 
                Bitmap bitMapImage = new System.Drawing.Bitmap(Server.MapPath(@"happynewyear.jpg"));
                Graphics graphicImage = Graphics.FromImage(bitMapImage);
 
                Color colorStringColor = System.Drawing.ColorTranslator.FromHtml("#ffffff");
                graphicImage.DrawString(name, new Font("Arial", 15, FontStyle.Bold), new SolidBrush(colorStringColor), new Point(100, 8));
 
                string stringOutPutFileName = Server.MapPath("Image\\happynewyear.jpg");
 
                using (MemoryStream memory = new MemoryStream())
                {
                    using (FileStream fs = new FileStream(stringOutPutFileName, FileMode.Create, FileAccess.ReadWrite))
                    {
                        bitMapImage.Save(memory, ImageFormat.Jpeg);
                        byte[] bytes = memory.ToArray();
                        fs.Write(bytes, 0, bytes.Length);
                    }
                }
                graphicImage.Dispose();
                bitMapImage.Dispose();
                bitMapImage.Dispose();
            }
            catch (Exception ex)
            {
 
            }
        }
    }
}
 
Image before writing text on image
 
image source
 


After writing text on image