ASP.NET 给图片打水印

public void AddWaterOnPicture(string srcImagePath)
   {
       string waterImagePath = Server.MapPath("~/skin/mainaer/images/water_logo.png");
       if (!File.Exists(waterImagePath))
           return;
       if (!File.Exists(srcImagePath))
           return;
       string backFileName = Path.GetDirectoryName(srcImagePath) + "/" + Path.GetFileNameWithoutExtension(srcImagePath) + "__back"+Path.GetExtension(srcImagePath );

       File.Copy(srcImagePath, backFileName, true);

       using (System.Drawing.Image image = System.Drawing.Image.FromFile(backFileName))
       {
           using (System.Drawing.Image wimg = System.Drawing.Image.FromFile(waterImagePath))
           {
               if (wimg.Height >= image.Height / 2)
                   return;
               if (wimg.Width >= image.Width / 2)
                   return;
               using (Bitmap bitmap = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb))
               using (Graphics picture = Graphics.FromImage(bitmap))
               {
                   picture.Clear(Color.Green);
                   picture.SmoothingMode = SmoothingMode.HighQuality;
                   picture.InterpolationMode = InterpolationMode.High;
                   picture.DrawImage(image, 0, 0, image.Width, image.Height);
                   int x = image.Width / 2 - wimg.Width / 2;
                   int y = image.Height / 2 - wimg.Width / 2;
                   picture.DrawImage(wimg, new Point(x, y));
                   bitmap.Save(srcImagePath);
               }
           }
       }
   }

posted on 2010-06-10 06:22  老代哥哥  阅读(143)  评论(0编辑  收藏  举报

导航