Link to home
Start Free TrialLog in
Avatar of meetpd
meetpdFlag for India

asked on

How to superimpose text on an image (ASP.NET C#)

Hi,

I have a background image on which I want to superimpose some text...how do I do it?

I know one way of doing it is writing the text in some image editor and save the image.

BUT, I dont want to do it that way. I want to show text programatically on the image.

See the attached image, the fonts are getting distorted when we write text in image editor and save. That's the reason I want to put text using code.
Thanks!
BlueBG.jpg
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

taken from http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-how-to-draw-text-on-an-image

here's a code snippet:
Bitmap myBitmap = new Bitmap("C:\\myImage.jpg");
Graphics g = Graphics.FromImage(myBitmap);
g.DrawString("My\nText", new Font("Tahoma", 40), Brushes.White, new PointF(0, 0));

Open in new window

output-1.jpg
u can play around with DrawString, changing fonts, colors, draw vertical text etc.
Avatar of meetpd

ASKER

Thanks for the quick reply.

I did it on the load event of the image but somehow it did not work.

What am I doing wrong?
protected void EmpBG(object sender, EventArgs e)
        {
            Bitmap myBitmap = new Bitmap("E:\\Images\\BlueBG.jpg");
            Graphics g = Graphics.FromImage(myBitmap);
            g.DrawString("My Text", new Font("Tahoma",20), Brushes.White, new PointF(5,5));

        }

Open in new window

ru using image as part of asp.net resource?
would u like to do it using CSS
Avatar of aholl
aholl

1st, Remember that code portion is run on the server side not the client side, 2nd, you have now burn text on to your image but you dint saved it localy and send it to the client...

If you want to make this work, you will have to write text to your original image and then save it to the new modified image in the page_load of your .cs (code page) file. remember to reference the same image path into your asp document.

Avatar of meetpd

ASKER

How can we do it using CSS?
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
and here's the before and after screenshots.
output-1.jpg
output-2.jpg
ok so, everything is ok now??
Hi The above posts are quite good.
But if you want to show different Text on on an Image on ASP.NET depending upon a drop down selection then see the below post.

http://www.freejobsreference.com/dotnetforums/47-How-to-superImpose-or-Overlay-Different-Text-on-Images-in-ASP-NET.aspx