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

nauna
 
on Jun 03, 2016 06:42 AM
7078 Views

this code is working fine to add water mark while uploading image on. 

I already have images on folder and it binds on image control, how can apply code on image1 control so when it load, it apply watermark. 

when i am changing value of fileuploadcontrol to image1.imageurl so it is giving error of input stream 

        string watermarkText = "© aspsnippet.com";
        //Get the file name.
        string fileName = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName) + ".png";

        //Read the File into a Bitmap.
        using (Bitmap bmp = new Bitmap(FileUpload1.PostedFile.InputStream, false))
        {
            using (Graphics grp = Graphics.FromImage(bmp))
            {
                //Set the Color of the Watermark text.
                Brush brush = new SolidBrush(Color.Red);

                //Set the Font and its size.
                Font font = new System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel);

                //Determine the size of the Watermark text.
                SizeF textSize = new SizeF();
                textSize = grp.MeasureString(watermarkText, font);

                //Position the text and draw it on the image.
                Point position = new Point((bmp.Width - ((int)textSize.Width + 10)), (bmp.Height - ((int)textSize.Height + 10)));
                grp.DrawString(watermarkText, font, brush, position);

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    //Save the Watermarked image to the MemoryStream.
                    bmp.Save(memoryStream, ImageFormat.Png);
                    memoryStream.Position = 0;

                    //Start file download.
                    Response.Clear();
                    Response.ContentType = "image/png";
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);

                    //Write the MemoryStream to the Response.
                    memoryStream.WriteTo(Response.OutputStream);
                    Response.Flush();
                    Response.Close();
                    Response.End();
                }
            }
        }

 

<asp:Image ID="Image1" runat="server" 
        ImageUrl="~/Images/_89843316_79842456.jpg" />

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Shashikant
 
on Jun 03, 2016 09:03 AM
on Jun 03, 2016 10:02 AM

Hi

Please refer below code.

HTML

<div>
    <asp:Image ID="Image1" runat="server" ImageUrl="~/Images/Chrysanthemum.jpg" />
</div>

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string watermarkText = "© aspsnippet.com";
        string fileName = Server.MapPath(Image1.ImageUrl);
        FileStream fs = new FileStream(fileName, FileMode.Open);
        using (Bitmap bmp = new Bitmap(fs, false))
        {
            using (Graphics grp = Graphics.FromImage(bmp))
            {
                Brush brush = new SolidBrush(Color.Red);
                Font font = new System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel);
                SizeF textSize = new SizeF();
                textSize = grp.MeasureString(watermarkText, font);
                Point position = new Point((bmp.Width - ((int)textSize.Width + 10)), (bmp.Height - ((int)textSize.Height + 10)));
                grp.DrawString(watermarkText, 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)
    If Not Me.IsPostBack Then
        Dim watermarkText As String = "© aspsnippet.com"
        Dim fileName As String = Server.MapPath(Image1.ImageUrl)
        Dim fs As FileStream = New FileStream(fileName, FileMode.Open)

        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 System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel)
                Dim textSize As SizeF = New SizeF()
                textSize = grp.MeasureString(watermarkText, font)
                Dim position As Point = New Point((bmp.Width - (CInt(textSize.Width) + 10)), (bmp.Height - (CInt(textSize.Height) + 10)))
                grp.DrawString(watermarkText, 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 If
End Sub

Screenshot