Introduction: In this
article I will explain how we can add the watermark of logo to images 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. Now copy the logo of website
and keep/paste it in images folder also. Drag and drop the fileupload control
from toolbox to aspx page.
HTML
markup of page:
<fieldset style="width:500px">
<legend>Example
to add Watermark of Logo 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
bmpStamp = new Bitmap(Server.MapPath("images") + "\\"
+ "logo.png");
Bitmap
bmpUpload = new Bitmap(FileUpload1.PostedFile.InputStream,
false);
Graphics
graphicsObj = Graphics.FromImage(bmpUpload);
Point
postionWaterMark = new Point((bmpUpload.Width
/ 20), (bmpUpload.Height / 20));
graphicsObj.DrawImage(bmpStamp,
postionWaterMark);
string
filepath = Server.MapPath("~/images/")
+ Guid.NewGuid() + Path.GetFileName(FileUpload1.PostedFile.FileName)
+ ".jpg";
bmpUpload.Save(filepath);
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
bmpStamp As New
Bitmap(Server.MapPath("images") + "\"
+ "logo.png")
Dim
bmpUpload As New
Bitmap(FileUpload1.PostedFile.InputStream, False)
Dim
graphicsObj As Graphics
= Graphics.FromImage(bmpUpload)
Dim
postionWaterMark As New
Point((bmpUpload.Width / 20),
(bmpUpload.Height / 20))
graphicsObj.DrawImage(bmpStamp,
postionWaterMark)
Dim
filepath As String
= Server.MapPath("~/images/")
& Guid.NewGuid().ToString() & Path.GetFileName(FileUpload1.PostedFile.FileName)
+ ".jpg"
bmpUpload.Save(filepath)
Response.Write("<script
type=""text/javascript"">alert('image uploaded
Successfully!!!');</script>")
Catch ex As Exception
End Try
End Sub
Now
build, run the project and see the result.
In this article we have learn to add logo watermark to images dynamically while uploading in asp.net (C#,VB). I hope you enjoyed this article.
That's pretty darn sweet! Can you add the watermark to an existing image?
ReplyDeleteThanks for your feedback... check this article : add watermark
Deletethank u very much
ReplyDelete