C # Image Watermarking examples and code,

Source: Internet
Author: User

C # Image Watermarking examples and code,

The class to be provided in this Article can add text watermarks to images and determine whether it is an image file. Tested to run, example download: http://hovertree.com/h/bjaf/5qc5eh6y.htm

Example:

The following is the code of the HovercWarter class:

1 using System. drawing; 2 using System. drawing. imaging; 3 using System. IO; 4 5 namespace HoverTreeBatch. hovercFrame 6 {7 public class HovercWarter 8 {9 public static Image AddTextToImg (Image image, string text) 10 {11 Bitmap bitmap = new Bitmap (image, image. width, image. height); 12 Graphics g = Graphics. fromImage (bitmap); 13 14 float fontSize = 12.0f; // font size 15 float textWidth = text. length * fontSize; // the Length of the text is 16 // The following defines a rectangular area, and then draws a white-bottom black character 17 float rectX = 0; 18 float rectY = 0; 19 float rectWidth = text. length * (fontSize + 8); 20 float rectHeight = fontSize + 8; 21 // declare the Rectangular Domain 22 RectangleF textArea = new RectangleF (rectX, rectY, rectWidth, rectHeight ); 23 24 Font font = new Font ("", fontSize); // defines the Font 25 Brush whiteBrush = new SolidBrush (Color. white); // White Brush. Use 26 Brush blackBrush = new SolidBrush (Color. black); // Black brush, with a background of 27 28 GB. fillRectangle (blackBrush, rectX, rectY, rectWidth, rectHeight); 29 30g. drawString (text, font, whiteBrush, textArea); 31 MemoryStream MS = new MemoryStream (); 32 // save as Jpg type 33 bitmap. save (MS, ImageFormat. jpeg); 34 35 Image h_hovercImg = Image. fromStream (MS); 36 37g. dispose (); 38 bitmap. dispose (); 39 40 41 return h_hovercImg; 42} 43 44 45 // <summary> 46 // determine the type of the uploaded file based on the file header 47 /// </summary> 48 // <param name = "filePath "> filePath is the complete file path </param> 49 // <returns> returns true or false </returns> 50 public static bool IsPicture (string filePath) 51 {52 try53 {54 FileStream fs = new FileStream (filePath, FileMode. open, FileAccess. read); 55 BinaryReader reader = new BinaryReader (fs); 56 string fileClass; 57 byte buffer; 58 buffer = reader. readByte (); 59 fileClass = buffer. toString (); 60 buffer = reader. readByte (); 61 fileClass + = buffer. toString (); 62 reader. close (); 63 fs. close (); 64 if (fileClass = "255216" | fileClass = "7173" | fileClass = "13780" | fileClass = "6677 ") 65 // ask hovertree. com66 // 255216 is jpg; 7173 is gif; 6677 is BMP, 13780 is PNG; 7790 is exe, 8297 is rar 67 {68 return true; 69} 70 else71 {72 return false; 73} 74} 75 catch76 {77 return false; 78} 79} 80} 81}

Another. NET question: http://hovertree.com/shortanswer/bjaf/9vqxwuda.htm

Development Technology Article collection: http://www.cnblogs.com/sosoft/p/kaifajishu.html

Related Article

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.