C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Graphics »

Simple C# code to Add Watermark an Image


Posted Date: 21 Dec 2008    Resource Type: Code Snippets    Category: Graphics
Author: Prajith mpMember Level: Silver    
Rating: 1 out of 5Points: 10



using System;
using System.Drawing;
using System.Drawing.Drawing2D;

public class Watermark
{

//this fuction takes an Image and String for watermarking as argument
//and returns an Image with watermark

public Bitmap WaterMarkToImage(string ImagePath, string watermark)
{
Bitmap bmp;
bmp = new Bitmap(ImagePath);

Graphics graphicsObject;
int x, y;
try
{
//create graphics object from bitmap
graphicsObject = Graphics.FromImage(bmp);
}
catch (Exception e)
{

Bitmap bmpNew = new Bitmap(bmp.Width, bmp.Height);
graphicsObject = Graphics.FromImage(bmpNew);

graphicsObject.DrawImage(bmp, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);

bmp = bmpNew;
}

int startsize = (bmp.Width / watermark.Length);//get the font size with respect to length of the string

//x and y cordinates to draw a string
x = 0;
y = bmp.Height / 2;

//System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical); -> draws a vertical string for watermark

System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.NoWrap);

//drawing string on Image
graphicsObject.DrawString(watermark, new Font("Verdana", startsize, FontStyle.Bold), new SolidBrush(Color.FromArgb(60, 255, 255, 255)), x, y, drawFormat);

//return a water marked image
return (bmp);
}



}




Invoking the Above method in a Button Click in ASP.net

Watermark wM = new Watermark();
System.Drawing.Bitmap bmp = wM.WaterMarkToImage("c:/c.jpg", "www.asp.net");
bmp.Save("c:/c1.jpg");
Image1.ImageUrl = "c:/c1.jpg";



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Watermark Image in ASP.net  .  Watermark C# code  .  Watermark an Image  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Watermark an Image in C#.net / ASP.net
Previous Resource: Balls that bounces within a form
Return to Discussion Resource Index
Post New Resource
Category: Graphics


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use