简体   繁体   English

ASP.NET:动态地为图像添加“水印”

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

I have seen great questions and answers regarding adding watermark on images with php 我已经看到有关使用php在图像上添加水印的很多问题和答案

I would like to do the same, this time with ASP.NET 我想这样做,这次是用ASP.NET

So here are a couple of questions. 所以这里有几个问题。

  1. How can i do that with ASP? 我怎么能用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. 这是来自codeproject的另一个示例http://www.codeproject.com/KB/web-image/ASPImaging1.aspx ,您可以对图像做很多想法,包括从图像添加水印。

I think that this process is take cpu power ether is on php, ether on asp.net. 我认为这个过程是采取cpu power ether在php,ether.net上的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. 水印可以是具有透明的png图像。

    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. 如果您希望自定义句柄,则上述代码为stand,但您只更改了保存行。 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+. 我发表的一篇文章中,有一个使用WPF而不是旧的,弃用的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. 正如你在文章中看到的那样,使用DrawingContext的DrawText方法添加文本,实际上很容易使用DrawImage,它接受一个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. 使用rect.X和rect.Y,在执行DrawImage()之前,您可以修改DrawingContext中徽标图像的相对位置。

old post but may be some one will look for ASP.Net Core version for text/image watermarks. 老帖但可能会有人会为文本/图像水印寻找ASP.Net核心版。

I just created a tool for this purpose, can be downloaded from nuget: 我刚刚为此创建了一个工具,可以从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. GroupDocs.Watermark for .NET是一个功能强大且易于使用的API,用于向图像文件添加文本和图像水印。 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. 披露: 我在GroupDocs担任开发人员传播者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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