Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C#

Generating email address images to avoid spam on your site

Rate me:
Please Sign up or sign in to vote.
4.60/5 (5 votes)
29 Jan 2011CPOL1 min read 16.3K   9   9
Generating email address images to avoid spam on your site

Nowadays, you can't publish an email address anywhere without receiving a bunch of spam immediately after. Those spam web crawlers just search in every site looking for everything and anything that resembles an email address.

I very often see people use different variations of writing their email like "myname at mydomain.com" or "myname at mydomain dot com".
I don't think this covers it anymore...
The best way, in my opinion, is to create an image of the email address instead of writing it. (That's what Facebook does as well.)

I started working on a user based site, and the users' emails will be displayed throughout the site. So I created a class that will dynamically generate email address images for me so my precious users won't be so vulnerable to all the spam out there.

Here it is:

C#
// Members //
private int _emailFontSize = 8;
private string _emailFontFamily = "Verdana";
private Brush _emailBackgroundColor = Brushes.White;
private Brush _emailFontColor = Brushes.Navy;
 
// Properties //
// I cut this out, just for convenience //
 
// Methods //
public void CreateEmailImage(string email)
{
      // create the font object
      Font myFont = new Font(_emailFontFamily, _emailFontSize);
             
      // create the image object
      Bitmap emailImage = new Bitmap((int)(myFont.SizeInPoints * email.Length), 
			myFont.Height);
 
      Graphics imgGraphics = Graphics.FromImage((Image)emailImage);
      imgGraphics.FillRectangle(_emailBackgroundColor, 
		new Rectangle(new Point(0, 0), emailImage.Size));
      imgGraphics.DrawString(email, myFont, _emailFontColor, new PointF(0, 0));
             
      // measure the actual size of the email string
      SizeF stringSize = imgGraphics.MeasureString(email, myFont);
             
      // crop the image we created
      Bitmap finalImage = CropImage(emailImage, 
		new Rectangle(new Point(0, 0), new Size((int)stringSize.Width,
                                    (int)stringSize.Height)));
 
      // save the image to a local file
      finalImage.Save(email + ".gif", ImageFormat.Gif);
  }
 
  private Bitmap CropImage(Image imgCrop, Rectangle cropArea)
  {
      Bitmap bmpImage = new Bitmap(cropArea.Width, cropArea.Height - 1);
      Graphics bmpG = Graphics.FromImage(bmpImage);
      bmpG.DrawImage(imgCrop, new Point(0, 0));
      return bmpImage;
  }
} 

I will probably make some changes before using this on my site:

  1. I don't think it's smart to give the file the name of the actual email address. I'll probably generate some unique ID but built from the users name.
  2. I haven't decided yet if I want the image to be a link to contact the user. If so, then I must create a unique id for each user, and have the link lead to that page with the unique id so I can identify the user being contacted.
  3. I'll probably add a simple caching mechanism to this, hence create a folder to store all the images, and then check if it exists before creating a new one each time.

Enjoy! :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Israel Israel
Started programming e-commerce sites with PHP & MySQL at the age of 14. Worked for me well for about 5 years.

Transfered to C# & asp.net, while serving in the IDF.
Worked on the 'Core Performance' Team at ShopYourWay.com (Sears Israel)
Currently working at Logz.io

Check out my blog!
or my twitter

Comments and Discussions

 
QuestionTwice the usage of memory on both db and server Pin
Pavan Navule7-Oct-12 21:55
Pavan Navule7-Oct-12 21:55 
AnswerRe: Twice the usage of memory on both db and server Pin
Gilly Barr2-Feb-13 7:11
Gilly Barr2-Feb-13 7:11 
QuestionNice work Pin
Umair Feroze1-Feb-11 22:32
Umair Feroze1-Feb-11 22:32 
AnswerRe: Nice work Pin
Gilly Barr9-Feb-11 9:40
Gilly Barr9-Feb-11 9:40 
GeneralNot new!!! Pin
shakil030400331-Jan-11 22:06
shakil030400331-Jan-11 22:06 
GeneralRe: Not new!!! Pin
Gilly Barr1-Feb-11 12:07
Gilly Barr1-Feb-11 12:07 
GeneralNice idea Pin
Lardus du Plessis31-Jan-11 18:21
Lardus du Plessis31-Jan-11 18:21 
GeneralRe: Nice idea Pin
Gilly Barr1-Feb-11 12:03
Gilly Barr1-Feb-11 12:03 
Thanks! Smile | :)
GeneralRe: Nice idea Pin
Gilly Barr9-Feb-11 9:38
Gilly Barr9-Feb-11 9:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.