ASP.NET: Adding a Watermark to Images on the Fly

I saw great questions and answers regarding adding a watermark on images with php

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

So here are a few questions.

  • How can I do this with ASP?
  • Will this process be a big overload for the server?
  • Can I use an image for a watermark instead of plain text?
+4
source share
6 answers

Here is another example of http://www.codeproject.com/KB/web-image/ASPImaging1.aspx from codeproject, which you can do a lot on the image, including adding a watermark from the image.

I think this process requires the processor power ether to be on php, ether on asp.net. Thus, the image cache scheme is mandatory for this kind of work.

Here is the basic code. In this code, you need to change the position of the watermark and the size of the images. A watermark can be a transparent PNG image.

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 want to create a custom descriptor, the above code is worth it, but you only change the save line. Sort of.

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

You need to use the HTTPModule as described in the ASP.NET Watermarker Module .

+2
source

Yes, you can do this using GDI + , using DrawString () on the image, then save it or return it as an answer.

+1
source

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

As you can see in the article, the text is added using the DrawText DrawingContext method, in fact, just use DrawImage, which accepts 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 executing DrawImage (), you can change the relative position of the logo image inside the DrawingContext.

+1
source

an old post, but maybe someone will be looking for a version of ASP.Net Core 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 a watermark image as shown 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 features such as resizing, cropping and adding watermark text as well.

see more samples here

0
source

GroupDocs.Watermark for .NET is a powerful and easy-to-use API for adding text as well as watermarked images to image files. Here's 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.

-1
source

Source: https://habr.com/ru/post/1345526/


All Articles