asp.net如何在图片上加水印文字具体实现
2013-12-12 00:00
926 查看
第一步,添加一个一般处理程序(Handler),本例是ImageHandler
第二步,在web.config里添加如下代码:
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>
相关文章推荐
- 关于ASP.NET中TreeView用法的一个小例子
- dataTable常用的属性和方法
- ASP.NET Web API自身对CORS的支持: EnableCorsAttribute特性背后的故事
- 在 Visual Studio 2013 中创建 ASP.NET Web 项目(1):概述 - 创建 Web 应用程序项目
- asp.net中FileUpload上传文件的方法
- Asp.Net 前后台交互小结
- asp.net发送E-mail
- Asp.net、Html、Css之间的关系
- Asp.Net学习进度备忘(第一步:ASP.NET Web Forms)
- 如何让asp.net执行命令,暂停几秒,再执行下一行命令?(已解决)
- 在 Visual Studio 2013 中创建 ASP.NET Web 项目(1):概述 - 创建 Web 应用程序项目
- 转:ASP.NET MVC中Unobtrusive Ajax的妙用
- ASP.NET方法如何重载需要必备哪些条件
- asp.net - 获得当前访问者登陆的域和windows账号
- Vs2010设计ASP.NET页面时出现"创建控件时出错"
- 【转载】在asp.net中使用session常见问题
- asp.net的Web应用报“超时时间已到。在操作完成之前超时时间已过或服务器未响应”问题的解决
- ASP.NET设置session超时时长的4中方法
- 在asp.net以外程序使用membership的配置
- asp.net常用技术总结