Introduction: In this
article I will explain how we can add the watermark text to images dynamically
while uploading in asp.net (C#, VB)
Description:
To
implement this functionality to project/website add a webform. Create a folder
images in project/website to store the uploaded images. Drag and drop the fileupload
control from toolbox to aspx page.
HTML
markup of page:
<fieldset style="width:500px">
<legend>Example
to add Watermark to uploaded images</legend>
<table>
<tr><td>Upload File</td><td>
<asp:FileUpload ID="FileUpload1" runat="server" /></td></tr>
<tr><td></td><td></td></tr>
<tr><td></td><td>
<asp:Button ID="Button1" runat="server" Text="Upload File" /></td></tr>
</table>
</fieldset>
Now
add the namespaces.
C#:
using
System.IO;
using
System.Drawing;
VB:
Imports
System.IO
Imports
System.Drawing
After
that write the below given code on button click.
C#:
protected void Button1_Click(object
sender, EventArgs e)
{
try
{
Bitmap
bmp = new Bitmap(FileUpload1.PostedFile.InputStream,
false);
Graphics
graphicsobj = Graphics.FromImage(bmp);
Brush
brush = new SolidBrush(Color.FromArgb(80, 255, 255, 255));
Point
postionWaterMark = new Point((bmp.Width
/ 2), (bmp.Height * 9 / 10));
graphicsobj.DrawString("© Articlemirror.in", new System.Drawing.Font("Arial", 30, FontStyle.Bold,
GraphicsUnit.Pixel), brush,
postionWaterMark);
string
filepath = Server.MapPath("~/images/")
+ Guid.NewGuid() + Path.GetFileName(FileUpload1.PostedFile.FileName);
bmp.Save(filepath);
graphicsobj.Dispose();
Response.Write("<script
type=\"text/javascript\">alert('image uploaded
Successfully!!!');</script>");
}
catch (Exception ex)
{
}
}
VB:
Protected Sub Button1_Click(sender As
Object, e As
System.EventArgs) Handles
Button1.Click
Try
Dim
bmp As New Bitmap(FileUpload1.PostedFile.InputStream, False)
Dim
graphicsobj As Graphics
= Graphics.FromImage(bmp)
Dim
brush As Brush
= New SolidBrush(Color.FromArgb(80, 255, 255, 255))
Dim
postionWaterMark As New
Point((bmp.Width / 2), (bmp.Height * 9 /
10))
graphicsobj.DrawString("© Articlemirror.in", New System.Drawing.Font("Arial", 30, FontStyle.Bold,
GraphicsUnit.Pixel), brush,
postionWaterMark)
Dim
filepath As String
= Server.MapPath("~/images/")
& Guid.NewGuid().ToString() & Path.GetFileName(FileUpload1.PostedFile.FileName)
bmp.Save(filepath)
graphicsobj.Dispose()
Response.Write("<script
type=""text/javascript"">alert('image uploaded
Successfully!!!');</script>")
Catch
ex As Exception
End Try
End Sub
No comments:
Post a Comment