asp.net如何在图片上加水印文字
来源:互联网 发布:管理科学就业前景知乎 编辑:程序博客网 时间:2024/04/29 02:08
为了保证网站图片的版权性和网络推广的可能性,有的客户可能需要在网站的图片上加上公司名称的水印。下面的代码就是介绍如何在图片上加上水印文字。
第一步,添加一个一般处理程序(Handler),本例是ImageHandler
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Net.Mime;using System.IO;using System.Drawing;using System.Drawing.Imaging;using System.Drawing.Drawing2D;/// <summary>/// Summary description for ImageHandler/// </summary>public class ImageHandler : IHttpHandler{ public ImageHandler() { } public string GetContentType(String path) { switch (Path.GetExtension(path)) { case ".bmp": return "Image/bmp"; case ".gif": return "Image/gif"; case ".jpg": return "Image/jpeg"; case ".png": return "Image/png"; default: break; } return String.Empty; } public ImageFormat GetImageFormat(String path) { switch (Path.GetExtension(path).ToLower()) { case ".bmp": return ImageFormat.Bmp; case ".gif": return ImageFormat.Gif; case ".jpg": return ImageFormat.Jpeg; case ".png": return ImageFormat.Png; default: return null; } } protected byte[] WatermarkImage(HttpContext context) { byte[] imageBytes = null; if (File.Exists(context.Request.PhysicalPath)) { // Normally you'd put this in a config file somewhere. string watermark = "世复检测"; Image image = Image.FromFile(context.Request.PhysicalPath); Graphics graphic; if (image.PixelFormat != PixelFormat.Indexed && image.PixelFormat != PixelFormat.Format8bppIndexed && image.PixelFormat != PixelFormat.Format4bppIndexed && image.PixelFormat != PixelFormat.Format1bppIndexed) { // Graphic is not a Indexed (GIF) image graphic = Graphics.FromImage(image); } else { /* Cannot create a graphics object from an indexed (GIF) image. * So we're going to copy the image into a new bitmap so * we can work with it. */ Bitmap indexedImage = new Bitmap(image); graphic = Graphics.FromImage(indexedImage); // Draw the contents of the original bitmap onto the new bitmap. graphic.DrawImage(image, 0, 0, image.Width, image.Height); image = indexedImage; } graphic.SmoothingMode = SmoothingMode.AntiAlias & SmoothingMode.HighQuality; Font myFont = new Font("Arial", 15); SolidBrush brush = new SolidBrush(Color.FromArgb(255, Color.Red)); /* This gets the size of the graphic so we can determine * the loop counts and placement of the watermarked text. */ SizeF textSize = graphic.MeasureString(watermark, myFont); //// Write the text across the image. //for (int y = 0; y < image.Height; y++) //{ // for (int x = 0; x < image.Width; x++) // { // PointF pointF = new PointF(x, y); // graphic.DrawString(watermark, myFont, brush, pointF); // x += Convert.ToInt32(textSize.Width); // } // y += Convert.ToInt32(textSize.Height); //} // Write the text at the right bottom of the image. for (int y = image.Height-25; y < image.Height; y++) { for (int x = image.Width-100; x < image.Width; x++) { PointF pointF = new PointF(x, y); graphic.DrawString(watermark, myFont, brush, pointF); x += Convert.ToInt32(textSize.Width); } y += Convert.ToInt32(textSize.Height); } using (MemoryStream memoryStream = new MemoryStream()) { image.Save(memoryStream, GetImageFormat(context.Request.PhysicalPath)); imageBytes = memoryStream.ToArray(); } } return imageBytes; } #region IHttpHandler Members public bool IsReusable { get { return false; } } public void ProcessRequest(HttpContext context) { context.Response.Clear(); context.Response.ContentType = GetContentType(context.Request.PhysicalPath); byte[] imageBytes = WatermarkImage(context); if (imageBytes != null) { context.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length); } else { // No bytes = no image which equals NO FILE. // Therefore send a 404 - not found response. context.Response.StatusCode = 404; } context.Response.End(); } #endregion}
第二步,在web.config里添加如下代码:
<httpHandlers> <!--<add verb="GET" type="ImageHandler" path="*.jpg,*.png,*.gif,*.bmp"/>--> <add verb="GET" type="ImageHandler" path="Uploads/*/*.jpg"/> </httpHandlers>
第三步,调试在每一个图片的右下角显示红色的“世复检测”四个字。
0 0
- asp.net如何在图片上加水印文字
- asp.net如何在图片上加水印文字
- asp.net如何在图片上加水印文字具体实现
- ASP.NET图片上传,加水印文字和水印图片!
- ASP.NET(C#)图片加文字、图片水印
- ASP.NET(C#)图片加文字、图片水印
- ASP.NET(C#)图片加文字、图片水印
- ASP.NET(C#)图片加文字、图片水印
- C# asp.net上传图片加水印文字(个人心得)
- asp.net]图片加水印
- asp.net图片加水印
- 在图片上加水印
- ASP.NET中利用DataGrid控件显示图片以及在图片和文字上加超链接
- 图片加文字水印
- 图片加水印文字
- asp.net 中给图片加水印
- ASP.NET图片上传加水印
- ASP.NET 图片加水印防盗链
- 二项分布
- 安卓的进化—Part2
- 25个帮助你提高技能的 CSS3 实战教程
- JS 获取JSON 数据,想让JSON 如何变成 key,和vale 各一组的数组啊
- linux命令汇总
- asp.net如何在图片上加水印文字
- String.Format格式说明
- Android实现局部图片滑动指引效果
- 8 款极具表现力的jQuery/CSS3网页菜单
- 欧拉角演示程序
- Mac和iOS开发资源汇总—更新于2013-10-14
- datagridview行高的设置
- 堆栈的链表实现
- ASP.NET弹出提示框几种基本方法