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" />
<br />
<asp:Button ID="Button1" runat="server" OnClick="OnDownoad" Text="Download" />
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 OnDownoad(object sender, EventArgs e)
{
string watermarkText1 = "© aspsnippet.com";
string watermarkText2 = "© love";
string watermarkText3 = "© my";
string filePath = Server.MapPath(Image1.ImageUrl);
using (FileStream fs = new FileStream(filePath, 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);
byte[] bytes = null;
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);
bytes = memoryStream.ToArray();
}
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.BinaryWrite(bytes);
Response.End();
}
}
}
}
VB.Net
Protected Sub OnDownoad(sender As Object, e As EventArgs)
Dim watermarkText1 As String = "© aspsnippet.com"
Dim watermarkText2 As String = "© love"
Dim watermarkText3 As String = "© my"
Dim filePath As String = Server.MapPath(Image1.ImageUrl)
Using fs As FileStream = New FileStream(filePath, 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)
Dim bytes As Byte() = Nothing
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)
bytes = memoryStream.ToArray()
End Using
Response.ContentType = "image/jpeg"
Response.AppendHeader("Content-Disposition", "attachment; filename=" & Path.GetFileName(filePath))
Response.BinaryWrite(bytes)
Response.End()
End Using
End Using
End Using
End Sub
Screenshot
Downloaded Image