Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / ASM
Tip/Trick

How to Create watermarked images in C# ASP.NET ?

Rate me:
Please Sign up or sign in to vote.
4.57/5 (6 votes)
3 Feb 2012CPOL 48K   17   2
How to Create watermarked images in C# ASP.NET ?
The below code will create a watermark on image using C#.

C#
//Use NameSpace
using System.Drawing;


C#
//Execute below code at the event, where u wants Water Marking. 
{ 
           // Create a bitmap object of the Image, Here I am taking image from File Control "FileTest" 
            Bitmap bmp = new Bitmap(FileTest.PostedFile.InputStream); 
            Graphics canvas = Graphics.FromImage(bmp); 
            try 
            { 
                Bitmap bmpNew = new Bitmap(bmp.Width, bmp.Height); 
                canvas = Graphics.FromImage(bmpNew); 
                canvas.DrawImage(bmp, new Rectangle(0, 0, 
bmpNew.Width, bmpNew.Height), 0, 0, bmp.Width, bmp.Height, 
GraphicsUnit.Pixel); 
                bmp = bmpNew; 
            } 
            catch(Exception ee) // Catch exceptions 
            { 
                Response.Write(ee.Message); 
            } 
            // Here replace "Text" with your text and you also can assign Font Family, Color, Position Of Text etc. 
            canvas.DrawString("Text", new Font("Verdana", 14, 
FontStyle.Bold), new SolidBrush(Color.Beige), (bmp.Width / 2), 
(bmp.Height / 2)); 
            // Save or display the image where you want. 
            bmp.Save(System.Web.HttpContext.Current.Server.MapPath("~/ 
WaterMarkImages/") + 
FileTest.PostedFile.FileName, 
System.Drawing.Imaging.ImageFormat.Jpeg); 
} 


You will find a great solution. :-D

License

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



Comments and Discussions

 
QuestionImage is corrupted when that image is dowloaded. Pin
Md. Ikram Shams30-Jun-13 1:07
Md. Ikram Shams30-Jun-13 1:07 
AnswerRe: Image is corrupted when that image is dowloaded. Pin
jorgeaikido20-Aug-15 6:27
jorgeaikido20-Aug-15 6:27 

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.