Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have and image with text to make sure that the text at the bottom of the image doesn't get covered by my water mark text, I want to make the new image larger --> water mark it --> save

What I have tried:

Here is my method, the new image "tempBitmap" does have the new size because when i go to image properties the original image size is less than the new one. However, the water mark text still covers the image text

public void waterMark(string text, string sourcePath, string destinationPathh)
       {
           using (Bitmap bitmap = new Bitmap(sourcePath))
           {
               Bitmap tempBitmap = new Bitmap((bitmap.Width + 30), (bitmap.Height + 30));//added +30 to make image larger
               using (Graphics graphics = Graphics.FromImage(tempBitmap))
               {
                   graphics.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height);
                   Brush brush = new SolidBrush(Color.Black);
                   Font font = new Font("Arial", 50, FontStyle.Italic, GraphicsUnit.Pixel);
                   SizeF textSize = new SizeF();
                   textSize = graphics.MeasureString(text, font);
                   Point position = new Point(bitmap.Width - ((int)textSize.Width + 200), bitmap.Height - ((int)textSize.Height + 10));// played around with the +10 to locate the water mark no luck.
                   graphics.DrawString((text), font, brush, position);
                   tempBitmap.Save(destinationPathh, ImageFormat.Tiff);
               }
           }
       }

thank you.
Posted
Updated 16-Jan-20 23:20pm

1 solution

This version gets the text dimensions first and creates a new bitmap with the height taller than the text width + 10 (don't have to change the width). When positioning the text it puts it 5 below the original height so centering it in the new space below, and centres it horizontally. You'd need to do additional checks like ensuring the text isn't wider than the image, maybe reducing the font size if it is.

C#
using (Bitmap bitmap = new Bitmap(sourcePath))
{
    Brush brush = new SolidBrush(Color.Black);
    Font font = new Font("Arial", 50, FontStyle.Italic, GraphicsUnit.Pixel);

    Bitmap tempBitmap = new Bitmap(bitmap.Width, bitmap.Height);
    Graphics tempGraphics = Graphics.FromImage(tempBitmap);
                
    SizeF textSize = tempGraphics.MeasureString(text, font);

    tempBitmap = new Bitmap(bitmap.Width, bitmap.Height + (int)textSize.Height + 10);

    using (Graphics graphics = Graphics.FromImage(tempBitmap))
    {
        graphics.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height);
        Point position = new Point((int)((bitmap.Width - textSize.Width) / 2), bitmap.Height + 5);
        graphics.DrawString((text), font, brush, position);
        tempBitmap.Save(destinationPathh, ImageFormat.Tiff);
    }
}
 
Share this answer
 
Comments
AskalotLearnalot 20-Jan-20 10:19am    
Here is the answer with little edits that work. thanks for the help.

            using (Bitmap bitmap = new Bitmap(sourcePath))
            {
                Brush brush = new SolidBrush(Color.Black);
                Font font = new Font("Arial", 50, FontStyle.Italic, GraphicsUnit.Pixel);
                Bitmap tempBitmap = new Bitmap(bitmap.Width, bitmap.Height);
                Graphics tempGraphics = Graphics.FromImage(tempBitmap);
                SizeF textSize = tempGraphics.MeasureString(text, font);
                tempBitmap = new Bitmap(bitmap.Width, bitmap.Height + (int)textSize.Height + 10);
                tempBitmap.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution);
                using (Graphics graphics = Graphics.FromImage(tempBitmap))
                {
                    graphics.FillRectangle(Brushes.White, 0, 0, bitmap.Width, bitmap.Height + 100);
                    graphics.DrawImage(bitmap,0,0 , bitmap.Width, bitmap.Height );
                    Point position = new Point(bitmap.Width - ((int)textSize.Width + 200), bitmap.Height+5);
                    
                    graphics.DrawString((text), font, brush, position);
                    
                    tempBitmap.Save(destinationPathh, ImageFormat.Tiff);

                 
                }
            }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900