asp.net如何在圖片上加浮水印文字具體實現

來源:互聯網
上載者:User

 這篇文章主要介紹了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>  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.