尝试为其添加水印时图像变暗。怎么解决?

时间:2017-01-30 17:43:02

标签: c# asp.net graphics watermark

我有一个使用asp.net开发的网站

此外还有一个上传图像的分区。所以当他们这样做时,我想给它添加一个水印

为此我要使用此代码

 using (var image = System.Drawing.Image.FromStream(sourcePath))
        {
            int nnx = image.Width;
            int nny = image.Height;
            Bitmap cpy = new Bitmap(nnx, nny, PixelFormat.Format32bppArgb);

            Bitmap watermarkImage = new Bitmap(@"D:\Contact.png");

            using (Graphics gr = Graphics.FromImage(cpy))
            {
                gr.Clear(Color.Transparent);

                // This is said to give best quality when resizing images
                gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                gr.CompositingQuality = CompositingQuality.HighQuality;
                gr.SmoothingMode = SmoothingMode.HighQuality;
                gr.PixelOffsetMode = PixelOffsetMode.HighQuality;

                using (TextureBrush watermarkBrush = new TextureBrush(watermarkImage))
                {
                    int x = (cpy.Width / 2 - watermarkImage.Width / 2);
                    int y = (cpy.Height / 2 - watermarkImage.Height / 2);
                    watermarkBrush.TranslateTransform(x, y);
                    gr.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(watermarkImage.Width + 1, watermarkImage.Height)));
                }
                //gr.DrawImage(image,
                //    new Rectangle(0, 0, nnx, nny),
                //    new Rectangle(0, 0, image.Width, image.Height),
                //    GraphicsUnit.Pixel);
            }
            fullImagePath = string.Format("/Images/full/{0}", imageFileName);

            cpy.Save(Server.MapPath(fullImagePath), image.RawFormat);
        }

但是当我保存图像时,水印就在那里而不是图像

enter image description here

这里有什么错误?

1 个答案:

答案 0 :(得分:0)

只需删除行gr.Clear,当您声明cpy变量时,请在构造函数中使用源图像。

以下是您的代码:

using (Image image = System.Drawing.Image.FromStream(sourcePath))
{
  Bitmap cpy = new Bitmap(image);
  Bitmap watermarkImage = new Bitmap(@"D:\Contact.png");

  using (Graphics gr = Graphics.FromImage(cpy))
  {
    gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
    gr.CompositingQuality = CompositingQuality.HighQuality;
    gr.SmoothingMode = SmoothingMode.HighQuality;
    gr.PixelOffsetMode = PixelOffsetMode.HighQuality;

    using (TextureBrush watermarkBrush = new TextureBrush(watermarkImage))
    {
      int x = (cpy.Width / 2 - watermarkImage.Width / 2);
      int y = (cpy.Height / 2 - watermarkImage.Height / 2);
      watermarkBrush.TranslateTransform(x, y);
      gr.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(watermarkImage.Width + 1, watermarkImage.Height)));
    }
  }

  var fullImagePath = string.Format("/Images/full/{0}", imageFileName);
  cpy.Save(Server.MapPath(fullImagePath), image.RawFormat);
}

查看Bitmap class documentation,您有一个接受图像作为参数(source)的构造函数,即:

  

从指定的位置初始化Bitmap类的新实例   现有形象。

因此,您的位图(cpy)现在是源图像的“副本”。 现在,你不需要清除任何东西,并且可以放置你的水印。

代码中的主要问题是因为您正在加载源图像,但没有做任何事情。

如果您出于任何原因想要使用Clear,请使用DrawImage方法。

这是一个非常简短的代码版本,用于在图像上插入水印:

// open the image
using (Image image = Image.FromFile(@"c:\sourceimage.jpg"))
{
  // create a new bitmap with original image
  Bitmap imageWithWatermark = new Bitmap(image);

  using (Graphics gr = Graphics.FromImage(imageWithWatermark))
  {
    // draw the watermark
    gr.DrawImage(Image.FromFile(@"c:\watermark.png"), new Point(0, 0));
  }

  imageWithWatermark.Save(@"c:\destimage.jpg", ImageFormat.Jpeg);
}