<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d8211560\x26blogName\x3dTech+Tips,+Tricks+%26+Trivia\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dBLUE\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttp://mvark.blogspot.com/search\x26blogLocale\x3den\x26v\x3d2\x26homepageUrl\x3dhttp://mvark.blogspot.com/\x26vt\x3d-5147029996388199615', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>

Tech Tips, Tricks & Trivia

by 'Anil' Radhakrishna
An architect's notes, experiments, discoveries and annotated bookmarks.

Search from over a hundred HOW TO articles, Tips and Tricks


Code: Programmatically add text to an image

The System.Drawing namespace offers a great deal of functionality that could be achieved only with paid third party tools in ASP earlier.

Try the following code snippet. It assumes that you have the image and text with you already...

<%@ Page language="C#" ContentType="image/jpeg"%>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
void Page_Load(Object o, EventArgs e)
{
String FileName = "image1.jpg";
String Message = "Season's Greetings";
Color FillColor = Color.FromArgb(127,255,255,255);
SolidBrush FillBrush = new SolidBrush(FillColor);
Rectangle FillRectangle = new Rectangle(5,5,310,50);

Font TextFont = new Font("Comic Sans MS",20);
SolidBrush TextBrush = new SolidBrush(Color.Navy);
StringFormat TextFormat = new StringFormat();
TextFormat.Alignment = StringAlignment.Center;
TextFormat.LineAlignment = StringAlignment.Center;
System.Drawing.Image GreetingImage = System.Drawing.Image.FromFile(Server.MapPath("images") + "\\" + FileName);
Graphics DrawingSurface = Graphics.FromImage(GreetingImage);
DrawingSurface.FillRectangle(FillBrush, FillRectangle);
DrawingSurface.DrawString(Message, TextFont, TextBrush, FillRectangle, TextFormat);
Stream Output = Response.OutputStream;
GreetingImage.Save(Output,ImageFormat.Jpeg);
}
</script>


The above interesting code snippet is from a posting on the Hyderabad Dotnet User Group (which is a part of MUGH). The UG is a INETA member and currently has 2700 members! And I'm one among them.

Labels:

Tweet this | Google+ it | Share on FB

« Home | Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »

Posted by Blogger ganesh | 8/31/2007 10:23:00 AM

Hai,

The above code is working nice..but in my application i need to save the output in desktop.please let me know how to save the text with image in my system.

Thanking You.    



Posted by Anonymous Anonymous | 8/12/2010 12:45:00 PM

Excellent!
I used this code to allow Electricity consumers to view their actual bill online by writing data from a database on top of the scanned jpeg image of a bill.    



»

Post a Comment