Hi counterkin,
Refer below sample.
HTML
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button Text="Upload" runat="server" OnClick="Upload" />
Namespaces
C#
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
VB.Net
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Code
C#
protected void Upload(object sender, EventArgs e)
{
string watermarkText = "© ASPSnippets.com";
//Get the file name.
string fileName = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName) + ".png";
FileUpload1.SaveAs(@"C:\project\watermark\uploads\" + fileName);
//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;
FileStream stream = new FileStream(@"C:\project\watermark\uploads\" + fileName, FileMode.Create, FileAccess.Write);
memoryStream.WriteTo(stream);
}
}
}
}
VB.Net
Protected Sub Upload(sender As Object, e As EventArgs)
Dim watermarkText As String = "© ASPSnippets.com"
'Get the file name.
Dim fileName As String = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName) & ".png"
FileUpload1.SaveAs("C:\project\watermark\uploads\" & fileName)
'Read the File into a Bitmap.
Using bmp As New Bitmap(FileUpload1.PostedFile.InputStream, False)
Using grp As Graphics = Graphics.FromImage(bmp)
'Set the Color of the Watermark text.
Dim brush As Brush = New SolidBrush(Color.Red)
'Set the Font and its size.
Dim font As Font = New System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel)
'Determine the size of the Watermark text.
Dim textSize As New SizeF()
textSize = grp.MeasureString(watermarkText, font)
'Position the text and draw it on the image.
Dim position As New Point((bmp.Width - (CInt(textSize.Width) + 10)), (bmp.Height - (CInt(textSize.Height) + 10)))
grp.DrawString(watermarkText, font, brush, position)
Using memoryStream As New MemoryStream()
'Save the Watermarked image to the MemoryStream.
bmp.Save(memoryStream, ImageFormat.Png)
memoryStream.Position = 0
Dim stream As FileStream = New FileStream("C:\project\watermark\uploads\" & fileName, FileMode.Create, FileAccess.Write)
memoryStream.WriteTo(stream)
End Using
End Using
End Using
End Sub