您的位置:首页 > 编程语言 > ASP

ASP.NET 给图片打水印

2010-06-10 06:22 134 查看
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);
               }
           }
       }
   }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: