Search code examples
c#asp.netwatermark

Watermark image doesn't work, get Blank image


I am trying to apply a watermark text over an image in ASP.NET. I want to use Image stream instead of using / writing to local files.

Here's the code:

 using (MemoryStream ms_small = new MemoryStream())
  {

       System.Drawing.Image image_small = System.Drawing.Image.FromStream(filestream); 
       width = image_small.Width;
       height = image_small.Height;


       string Copyright = "Hi I am copyright watermark";

       Graphics grPhoto = Graphics.FromImage(image_small);

       int phWidth = image_small.Width;
       int phHeight = image_small.Height;

        grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

       int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };
       Font crFont = null;
       SizeF crSize = new SizeF();


       for (int i = 0; i < 7; i++)
       {
         crFont = new Font("arial", sizes[i], FontStyle.Bold);
         crSize = grPhoto.MeasureString(Copyright, crFont);
         if ((ushort)crSize.Width < (ushort)phWidth)
            break;
       }

       int yPixlesFromBottom = (int)(phHeight * .05);

       float yPosFromBottom = ((phHeight - yPixlesFromBottom) - (crSize.Height / 2));

       float xCenterOfImg = (phWidth / 2);


       StringFormat StrFormat = new StringFormat();
       StrFormat.Alignment = StringAlignment.Center;


       SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));


        grPhoto.DrawString(Copyright,             
               crFont,                                  
               semiTransBrush2,                         
               new PointF(xCenterOfImg + 1, yPosFromBottom + 1),
                            StrFormat);

            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
            grPhoto.DrawString(Copyright,            
                 crFont,                           
                 semiTransBrush,                          
                 new PointF(xCenterOfImg, yPosFromBottom), 
                 StrFormat);                      

                   Bitmap outputBitMap = new Bitmap(image_small.Width, image_small.Height, grPhoto);
                   image_small = (Image)outputBitMap;
                   ImageBuilder.Current.Build(image_small, ms_small, rs_small_jpg);

               }

The watermark code is from here.

Again, what I want to achieve is a watermark text on top of the image and without writing the image locally or using temporary local image file to apply the watermark. I am getting a blank image instead of the image with the watermark.


Solution

  • Your problem was in used graphics for image in end of method. Instead this, you must use original image, beacause all graphics were drawing in it.

    Replace this string:

    Bitmap outputBitMap = new Bitmap(image_small.Width, image_small.Height, grPhoto);
    

    With this:

    Bitmap outputBitMap = new Bitmap(imageSmall);
    

    Good luck!