• Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
    • Tips
    • QA’s
    • Misc
  • Course
  • Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
    • Tips
    • QA’s
    • Misc
  • Course
  • #News
  • #APPS
  • #Apple WWDC
  • #Google I/O
  • #Microsoft Ignite
  • #Let’s Talk
  • #Advertise

MyCodeTips mycodetips-newlogocopy1

  • Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
    • Tips
    • QA’s
    • Misc
  • Course
.Net

How to add watermark to an image in c#

watermark to an image

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”;

  • Click to share on Reddit (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • More
  • Click to share on Pocket (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
Written by Ranjan - June 8, 2013 - 2289 Views
Tags | asp.net, C#
AUTHOR
Ranjan

This website is basically about of what we learnt from my years of experience as a software engineer on software development specifically on mobile application development, design patterns/architectures and its changing scenarios, security, troubleshooting, tools, tips&tricks and many more.

You Might Also Like

mycodetips dotnet

Tips to Bind Dropdownlist in Asp.net

December 2, 2013
csharp-vb-net

Dictionary, How to read-write data in using C# and VB.net

June 6, 2013
Next Post
Previous Post

Support us

Subscribe for updates

Join 8,278 other subscribers

Latest Posts

  • Exploring Single Point Failure
    Exploring Single Point Failures: Causes and Impacts
  • primitive-datatypes-new
    Exploring the Pros and Cons of Primitive Data Types
  • best practice clean code
    Essential Coding Standards and Best Practices for Clean Code
  • YT-Featured-Templates--lld
    What Business Problems Solves in Low Level Design (LLD)
  • SRP-SingleResopnsibility
    SRP : Single Responsibility Principle in Swift and Objective-C
whiteboard

Whiteboard(PRO)

whiteboard

Whiteboard(lite)

alphabets

Kids Alphabet

do2day

Do2Day

  • #about
  • #myapps
  • #contact
  • #privacy
  • #Advertise
  • #myQuestions

Android Database Interview IOS IOSQuestions Javascript Objective-c Programming Swift Tips&Tricks Web Wordpress

  • Exploring Single Point Failures: Causes and Impacts
  • Exploring the Pros and Cons of Primitive Data Types
  • Essential Coding Standards and Best Practices for Clean Code
MyCodeTips

©mycodetips.com

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.