Download Source
In this article you will learn how to upload image and show preview.there is a textbox which is used to add a watermark text in image.there is button which is used to download the watermark image. .mdf database is create in visual studio
HTML CODE
C# CODE
Output Preview
Download Source
In this article you will learn how to upload image and show preview.there is a textbox which is used to add a watermark text in image.there is button which is used to download the watermark image. .mdf database is create in visual studio
HTML CODE
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="button.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div align="center" style="font-size: 20px;">
Upload Image And Add Watermark Text
</div>
<br />
<div>
<table style="margin-left: 350px;">
<tr>
<td>
<div style="margin-top:-140px;">
<asp:FileUpload ID="fupload" runat="server" />
<br />
<input id="btnupload" runat="server" type="submit" value="Upload" class="shiny-button" onserverclick="btnupload_ServerClick" />
</div>
</td>
<td>
<asp:Image ID="img1" runat="server" Width="300px" Height="180px" />
<br />
<br />
<input type="text" id="txtwatermark" runat="server" class="textbox" />
<br />
<br />
<input type="submit" id="btndownload" runat="server" value="Download Image" class="shiny-button" onserverclick="btndownload_ServerClick" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
C# CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
namespace WatermarkImage
{
public partial class Home : System.Web.UI.Page
{
public static string filename;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnupload_ServerClick(object sender, EventArgs e)
{
if (fupload.HasFile) //uploading file
{
filename = Path.GetFileName(fupload.PostedFile.FileName);
fupload.SaveAs(Server.MapPath("~/Upload/" + filename)); //save image in upload folder
img1.ImageUrl = "~/Upload/" + filename;
}
}
protected void btndownload_ServerClick(object sender, EventArgs e)
{
string fileName;
int num;
Random obj = new Random(); //random number genrate
num = obj.Next(0, 1000000);
fileName = (num + ".jpg");
System.Drawing.Image upImage = System.Drawing.Image.FromFile(Server.MapPath(img1.ImageUrl));
using (System.Drawing.Graphics g = Graphics.FromImage(upImage))
{
int opacity = 120; //show text to image
SolidBrush brush = new SolidBrush(Color.FromArgb(opacity, Color.Black));
Font font = new Font("Calibri", 50);
g.DrawString(txtwatermark.Value, font, brush, new PointF(8, 15));
upImage.Save(Path.Combine(Server.MapPath("~\\Download\\" + fileName)));
g.Dispose();
brush.Dispose();
font.Dispose();
}
upImage.Dispose();
Response.Clear(); //download image from download folder
Response.ContentType = "Application/jpg";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + "");
Response.TransmitFile("~\\Download\\" + fileName);
Response.Flush();
}
}
}
Output Preview
Download Source
No comments:
Post a Comment