C # Add watermarks to images based on two requirements,
Use Cases
1. This is often used by everyone. It is generally four corners of an image, which are added based on the horizontal and vertical coordinates.
2. Text is always centered in the image based on a fixed position. At first, I used the first scenario to calculate coordinates based on the length of the watermark Chinese characters. Later, I found that the method was always unreliable. Now, draw a rectangle in the fixed area of the image (watermark area), add the watermark Chinese characters in the rectangle, and use the painting brush to keep the text centered.
Scenario 1 Effect
Scenario 2 Effect
Scenario 1 Add a watermark
Static void addWatermarkText (Graphics picture, int fontsize, string _ watermarkText, string _ watermarkPosition, int _ width, int _ height) {int [] sizes = new int [] {32, 14, 12, 10, 8, 6, 4}; Font crFont = null; SizeF crSize = new SizeF (); crFont = new Font ("", fontsize, FontStyle. bold); crSize = picture. measureString (_ watermarkText, crFont); float xpos = 0; float ypos = 0; Color color = Color. firebrick; switch (_ watermarkPosition) {case "WM_TOP_LEFT": xpos = (float) _ width * (float ). 01) + (crSize. width/2); ypos = (float) _ height * (float ). 01; break; case "WM_TOP_RIGHT": xpos = (float) _ width * (float ). 99)-(crSize. width/2); ypos = (float) _ height * (float ). 01; break; case "WM_BOTTOM_RIGHT": xpos = (float) _ width * (float ). 99)-(crSize. width/2); ypos = (float) _ height * (float ). 99)-crSize. height; break; case "WM_BOTTOM_LEFT": xpos = (float) _ width * (float ). 01) + (crSize. width/2); ypos = (float) _ height * (float ). 99)-crSize. height; break;} StringFormat StrFormat = new StringFormat (); StrFormat. alignment = StringAlignment. center; SolidBrush semiTransBrush2 = new SolidBrush (Color. fromArgb (153, 0, 0, 0); // Add a shadow picture. drawString (_ watermarkText, crFont, semiTransBrush2, xpos + 1, ypos + 1, StrFormat); SolidBrush semiTransBrush = new SolidBrush (color); // Add the watermark picture. drawString (_ watermarkText, crFont, semiTransBrush, xpos, ypos, StrFormat); semiTransBrush2.Dispose (); semiTransBrush. dispose ();}
Scenario 2: Add a watermark
Static void addWatermarkText (Graphics picture, string type, int fontsize, string _ watermarkText) {// 1. Draw a rectangle RectangleF drawRect; Color color; if (type = "Top ") {fig = new RectangleF (73,135,450, 64); color = Color. fromArgb (255,255,255);} else {drawRect = new RectangleF (194,245,250, 39); color = Color. fromArgb (244,226, 38);} // 2. Draw the watermark text Font crFont = null Based on the rectangle; StringFormat StrFormat = new StringFormat (); StrFormat. alignment = StringAlignment. center; crFont = new Font ("", fontsize, FontStyle. bold); SolidBrush semiTransBrush = new SolidBrush (color); // Add the watermark picture. drawString (_ watermarkText, crFont, semiTransBrush, drawRect, StrFormat); semiTransBrush. dispose ();}
Summary
Compared with the first method, the second method is more intuitive and concise. You only need to calculate the fixed coordinates on the image you want to add the watermark, and then draw a rectangle first, then, draw the watermark Chinese characters in the rectangle so that no matter how the watermark Chinese characters change, they can be centered at the fixed position of the image.