简体   繁体   中英

ASP.NET: Adding 'Watermark' to images on the fly

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?

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();
}

您需要使用ASP.NET水印模块文章中描述的HTTPModule

是的,您可以使用GDI + ,在Image上使用DrawString() ,然后保存或将其作为响应返回。

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM