尝试向其添加水印时图像变暗。如何解决?

Image getting darker when try to add a watermark to it. How to solve it?

我有一个使用 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

这里有什么错误?

只需删除行 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, you have a constructor that accept a image as parameter (source),即:

Initializes a new instance of the Bitmap class from the specified existing image.

因此,您的位图 (cpy) 现在是源图像的 "copy"。 现在,你不需要清除任何东西,并且可以添加你的水印。

您代码中的主要问题是因为您正在加载源图像,但没有执行任何操作。

如果您出于任何原因想要使用 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);
}