How To Add Watermark Text To Images in ASP.NET Using C# Chintan Prajapati March 22, 2018 4 min read Prevention is better than preserve, Most of the time, we run websites/blogs, face issues of our images being copied and Used on other blogs. There are many techniques we can use to protect our copyrighted content and prevent people from doing so. One of them is watermarking our images with our logo or watermarking the website name on the images. In one of my project, I had to face the same Issue. In this article, Here I am going to propose a feasible solution to add Text and Image as a watermark on our Image/Photo using C#.Follow these simple steps:Steps: Select Image which you want to set Watermark. Create two folder one is Imagtoor store your watermark logo image those you want to show on Image/Photo as a watermark.Create One more folder Img Watermark in this folder store the final image. Create a class i.e. a Watermarker in the App_Start folder to do that right-click on the App_Start folder then click on Add Items; select class gives it the name Watermarker.Note: Give the reference to “System.Windows.Forms”using System; using System.ComponentModel; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; namespace Watermark { #region WatermarkPosition public enum WatermarkPosition { Absolute, TopLeft, TopRight, TopMiddle, BottomLeft, BottomRight, BottomMiddle, MiddleLeft, MiddleRight, Center } #endregion #region Watermarker public class Watermarker { #region Private Fields private Image m_image; private Image m_originalImage; private Image m_watermark; private float m_opacity = 1.0f; private WatermarkPosition m_position = WatermarkPosition.Absolute; private int m_x = 0; private int m_y = 0; private Color m_transparentColor = Color.Empty; private RotateFlipType m_rotateFlip = RotateFlipType.RotateNoneFlipNone; private Padding m_margin = new Padding(0); private Font m_font = new Font(FontFamily.GenericSansSerif, 10); private Color m_fontColor = Color.Black; private float m_scaleRatio = 1.0f; #endregion #region Public Properties [Browsable(false)] public Image Image { get { return m_image; } } public WatermarkPosition Position { get { return m_position; } set { m_position = value; } } public int PositionX { get { return m_x; } set { m_x = value; } } public int PositionY { get { return m_y; } set { m_y = value; } } public float Opacity { get { return m_opacity; } set { m_opacity = value; } } public Color TransparentColor { get { return m_transparentColor; } set { m_transparentColor = value; } } public RotateFlipType RotateFlip { get { return m_rotateFlip; } set { m_rotateFlip = value; } } public Padding Margin { get { return m_margin; } set { m_margin = value; } } public float ScaleRatio { get { return m_scaleRatio; } set { m_scaleRatio = value; } } public Font Font { get { return m_font; } set { m_font = value; } } public Color FontColor { get { return m_fontColor; } set { m_fontColor = value; } } #endregion #region Constructors public Watermarker(Image image) { LoadImage(image); } public Watermarker(string filename) { LoadImage(Image.FromFile(filename)); } #endregion #region Public Methods public void ResetImage() { m_image = new Bitmap(m_originalImage); } public void DrawImage(string filename) { DrawImage(Image.FromFile(filename)); } public void DrawImage(Image watermark) { if (watermark == null) throw new ArgumentOutOfRangeException("Watermark"); if (m_opacity < 0 || m_opacity > 1) throw new ArgumentOutOfRangeException("Opacity"); if (m_scaleRatio <= 0) throw new ArgumentOutOfRangeException("ScaleRatio"); m_watermark = GetWatermarkImage(watermark); m_watermark.RotateFlip(m_rotateFlip); Point waterPos = GetWatermarkPosition(); Rectangle destRect = new Rectangle(waterPos.X, waterPos.Y, m_watermark.Width, m_watermark.Height); ColorMatrix colorMatrix = new ColorMatrix( new float[][] { new float[] { 1, 0f, 0f, 0f, 0f}, new float[] { 0f, 1, 0f, 0f, 0f}, new float[] { 0f, 0f, 1, 0f, 0f}, new float[] { 0f, 0f, 0f, m_opacity, 0f}, new float[] { 0f, 0f, 0f, 0f, 1} }); ImageAttributes attributes = new ImageAttributes(); attributes.SetColorMatrix(colorMatrix); if (m_transparentColor != Color.Empty) { attributes.SetColorKey(m_transparentColor, m_transparentColor); } using (Graphics gr = Graphics.FromImage(m_image)) { gr.DrawImage(m_watermark, destRect, 0, 0, m_watermark.Width, m_watermark.Height, GraphicsUnit.Pixel, attributes); } } public void DrawText(string text) { Image textWatermark = GetTextWatermark(text); DrawImage(textWatermark); } #endregion #region Private Methods private void LoadImage(Image image) { m_originalImage = image; ResetImage(); } private Image GetTextWatermark(string text) { Brush brush = new SolidBrush(m_fontColor); SizeF size; using (Graphics g = Graphics.FromImage(m_image)) { size = g.MeasureString(text, m_font); } Bitmap bitmap = new Bitmap((int)size.Width, (int)size.Height); bitmap.SetResolution(m_image.HorizontalResolution, m_image.VerticalResolution); using (Graphics g = Graphics.FromImage(bitmap)) { g.DrawString(text, m_font, brush, 0, 0); } return bitmap; } private Image GetWatermarkImage(Image watermark) { if (m_margin.All == 0 && m_scaleRatio == 1.0f) return watermark; int newWidth = Convert.ToInt32(watermark.Width * m_scaleRatio); int newHeight = Convert.ToInt32(watermark.Height * m_scaleRatio); Rectangle sourceRect = new Rectangle(m_margin.Left, m_margin.Top, newWidth, newHeight); Rectangle destRect = new Rectangle(0, 0, watermark.Width, watermark.Height); Bitmap bitmap = new Bitmap(newWidth + m_margin.Left + m_margin.Right, newHeight + m_margin.Top + m_margin.Bottom); bitmap.SetResolution(watermark.HorizontalResolution, watermark.VerticalResolution); using (Graphics g = Graphics.FromImage(bitmap)) { g.DrawImage(watermark, sourceRect, destRect, GraphicsUnit.Pixel); } return bitmap; } private Point GetWatermarkPosition() { int x = 0, y = 0; switch (m_position) { case WatermarkPosition.Absolute: x = m_x; y = m_y; break; case WatermarkPosition.TopLeft: x = 0; y = 0; break; case WatermarkPosition.TopRight: x = m_image.Width - m_watermark.Width; y = 0; break; case WatermarkPosition.TopMiddle: x = (m_image.Width - m_watermark.Width) / 2; y = 0; break; case WatermarkPosition.BottomLeft: x = 0; y = m_image.Height - m_watermark.Height; break; case WatermarkPosition.BottomRight: x = m_image.Width - m_watermark.Width; y = m_image.Height - m_watermark.Height; break; case WatermarkPosition.BottomMiddle: x = (m_image.Width - m_watermark.Width) / 2; y = m_image.Height - m_watermark.Height; break; case WatermarkPosition.MiddleLeft: x = 0; y = (m_image.Height - m_watermark.Height) / 2; break; case WatermarkPosition.MiddleRight: x = m_image.Width - m_watermark.Width; y = (m_image.Height - m_watermark.Height) / 2; break; case WatermarkPosition.Center: x = (m_image.Width - m_watermark.Width) / 2; y = (m_image.Height - m_watermark.Height) / 2; break; default: break; } return new Point(x, y); } #endregion } #endregion } In Controller write following code. using System.Drawing; using Watermark; using System.Windows.Forms; using System.IO; public ActionResult WaterMarkImage(HttpPostedFileBase fileToUpload) { using (Image image = Image.FromStream(fileToUpload.InputStream, true, false)) { string name = Path.GetFileNameWithoutExtension(fileToUpload.FileName); var ext = Path.GetExtension(fileToUpload.FileName); string myfile = name+ext; var saveImagePath = Path.Combine(Server.MapPath(“~/ImgWatermark”), myfile); Image watermarkImage = Image.FromFile(Server.MapPath(“/Img/watermarklogo.png”)); Watermarker objWatermarker = new Watermarker(image); for (int i = 0; i < image.Height; i++) { for (int j = 0; j < image.Width; j++) { // Set the properties for the logo objWatermarker.Position = WatermarkPosition.Absolute; objWatermarker.PositionX = j; objWatermarker.PositionY = i; objWatermarker.Margin = new Padding(20); objWatermarker.Opacity = 0.5f; objWatermarker.TransparentColor = Color.White; objWatermarker.ScaleRatio = 3; // Draw the logo objWatermarker.DrawImage(watermarkImage); //Draw the Text //objWatermarker.DrawText(“WaterMarkDemo”) j = j + 400;// watermark image width } i = i + 120;// } objWatermarker.Image.Save(saveImagePath); return RedirectToAction(“Index”, new { imgName = myfile }); } } Please modify your Index Action for show image on UI: public ActionResult Index(string imgName) { // PdfcrowdTest(); // objImg.Save(Server.MapPath(“~/ImgWatermark/2.jpg”)); ViewBag.ImgName = imgName; return View(); } Example:The source code shown in this article can be downloaded from here:https://github.com/prajapatichintan/Add-Watermark-on-Image