C # Two different scenarios and solutions for adding a watermark to a picture _c# tutorial

Source: Internet
Author: User

Scene One

That is, we often use, is generally the picture of the 4 corners, based on horizontal ordinate to add.

The effect is as follows:

Add Watermark Method

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 ("Microsoft Ya Hei", 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).) + (CRSIZE.WIDTH/2);
     Ypos = (float) _height * (float). 01;
    Break
     Case "Wm_top_right": Xpos = ((float) _width * (float)-(CRSIZE.WIDTH/2);
     Ypos = (float) _height * (float). 01;
    Break
     Case "Wm_bottom_right": Xpos = ((float) _width * (float)-(CRSIZE.WIDTH/2);
     Ypos = ((float) _height * (float)-crsize.height;
    Break Case "Wm_bottom_left": Xpos = ((float) _width * (float) + (crsize.wiDTH/2);
     Ypos = ((float) _height * (float)-crsize.height;
     
   Break
   } StringFormat Strformat = new StringFormat ();
   Strformat.alignment = Stringalignment.center; SolidBrush semiTransBrush2 = new SolidBrush (Color.FromArgb (153, 0, 0, 0))//Add shadow picture.
 
   DrawString (_watermarktext, Crfont, SEMITRANSBRUSH2, xpos + 1, ypos + 1, strformat); SolidBrush Semitransbrush = new SolidBrush (color); Add a watermark to the picture.
 
   DrawString (_watermarktext, Crfont, Semitransbrush, xpos, ypos, Strformat);
   Semitransbrush2.dispose ();
  Semitransbrush.dispose (); }

Scene Two

The text is always centered within the picture, based on a fixed position. In the beginning, I based on the first scenario to compute the coordinates based on the length of the watermark characters, and later found that the method was always unreliable. Now it's time to draw a rectangle in the picture's fixed area (the Watermark area), and then add the watermark characters to the rectangle, and use the brush to keep the text centered.

The effect chart is as follows

How to add a watermark

static void Addwatermarktext (Graphics picture,string type, int fontsize, string _watermarktext)
  {
   //1, first draw rectangle
   RectangleF DrawRect;
   Color color;
   if (type = = "Top")
   {
     drawrect = new RectangleF (135, 450,);
     color = Color.FromArgb (255, 255, 255);
   }
   else
   {
     drawrect = new RectangleF (194, 245,);
     color = Color.FromArgb (244, N.);
    
   2, based on the rectangular watermark text
   Font crfont = null;
 
   StringFormat Strformat = new StringFormat ();
   Strformat.alignment = Stringalignment.center;
 
   Crfont = new Font ("Microsoft Ya Hei", FontSize, fontstyle.bold);
   SolidBrush Semitransbrush = new SolidBrush (color); Add a watermark to the picture
   . DrawString (_watermarktext, Crfont, Semitransbrush, DrawRect, Strformat);
 
   Semitransbrush.dispose ();
  }

Summarize

Compared to the first method, the second method is more intuitive, more concise, you need to add a watermark on the picture to calculate the fixed coordinates and then draw a rectangle, and then the watermark characters in the rectangle, so no matter how the watermark changes can be in the picture fixed position Center. The above is the entire content of this article, I hope to be able to study or work to bring certain help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.