WaterMark کردن متن روی تصویر Asp.Net
شنبه 19 مهر 1393در این مقاله نحوه نوشتن متن رو تصویر بعد از آپلود رو آموزش خواهم داد .
سلام دوستان
قصد داریم در این مقاله متن Barnamenevisan.Org رو رو تصاویری که با استفاده از FileUpload به سرور منتقل میشوند را درج کنیم .
ابتدا یک Button و یک FileUpload ایجاد میکنیم
<asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button1" Text="انتقال به سرور" runat="server" onclick="Button1_Click"/>
سپس کدها مربوط به کلید انتقال به سرور
string watermarkText = "© Barnamenevisan.Org";
//Get the file name.
string fileName = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName) + ".png";
//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;
//Start file download.
Response.Clear();
Response.ContentType = "image/png";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
//Write the MemoryStream to the Response.
memoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.Close();
Response.End();
}
}
}
نمونه هم ضمیمه شده
- ASP.net
- 4k بازدید
- 19 تشکر