简体   繁体   中英

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

ihave a website developed by using asp.net

In ther there is a secition to upload images. So when they do it I want to add a watermark to it

So to do that I am using this code

 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);
        }

But when I save the image, watermark is there but not the image

enter image description here

What is the error here?

Just remove the line gr.Clear , and when you declare the cpy variable, use the source image in constructor.

Here is your code working:

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);
}

Looking at Bitmap class documentation , you have a constructor that accept a image as parameter ( source ), that:

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

So, your Bitmap ( cpy ) is now a "copy" of the source image. Now, you don't need to clear anything, and can put your watermark.

The main problem in your code is because you're loading the source image, but was not doing anything.

If, for any reason, you want to use the Clear , after that, use the DrawImage method.

Here a very short version of your code to insert a watermark on a image:

// 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM