zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

NetASP 实现 如何 图片 文字 具体 加水
2023-06-13 09:15:13 时间

第一步,添加一个一般处理程序(Handler),本例是ImageHandler

复制代码代码如下:


usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.Net.Mime;
usingSystem.IO;
usingSystem.Drawing;
usingSystem.Drawing.Imaging;
usingSystem.Drawing.Drawing2D;

///<summary>
///SummarydescriptionforImageHandler
///</summary>
publicclassImageHandler:IHttpHandler
{
   publicImageHandler()
   {
   }

   publicstringGetContentType(Stringpath)
   {
       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;
       }
       returnString.Empty;
   }

   publicImageFormatGetImageFormat(Stringpath)
   {
       switch(Path.GetExtension(path).ToLower())
       {
           case".bmp":returnImageFormat.Bmp;
           case".gif":returnImageFormat.Gif;
           case".jpg":returnImageFormat.Jpeg;
           case".png":returnImageFormat.Png;
           default:returnnull;
       }
   }

   protectedbyte[]WatermarkImage(HttpContextcontext)
   {

       byte[]imageBytes=null;
       if(File.Exists(context.Request.PhysicalPath))
       {
           //Normallyyou"dputthisinaconfigfilesomewhere.
           stringwatermark="世复检测";

           Imageimage=Image.FromFile(context.Request.PhysicalPath);

           Graphicsgraphic;
           if(image.PixelFormat!=PixelFormat.Indexed&&image.PixelFormat!=PixelFormat.Format8bppIndexed&&image.PixelFormat!=PixelFormat.Format4bppIndexed&&image.PixelFormat!=PixelFormat.Format1bppIndexed)
           {
               //GraphicisnotaIndexed(GIF)image
               graphic=Graphics.FromImage(image);
           }
           else
           {
               /*Cannotcreateagraphicsobjectfromanindexed(GIF)image.
                *Sowe"regoingtocopytheimageintoanewbitmapso
                *wecanworkwithit.*/
               BitmapindexedImage=newBitmap(image);
               graphic=Graphics.FromImage(indexedImage);

               //Drawthecontentsoftheoriginalbitmapontothenewbitmap.
               graphic.DrawImage(image,0,0,image.Width,image.Height);
               image=indexedImage;
           }
           graphic.SmoothingMode=SmoothingMode.AntiAlias&SmoothingMode.HighQuality;

           FontmyFont=newFont("Arial",15);
           SolidBrushbrush=newSolidBrush(Color.FromArgb(255,Color.Red));

           /*Thisgetsthesizeofthegraphicsowecandetermine
            *theloopcountsandplacementofthewatermarkedtext.*/
           SizeFtextSize=graphic.MeasureString(watermark,myFont);

           ////Writethetextacrosstheimage.
           //for(inty=0;y<image.Height;y++)
           //{
           //   for(intx=0;x<image.Width;x++)
           //   {
           //       PointFpointF=newPointF(x,y);
           //       graphic.DrawString(watermark,myFont,brush,pointF);
           //       x+=Convert.ToInt32(textSize.Width);
           //   }
           //   y+=Convert.ToInt32(textSize.Height);
           //}


           //Writethetextattherightbottomoftheimage.
           for(inty=image.Height-25;y<image.Height;y++)
           {
               for(intx=image.Width-100;x<image.Width;x++)
               {
                   PointFpointF=newPointF(x,y);
                   graphic.DrawString(watermark,myFont,brush,pointF);
                   x+=Convert.ToInt32(textSize.Width);
               }
               y+=Convert.ToInt32(textSize.Height);
           }

           using(MemoryStreammemoryStream=newMemoryStream())
           {
               image.Save(memoryStream,GetImageFormat(context.Request.PhysicalPath));
               imageBytes=memoryStream.ToArray();
           }

       }
       returnimageBytes;
   }

   #regionIHttpHandlerMembers

   publicboolIsReusable
   {
       get{returnfalse;}
   }

   publicvoidProcessRequest(HttpContextcontext)
   {
       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
       {
           //Nobytes=noimagewhichequalsNOFILE.   
           //Thereforesenda404-notfoundresponse.
           context.Response.StatusCode=404;
       }
       context.Response.End();
   }

   #endregion
}

第二步,在web.config里添加如下代码:

复制代码代码如下:


   <httpHandlers>
     <!--<addverb="GET"type="ImageHandler"path="*.jpg,*.png,*.gif,*.bmp"/>-->
     <addverb="GET"type="ImageHandler"path="Uploads/*/*.jpg"/>     
   </httpHandlers>