Click here to Skip to main content
Click here to Skip to main content

Using Alpha in PNG to Make a Magic Card

By , 12 Oct 2007
Rate this:
Please Sign up or sign in to vote.
Screenshot - front-text.jpg
The image with white background.


Screenshot - back-text.jpg
The same image with black background.

Introduction

This is a funny tool. It will help you to generate a PNG file that has 2 layers, different layers have different text, so you can hide your message into the image, send it to your friend and ask her or him to guess.

Background

This idea was for the birthday card I sent to my girlfriend (now she's my wife). I wanted to DIY some gift for her, but you know, I'm an IT guy, hand made is hard for me, so I thought of designing an E-card. When I worked on Photoshop, I created 2 layers for black and white background, and put text on it. Fortunately, I found some interesting effects and finally I created a Web page with JavaScript effect to change the background color from white to black, and the image displayed in different text.

Recently, my wife asked me to do this again for her friend, so I finally wrote this program for this article.

Using the Code

There is one function to generate the image, the rest mostly is the UI and I will not focus on that.

The function is as given below:

private void GenerateImage()
{
    //get the graphics from image, image is created in the constructor
    Graphics g = Graphics.FromImage(m_Image);

    //if the image is not free, just clean and redraw
    g.Clear(Color.Empty);
    int area = m_Image.Width * m_Image.Height;
    float x = -1;
    float y = -1;
    int alpha = -1;
    int size = -1;
    SolidBrush brush = new SolidBrush(this.ForeColor);
    PointF position = new PointF(m_Image.Width / 2, m_Image.Height / 2);
    Random rand = new Random();
    StringFormat format = new StringFormat();
    format.Alignment = StringAlignment.Near;
    format.LineAlignment = StringAlignment.Near;

    //set the back text color as white
    brush.Color = Color.FromArgb(150, Color.White);
    g.DrawString(m_BackText, this.Font, brush, position, format);

    //draw some star at the background
    for (int i = 0; i < area * BACK_STAR_AREA; i++)
    {
        x = rand.Next(m_Image.Width);
        y = rand.Next(m_Image.Height);
        alpha = rand.Next(MIN_STAR_ALPHA, MAX_STAR_ALPHA);
        size = rand.Next(MIN_STAR_SIZE, MAX_STAR_SIZE);
        brush.Color = Color.FromArgb(alpha, Color.LightYellow);
        g.FillEllipse(brush, x - size / 2, y - size / 2, size, size);
    }

    //set the front text as black
    brush.Color = Color.FromArgb(180, Color.Black);
    g.DrawString(m_FrontText, this.Font, brush, position, format);

    //draw some star at the background
    for (int i = 0; i < area * FRONT_STAR_AREA; i++)
    {
        x = rand.Next(m_Image.Width);
        y = rand.Next(m_Image.Height);
        alpha = rand.Next(MIN_STAR_ALPHA, MAX_STAR_ALPHA);
        size = rand.Next(MIN_STAR_SIZE, MAX_STAR_SIZE);
        brush.Color =Color.FromArgb(alpha, Color.Black);
        g.FillEllipse(brush, x - size / 2, y - size / 2, size, size);
    }

    //dispose and cleanup
    brush.Dispose();
}

Points of Interest

32 bit alpha image is very interesting, to get a better idea you can work on it. A static image with dynamic effect becomes a different image. Now you don't need to prepare N images and JavaScript effect for changing the image. Just change the background and get a different visual effect. Work on it now and give a surprise to your friend.

Web Browsers

Web browsers supporting 32 bit PNG: Internet Explorer 7, Firefox, Safari.
Internet Explorer 6 or earlier versions are not supported - the user will see the background color is changing and the image remains the same.

History

  • 12th October, 2007: Article created

License

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

About the Author

Jacky Yiu
Architect MouxIdea Limited
Hong Kong Hong Kong
1981 Born in Hong Kong
1996 Become Badminton Trainer
1997 Hong Kong's Return to China
1998 The Year After Hong Kong's Return to China
1999 The Year Before Millennium
2000 First touch of programming - ASP(guestbook)
2001 Outstanding Student Award - Computing Department
2002 Xcellence Developer Awards - Best Graphical Focused Application(Game) Award
2003 Microsoft MVP - .NET
2004 Be lost in Technology
2005 Microsoft MVP - C#
2006 Microsoft MVP - C#
2007 Getting Marry - Cheers~
2008 Microsoft MVP - C#
2009 Microsoft MVP - C#
2010 Microsoft MVP - C#
2011 Start my software hut
 
http://www.csharpfans.com


http://www.bugyiu.com


http://www.mouxidea.com

Comments and Discussions

 
GeneralReally nice PinmemberBigdeak10-Jun-10 2:47 
Generalcongratulate Pinmemberyufb125-Feb-10 18:02 
QuestionConnection with Visual Cryptography ? Pinmembertectures19-Oct-07 5:42 
AnswerRe: Connection with Visual Cryptography ? PinmemberJacky Yiu20-Oct-07 8:21 
GeneralPretty cool PinmemberSteve Hansen12-Oct-07 5:31 
GeneralRe: Pretty cool PinmemberJacky Yiu12-Oct-07 5:35 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140421.2 | Last Updated 12 Oct 2007
Article Copyright 2007 by Jacky Yiu
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid