Click here to Skip to main content
16,017,922 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use class from this article Using Trigonometry and Pythagoras to WaterMark an Image[^]
All work good, but after applying watermark size of image increase greatly. If input file have size 144 kb - in ouptut it increase to 640kb. Why it so?
C#
public class WaterMark {
  private string waterMarkText;
  private string fontName;
  private FontStyle fontStyle;
  private Color color;
  private int maxFontSize;

  public WaterMark(string waterMarkText,
   string fontName, int maxFontSize,
   FontStyle fontStyle, Color color,
   byte alpha) {
   this.waterMarkText = waterMarkText;
   this.fontName = fontName;
   this.fontStyle = fontStyle;
   this.color = Color.FromArgb(alpha, color);
   this.maxFontSize = maxFontSize;
  }

  public Bitmap Apply(string url) {
   Bitmap bmp;
   using(Bitmap newBitmap = new Bitmap(url))
   using(Graphics g = Graphics.FromImage(newBitmap)) {
    double tangent = (double) newBitmap.Height /
     (double) newBitmap.Width;


    double angle = Math.Atan(tangent) * (180 / Math.PI);

    double halfHypotenuse = Math.Sqrt((newBitmap.Height * newBitmap.Height) +
     (newBitmap.Width *
      newBitmap.Width)) / 2;

    StringFormat stringFormat = new StringFormat();
    stringFormat.Alignment = StringAlignment.Center;
    stringFormat.LineAlignment = StringAlignment.Center;

    Font font = new Font(fontName, maxFontSize,
     fontStyle);
    for (int i = maxFontSize; i > 0; i--) {
     font = new Font(fontName, i, fontStyle);
     SizeF sizef = g.MeasureString(waterMarkText,
      font, int.MaxValue);

     double sin = Math.Sin(angle * (Math.PI / 180));
     double cos = Math.Cos(angle * (Math.PI / 180));

     double opp1 = sin * sizef.Width;
     double adj1 = cos * sizef.Height;

     double opp2 = sin * sizef.Height;
     double adj2 = cos * sizef.Width;

     if (opp1 + adj1 < newBitmap.Height &&
      opp2 + adj2 < newBitmap.Width) {
      break;
     }
    }

    g.SmoothingMode = SmoothingMode.None;
    g.RotateTransform((float) angle);
    g.DrawString(waterMarkText, font,
     new SolidBrush(color),
     new Point((int) halfHypotenuse, 0),
     stringFormat);

    bmp = new Bitmap(newBitmap);
    newBitmap.Dispose();

   }

   return bmp;
  }

 }


What I have tried:

I try use this code but its not help to me.
Posted
Updated 4-Nov-16 23:47pm

1 solution

Since you don't show us where you save the file, we can't be sure what format you are saving it in - and that makes a huge difference to the file size and is likely to be related to your problem.
If for example, the input file is a JPG and you save it as a BMP, then it will grow in size dramatically, even if you don't change the image - because JPG is a compressed format, and BMP is not.

So start by looking at how you save it - read the image, do not change it at all, and then save it under a new name - compare the sizes, and if they are still as wildly different, then check the file types.

And do be aware that if you are editing JPG files, you will lose image quality every time you save it, as JPG (as well as some other formats) is a lossy compression, which discards information. 5 or 6 edit-save iterations can destroy an image almost completely if you aren't careful.
 
Share this answer
 

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