using System;
using System.Web;
using System.Drawing;
public class WriteTextAsImage : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
string textToWrite = context.Request.QueryString["text"].ToString();
textToWrite = utilityFunctions.Decrypt(textToWrite); // decrypt the string
if (textToWrite.Trim().Length > 0)
{
Bitmap image = new Bitmap(400, 30);
Graphics g = null;
try
{
g = Graphics.FromImage(image);
Font f = new Font("Arial", 10, FontStyle.Regular);
SolidBrush b = new SolidBrush(Color.White);
g.FillRectangle(b, 0, 0, 400, 30);
g.DrawString(textToWrite, f, Brushes.Blue, 2, 5);
f.Dispose();
image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ee)
{
context.Response.Write(ee.Message.ToString());
}
finally
{
image.Dispose();
g.Dispose();
}
}
}
public bool IsReusable {
get {
return true;
}
}
}
Let me explain the above code snippets in the in the simple steps.
Use the System.Drawing namespace.
In the ProcessRequest method, as we are going to create an image with the help of this handler file, set the ContextType as "image/jepg". Content type is nothing but the HTTP MIME type, for more details about several MIME types see this article http://msdn.microsoft.com/en-us/library/ms775147.aspx
Get the text to write as as image from the querystring (text).
Decrypt it (because we shall pass the encrypted string to this page so decrypt it to get the actual string)
Create a new bitmap image using BitMap object.
Create a graphic object from the bitmap.
Create Font object with the desired style in which we want to write text.
By default, the BitMap object is created as black backgroud, so lets make it white. To do that create SolidBrush object and fill the graphic object with the brush using FillRectangle method.
Finally draw the text on the graphic by calling DrawString method and passing text to write, font, color and other padding parameters.
Till now we have the graphic object, now save it as the OutputStream. To do that call the save method of the bitmap object and pass stream and image format as parameter.
We should never forget to dispose the object we have instantiated while creating the object, so dispose them using Dispose method of respective objects.
IsReusable method indicates whether another request can use the IHttpHandler instance. I am returning true so that other requests can also use this instance of the IHttpHandler.
Function to encrypt and decrypt the string. Following functions to encrypt and decrypt string is simple, you can use more complex encryption and decryption mechanism in real application to get more security of the string you pass.
public class utilityFunctions
{
// simply encrypt the string
public static string Encrypt(string text)
{
byte[] bytes = Encoding.ASCII.GetBytes(text);
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = Convert.ToByte(Convert.ToInt64(bytes[i]) + 5);
}
return Encoding.ASCII.GetString(bytes);
}
// simple decrypt the string
public static string Decrypt(string text)
{
byte[] bytes = Encoding.ASCII.GetBytes(text);
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = Convert.ToByte(Convert.ToInt64(bytes[i]) - 5);
}
return Encoding.ASCII.GetString(bytes);
}
}
Using .ashx file
So our .ashx file are ready to be used to create dynamic image provided it is passed a querystring name "text". Now, lets see how to use this file.
<img alt="Contact Details" title="Contact Details"
src="/WriteTextAsImage.ashx?text=<%= utilityFunctions.Encrypt("yourname@myfunda.net") %>" />
The actual img tag will appear like following in the page source.
<img alt="Conact Details" title="Conact Details"
src="/WriteTextAsImage.ashx?text=~tzwsfrjEr~kzsif3sjy" />
To use this file, we need to simply specify the src attribute of img element and pass the text(encrypted) to write as its querystring. If you followed the exact step and have implemented above code, your image should appear like below in the browser.
Hope this article will help you to avoid spam by creating email id and other information as image. Thank you for reading.