日期:2014-05-17  浏览次数:21024 次

给图片加水印为什么加不上去呢??
public void ProcessRequest(HttpContext context)
  { string path = context.Request.PhysicalPath;
  string defaultPath = context.Server.MapPath("~/Images/default.jpg");
  string watherPath = context.Server.MapPath("~/Images/WaterMark.jpg");
  Image cover;
  if (File.Exists(path))
  {
  Image water = Image.FromFile(watherPath);
  cover= Image.FromFile(path);
  Graphics g = Graphics.FromImage(cover);
  g.DrawImage(water, cover.Width - water.Width, cover.Height - water.Height, water.Width, water.Height);
  g.Dispose();}
  else 
  {cover = Image.FromFile(defaultPath);}
  context.Response.ContentType = "image/jpeg";
  cover.Save(context.Response.OutputStream, ImageFormat.Jpeg);
  cover.Dispose();
  context.Response.End();
  }

------解决方案--------------------
C# code

            string path = context.Request.PhysicalPath;
            string water = context.Server.MapPath("~/image/water.jpg");
            Image img = null;
            if (!File.Exists(path))
            {
                string defaultPath = context.Server.MapPath("~/image/default.gif");
                img = Image.FromFile(defaultPath);
            }
            else
            {
                img = Image.FromFile(path);
                Image waterImage = Image.FromFile(water);
                Graphics g = Graphics.FromImage(img);
                g.DrawImage(waterImage, new Rectangle(img.Width - waterImage.Width, img.Height - waterImage.Height, waterImage.Width, waterImage.Height), new Rectangle(0, 0, waterImage.Width, waterImage.Height), GraphicsUnit.Pixel);
                waterImage.Dispose();
                g.Dispose();
            }
            img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
            img.Dispose();
            context.Response.ContentType = "image/jpeg";
            context.Response.End();