Display multiple Watermark on existing ASP.Net Image using C# and VB.Net

t01714500930
 
on Mar 27, 2021 07:22 AM
1064 Views

ref: 

Display Watermark on Existing ASP.Net Image on Page Load using C# and VB.Net

string watermarkText1 = "© aspsnippet.com";
string watermarkText2 = "© love";
string watermarkText3 = "© my";

3 watermark text possible? 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Mar 27, 2021 08:00 AM

Hi t01714500930,

Check this example. Now please take its reference and correct your code.

HTML

<asp:Image ID="Image1" ImageUrl="~/Tulips.jpg" runat="server" Width="300" Height="300" />

Namespaces

C#

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

VB.Net

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string watermarkText1 = "© aspsnippet.com";
        string watermarkText2 = "© love";
        string watermarkText3 = "© my";
        string fileName = Server.MapPath(Image1.ImageUrl);
        using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate))
        {
            using (Bitmap bmp = new Bitmap(fs, false))
            {
                using (Graphics grp = Graphics.FromImage(bmp))
                {
                    Brush brush = new SolidBrush(Color.Red);
                    Font font = new Font("Arial", 50, FontStyle.Bold, GraphicsUnit.Pixel);
                    SizeF textSize = new SizeF();
                    textSize = grp.MeasureString(watermarkText3, font);
                    Point position = new Point((bmp.Width - ((int)textSize.Width + 10)), (bmp.Height - ((int)textSize.Height + 10)));
                    grp.DrawString(watermarkText3, font, brush, position);

                    textSize = grp.MeasureString(watermarkText2, font);
                    position = new Point((bmp.Width - ((int)textSize.Width + 10)), (bmp.Height - ((int)textSize.Height + 60)));
                    grp.DrawString(watermarkText2, font, brush, position);

                    textSize = grp.MeasureString(watermarkText1, font);
                    position = new Point((bmp.Width - ((int)textSize.Width + 10)), (bmp.Height - ((int)textSize.Height + 120)));
                    grp.DrawString(watermarkText1, font, brush, position);

                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        bmp.Save(memoryStream, ImageFormat.Png);
                        string base64String = Convert.ToBase64String(memoryStream.ToArray());
                        string imageUrl = "data:image/png;base64," + base64String;
                        Image1.Attributes.Add("src", imageUrl);
                    }
                }
            }
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim watermarkText1 As String = "© aspsnippet.com"
        Dim watermarkText2 As String = "© love"
        Dim watermarkText3 As String = "© my"
        Dim fileName As String = Server.MapPath(Image1.ImageUrl)
        Using fs As FileStream = New FileStream(fileName, FileMode.OpenOrCreate)
            Using bmp As Bitmap = New Bitmap(fs, False)
                Using grp As Graphics = Graphics.FromImage(bmp)
                    Dim brush As Brush = New SolidBrush(Color.Red)
                    Dim font As Font = New Font("Arial", 50, FontStyle.Bold, GraphicsUnit.Pixel)
                    Dim textSize As SizeF = New SizeF()
                    textSize = grp.MeasureString(watermarkText3, font)
                    Dim position As Point = New Point((bmp.Width - (CInt(textSize.Width) + 10)), (bmp.Height - (CInt(textSize.Height) + 10)))
                    grp.DrawString(watermarkText3, font, brush, position)

                    textSize = grp.MeasureString(watermarkText2, font)
                    position = New Point((bmp.Width - (CInt(textSize.Width) + 10)), (bmp.Height - (CInt(textSize.Height) + 60)))
                    grp.DrawString(watermarkText2, font, brush, position)

                    textSize = grp.MeasureString(watermarkText1, font)
                    position = New Point((bmp.Width - (CInt(textSize.Width) + 10)), (bmp.Height - (CInt(textSize.Height) + 120)))
                    grp.DrawString(watermarkText1, font, brush, position)

                    Using memoryStream As MemoryStream = New MemoryStream()
                        bmp.Save(memoryStream, ImageFormat.Png)
                        Dim base64String As String = Convert.ToBase64String(memoryStream.ToArray())
                        Dim imageUrl As String = "data:image/png;base64," & base64String
                        Image1.Attributes.Add("src", imageUrl)
                    End Using
                End Using
            End Using
        End Using
    End If
End Sub

Screenshot