asp.net如何在图片上加水印文字具体实现

 更新时间:2013年12月12日 15:30:47   投稿:shangke  
这篇文章主要介绍了asp.net如何在图片上加水印文字具体实现,有需要的朋友可以参考一下

第一步,添加一个一般处理程序(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>

相关文章

  • MAUI中实现构建跨平台原生控件

    MAUI中实现构建跨平台原生控件

    这篇文章介绍了MAUI中实现构建跨平台原生控件的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-02-02
  • 使用.Net Core实现的一个图形验证码功能

    使用.Net Core实现的一个图形验证码功能

    SimpleCaptcha是一个使用简单,基于.Net Standard 2.0的图形验证码模块。这篇文章主要介绍了使用.Net Core实现的一个图形验证码功能,需要的朋友可以参考下
    2021-12-12
  • http转https的实战记录(iis 7.5)

    http转https的实战记录(iis 7.5)

    这篇文章主要给大家介绍了关于http转https的相关资料,文中是最近的一次实战记录,基于iis7.5,通过一步步的图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2018-01-01
  • .NET8 依赖注入

    .NET8 依赖注入

    依赖注入是一种设计模式,用于解耦组件(服务)之间的依赖关系,它通过将依赖关系的创建和管理交给外部容器来实现,而不是在组件(服务)内部直接创建依赖对象,本文介绍.NET8 依赖注入的相关知识,感兴趣的朋友一起看看吧
    2023-12-12
  • .net读写xml文档详解

    .net读写xml文档详解

    这篇文章主要介绍了.net读写xml文档的示例,需要的朋友可以参考下
    2014-05-05
  • 微信公众平台开发之发送图文消息.Net代码解析

    微信公众平台开发之发送图文消息.Net代码解析

    这篇文章主要为大家详细解析了微信公众平台开发之发送图文消息.Net代码,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • 浅谈ASP.NET中MVC 4 的JS/CSS打包压缩功能

    浅谈ASP.NET中MVC 4 的JS/CSS打包压缩功能

    打包(Bundling)及压缩(Minification)指的是将多个js文件或css文件打包成单一文件并压缩的做法,如此可减少浏览器需下载多个文件案才能完成网页显示的延迟感等,能有效缩小文件案体积,提高传输效率,提供使用者更流畅的浏览体验。
    2015-06-06
  • asp.net的公共变量声明问题(解决)

    asp.net的公共变量声明问题(解决)

    前段时间有个项目中登录的时候碰到了声明公共变量的情况,用户类型分为二种类型,所以不同类型用户登录的时候所走的情况不一样,目前用AB分别代表二类用户。
    2013-03-03
  • netcore 生成验证码的过程详解

    netcore 生成验证码的过程详解

    这篇文章主要介绍了netcore 生成验证码的过程详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧
    2024-06-06
  • ASP.NET Core通用主机的系统配置

    ASP.NET Core通用主机的系统配置

    这篇文章介绍了ASP.NET Core通用主机系统配置的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07

最新评论