web 2.0

Useful Image WaterMark Extension Method

Hi !

Today i'll give you a useful Extension Method in order to Add some Text to Image that can be used in HttpHandler to display copyrighted images from Database for a Photo Store for example.

WaterMarkImageHandler.ashx

Here is the Extension Code

public static Bitmap WaterMarkImage(this Bitmap bmp, String Text)
{
    Image img = new Bitmap(bmp, new Size(bmp.Width, bmp.Height));
    Bitmap tmp = new Bitmap(img);
    using (Graphics graphic = Graphics.FromImage(tmp))
    {
        SolidBrush brush = new SolidBrush(Color.FromArgb(120, 255, 255, 255));
        Font font = new Font("Arial", 18, FontStyle.Bold);
        graphic.DrawString(Text, font, brush, new PointF(10, 30));
    }
    return tmp;
}

Here is a Sample of Usage

public class WaterMarkImageHandler : 
IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "image/jpeg";
        Image img = Bitmap.FromFile(context.Server.MapPath("~/03SC008.JPG"));
        Bitmap bmp = new Bitmap(img);
        bmp = bmp.WaterMarkImage("SB2 DEVELOPPERS BLOG TEST");
        bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);
    }

    public bool IsReusable
    {
        get
        {
            return true;
        }
    }
}

Hope this Help's !

Download Solution - WaterMarkImageHandler.zip



Views(662)

kick it on DotNetKicks.com

Share/Save/Bookmark Subscribe

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

ASP.NET | C#

Comments

Add comment


 

biuquote
Loading



Technorati Profile