5

I have seen great questions and answers regarding adding watermark on images with php

I would like to do the same, this time with ASP.NET

So here are a couple of questions.

  1. How can i do that with ASP?
  2. Is this process going to be a great overload for the server?
  3. Could i use an image for a watermark instead of simple text?
Community
  • 1
  • 1
OrElse
  • 9,709
  • 39
  • 140
  • 253
  • [My open-source imageresizing.net](http://imageresizing.net) project allows you to define multiple image and text layers (or groups of them) to use as watermarks. Fully XML configurable, see [the documentation page](http://imageresizing.net/plugins/watermark). You can reference a watermark by adding it to the image URL, like image.jpg?watermark=logo. You can easily enforce watermarks using the `Pipeline.Rewrite` event handler; any rules you want to apply are available. Also, it's both ASP.NET and ASP compatible, since it has a URL API. – Lilith River Dec 09 '11 at 07:43

6 Answers6

3

Here is one more example http://www.codeproject.com/KB/web-image/ASPImaging1.aspx from codeproject that you can do many thinks on the image, including adding watermark from image.

I think that this process is take cpu power ether is on php, ether on asp.net. So an image cache schema is a must for this kind of works.

Here is some basic code. In this code you have to change the position of the watermark, and the size of the images. The watermark can be a png image with tranparent.

    public void MakePhoto(...parametres...)
    {
        Bitmap outputImage = null;
        Graphics g = null;

        try
        {                
            // the final image
            outputImage = new Bitmap(OutWidth, OutHeight, PixelFormat.Format24bppRgb);

            g = Graphics.FromImage(outputImage);
            g.CompositingMode = CompositingMode.SourceCopy;
            Rectangle destRect = new Rectangle(0, 0, OutWidth, OutHeight);

            // the photo
            using (var BasicPhoto = new Bitmap(cBasicPhotoFileOnDisk))
            {
                g.DrawImage(BasicPhoto, destRect, 0, 0, BasicPhoto.Width, BasicPhoto.Height, GraphicsUnit.Pixel);
            }

            g.CompositingMode = CompositingMode.SourceOver;
            // the watermark
            using (var WaterMark = new Bitmap(cWaterMarkPhotoOnDisk))
            {
                Rectangle destWaterRect = new Rectangle(0, 0, OutWidth, OutHeight);

                g.DrawImage(WaterMark, destWaterRect, 0, 0, OutWidth, OutHeight, GraphicsUnit.Pixel);
            }

            outputImage.Save(TheFileNameTosaveIt, ImageFormat.Jpeg);

        }
        catch (Exception x)
        {
            Debug.Assert(false);
            ... log your error, and send an error image....                
        }
        finally
        {
            if (outputImage != null)
                outputImage.Dispose();

            if (g != null)
                g.Dispose();
        }
    }

If you wish to make a custom handle the above code is stands, but you change the save line only. Something like.

public void ProcessRequest (HttpContext context)    
{
    context.Response.ContentType = "image/jpeg";

    // add you cache here
    context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(200));
    context.Response.Cache.SetMaxAge(new TimeSpan(0, 200, 0));
    context.Response.BufferOutput = false;


    ..... the above code....
    outputImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
    ..... the above code....


    context.Response.End();
}
Aristos
  • 66,005
  • 16
  • 114
  • 150
2

You'll need to use a HTTPModule as described in the ASP.NET Watermarker Module article.

Radu Pintilie
  • 339
  • 2
  • 9
1

Yes you can do this by using GDI+, using DrawString() on an Image, then saving it or returning it as a response.

Gerrie Schenck
  • 22,148
  • 20
  • 68
  • 95
1

In a post I made there is an example on watermarking a text on an image, using WPF instead of the old, deprecated GDI+.

As you can see in the article, the text is added by using the DrawText method of the DrawingContext, is really easy to use DrawImage instead, wich accept a BitmapImage.

With something like:

BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.CacheOption = BitmapCacheOption.OnLoad;
logo.UriSource = new Uri(your_physical_logopath);
logo.EndInit();

Rect rect = new Rect(0, 0, (double)logo.PixelWidth, (double)logo.PixelHeight);

dc.DrawImage(logo, rect);

With rect.X and rect.Y, before you execute DrawImage(), you can modify the relative position of the logo image inside the DrawingContext.

tanathos
  • 5,566
  • 4
  • 34
  • 46
0

old post but may be some one will look for ASP.Net Core version for text/image watermarks.

I just created a tool for this purpose, can be downloaded from nuget:

PM> Install-Package LazZiya.ImageResize -Version 2.0.0

add watermark image as below :

var img = Image.FromFile("wwwroot\\imags\\my-image.jpg");
var watermark = Image.FromFile("wwwroot\\images\\watermark.png");

img.ImageWatermark(watermark, 
    TargetSpot.TopRight, //spot to place the watermark
    10,                  //margin from border
    40);                 //opacity of image watermark

img.SaveAs("wwwroot\\images\\new-image.jpg");

the tool has more capabilities as resize, crop and adding text watermark as well.

see more samples here

LazZiya
  • 5,286
  • 2
  • 24
  • 37
0

GroupDocs.Watermark for .NET is a powerful and easy to use API for adding text as well as image watermarks to the image files. This is how you can add watermarks with a few lines of code:

using (ImageDocument doc = Document.Load<ImageDocument>("D:\\image.jpeg"))
{
    // Add text watermark
    TextWatermark watermark = new TextWatermark("Protected Document", new Font("Arial", 8));
    watermark.HorizontalAlignment = HorizontalAlignment.Center;
    watermark.VerticalAlignment = VerticalAlignment.Center;
    doc.AddWatermark(watermark);

    // Add image watermark
    ImageWatermark imageWatermark = new ImageWatermark("D:\\watermark.png");
    doc.AddWatermark(imageWatermark);

    // Save document
    doc.Save("D:\\output.jpeg");
}

Disclosure: I work as a Developer Evangelist at GroupDocs.

bummi
  • 27,123
  • 14
  • 62
  • 101
Usman Aziz
  • 100
  • 3
  • @bummi can you please explain why these parts of answer 1) "new visitors may get help from this answer." and 2) "I work as a Developer Evangelist at GroupDocs." should not be removed? They add nothing to the answer itself. – sanyassh May 03 '19 at 11:18
  • This is the paid one, right? – sanampakuwal Jun 12 '23 at 03:40