asp.net how to add the watermark text to the picture to realize

Source: Internet
Author: User
Tags config httpcontext

  This article mainly introduces the ASP.net how to add watermark text to the picture to achieve, the need for friends can refer to the

The first step is to add a generic handler (Handler), in this case ImageHandler   code as follows: 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:ihttp Handler {    public imagehandler ()     {   }       public string Getconten Ttype (String path)     {        switch (path.getextension (path))       &NBSP ; {            case '. bmp ': Return "image/bmp";             CAs E ". gif": Return "Image/gif";             case ". jpg": Return "Image/jpeg";       &NBsp     case ". png": Return "Image/png";             Default:break;        }         return String.Empty;    }       public imageformat Getimageformat (String path)     {      & nbsp Switch (path.getextension (Path). ToLower ())         {            case '. bmp ': Return imageformat.bmp &N Bsp           case ". gif": return imageformat.gif;             case ". jpg": return imageformat.jpeg;             case ". png": Return imageformat.png;             Default:return null;        }     {      protected byte[] Watermarkimage (HttpContext context) &N Bsp   {          byte[] imagebytes = null;         if (File.exists (context. Request.PhysicalPath)         {           //Normally you ' d put this In a config file somewhere.             String watermark = "World complex Detection";               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)             {        &NBS P      //Graphic is not a Indexed (GIF) image                 graph IC = graphics.fromimage (image);                         Else             {               /* cannot Create a Graphics object from a indexed (GIF) image.                  &NBSP ; * So we ' re going to copy the image into a new bitmap so                  &NB Sp;* we can work with it. *                 Bitmap indexedimage = new Bitmap (image);                 graphic = Graphics.fromimage (indexedimage);                  //Draw the contents of the original bitmap onto the new bi tmap.                  graphic. DrawImage (image, 0, 0, image. Width, image. Height);                 image = Indexedimage;                       &NBsp Graphic. SmoothingMode = Smoothingmode.antialias & smoothingmode.highquality;               Font myfont = new Font ("Arial", 15);             SolidBrush brush = new SolidBrush (Color.FromArgb (255, color.red));              /* Gets the size of the graphic so we can determine    &NB Sp          * the loop counts and placement of the watermarked text. *             SizeF textsize = graphic. MeasureString (watermark, myfont);              ////Write the text across the image.          & nbsp  //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);            //  &NBSP,            //   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 = Imag e.width-100; x < image. Width; x + +)                 {            &NBsp       PointF PointF = new PointF (x, y);                     graphic. DrawString (watermark, myfont, Brush, PointF);                     x + + Convert.ToInt32 (textsize.width);                                 y = Con Vert. ToInt32 (Textsize.height);                           using (MemoryStream memorystr EAM = new MemoryStream ())             {              &NB Sp Image. Save (MemoryStream, Getimageformat (context). Request.PhysicalPath));                 imagebytes = Memorystream.toarray ();                               return IMAGEB Ytes;    }       #region IHttpHandler members       public bool IsReusable     {  &NB Sp     get {return false;}    }       public void ProcessRequest (HttpContext context) & nbsp   {        context. Response.Clear ();         context. Response.ContentType = getContentType (context. Request.PhysicalPath);         byte[] Imagebytes = watermarkimage (context);         if (imagebytes!= null)         {          &NBSP ; Context. Response.OutputStream.Write (imagebytes, 0, imagebytes.length);        }         else         {        & nbsp  /No bytes = no image which equals no FILE.                //Therefore send a 404-not found response.        &nbsp     Context. Response.statuscode = 404;        }         context. Response.End ();    }       #endregion}       Second step, add the following code in Web.config:   code as follows:     <httpHandlers>       <!--<add verb= "Get" type= "ImageHandler path=" *.jpg,*.png,*.gif,*.bmp "/>-->       <add verb=" get "type=" ImageHandler "path=" uploads/*/*.jpg "/>           </httpHandlers>    
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.