当前位置:  编程技术>.net/c#/asp.net

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

    来源: 互联网  发布时间:2014-08-25

    本文导语:  第一步,添加一个一般处理程序(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 Syste...

第一步,添加一个一般处理程序(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 description for ImageHandler
///
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里添加如下代码:

代码如下:

   
     
           
   

    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 在ASP中可以轻易实现,不知道在JSP中能否实现?
  • Linux平台下哪种方法实现ASP好?
  • 求解!Java如何能在多个applet之间实现类似asp的session类!
  • 调试jsp的时如何实现像asp中的response.end的效果
  • 怎么用能让asp实现统计在线人数的功能。在线等待!!!帮忙送分!
  • 在jsp 中如何实现像asp 中的页面弹出式的菜单
  • asp.net 参数不同共用一个页面的实现方法
  • Asp.net防止重复提交的实现方法
  • 用linux做服务器运行ASP.net网站,请问具体怎样实现??
  • ASP.NET MVC3 实现全站重定向的简单方法
  • 在jsp中如何实现与asp中的Request.ServerVariables("SCRIPT_NAME")同等的功能?
  • Jsp中怎么实现类似ASP中split函数的功能?
  • 如何实现ASP中类似Global.asa的功能???
  • jsp中如何实现asp中的response.end,或php中的exit()功能?
  • java实现的asp服务器 OpenASP
  • ASP的功能在JSP中怎么实现呀?谢谢!!!
  • asp去掉html,保留img br p div的正则实现代码
  • asp.net 实现php的md5()函数功能
  • asp.net session实现用户登录的疑问
  • 怎样将Tomcat嵌入IIS中,实现用ASP与JSP都用IIS来做!
  • ASP.NET之 Ajax相关知识介绍及组件图
  • 我想了解一些关于Java怎样与Asp或Asp.net结合方面在未来发展方向的问题?
  • c#/ASP.NET操作cookie(读写)代码示例
  • asp.net UrlEncode对应asp urlencode的处理方法
  • asp.net实例 定义和使用asp:AccessDataSource
  • win2008 r2 服务器环境配置(FTP/ASP/ASP.Net/PHP)
  • asp与asp.net的session共享
  • 如何在unix下发布asp?
  • 怎么让Apache支持Asp?
  • ??谁能把ASP代码改为JSP的
  • ASP和ASP.Net共享Session解决办法


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3