/ * Description: Generate watermark, can generate files on top left, lower left, upper right, center, transparency, only valid for JPEG or JPG format! Author: Blue.Dream statement: I recently discovered that many website quoted the article, saying that the author information are omitted, please reference the author of this site do not omit author of * / using System; System using; using System.Drawing. . Drawing.Imaging; using system.ComponentModel; use system.windows.form; using system.data; using system.io;
Namespace Test {////
/// Form1 summary description. ///
Public class form1: system.windows.Forms.form {// Declares the number of bits of the random number generated by the encrypted character PUBLIC const INT length = 32; // Declare the separation length of the encrypted character PUBLIC Const Int Len = 4; private system. Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.Button btn_Browser; private System.Windows.Forms.Button button2; private System.Windows.Forms.PictureBox imgSource; private System. Windows.Forms.PictureBox imgObject; private System.Windows.Forms.PictureBox imgWater; private System.Windows.Forms.Button btn_Water; private string SourceFile; private string WaterFile; private string SaveFile; private System.Windows.Forms.TextBox alpha; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.GroupBox groupBox3; private System.Windows.Forms.ComboBox local; /// /// necessary design variables . ///Prate system.componentmodel.container components = null;
Public Form1 () {// // Windows Form Designer Support for // InitializeComponent (); this.init ();
// // Todo: Add any constructor code after INITIALIZEComponent call //}
private void Init () {DataTable ht = new DataTable (); DataColumn objcol = new DataColumn ( "Keys"); objcol.DataType = typeof (System.String); ht.Columns.Add (objcol); objcol = new DataColumn ( "Value"); objcol.datatype = typeof (system.int32); ht.columns.add (objcol); DATAROW ROW = HT.NEWROW (); Row [0] = "Row"; Row [1] = 1; Ht.Rows.add (row); row = ht.newrow (); row [0] = "Row"; row [1] = 2; ht.rows.add (row); row = ht.newrow (); Row [0] = "Right"; row [1] = 3; ht.rows.add (row); row = ht.newrow (); row [0] = "Right"; ROW [1] = 4; Ht.Rows.add (row); row = ht.newrow (); row [0] = "Case"; row [1] = 5; ht.rows.add (row); this.local.dataSource = HT; THISLOCAL.DISPLAYMEMBER = "keys"; this.local.Valuemember = "value";} ///
/// Clean all the resources being used. ///
Protected Override Void Dispose (bool disposing) {if (disponents! = null) {components.dispose ();}} Base.Dispose (Disposing);
#Region Windows Form Designer generated code /////
/// Designer supports the required method - Do not use the code editor to modify the // / this method. ///
private void InitializeComponent () {this.groupBox1 = new System.Windows.Forms.GroupBox (); this.groupBox2 = new System.Windows.Forms.GroupBox (); this.btn_Browser = new System.Windows.Forms.Button () ; this.button2 = new System.Windows.Forms.Button (); this.imgSource = new System.Windows.Forms.PictureBox (); this.imgObject = new System.Windows.Forms.PictureBox (); this.imgWater = New system.windows.Forms.PictureBox (); this.btn_water = new system.windows.forms.button (); this.local = new system.windows.forms.comBobox (); this.alpha = new system.windows. Forms.TextBox (); this.label1 = new system.windows.forms.label (); this.label2 = new system.windows.forms.label (); this.groupbox3 = new system.windows.forms.groupbox () ; this.groupBox1.SuspendLayout (); this.groupBox2.SuspendLayout (); this.groupBox3.SuspendLayout (); this.SuspendLayout (); // // groupBox1 // this.groupBox1.Controls.AddRange (new System.Windows .Forms.control [] {this.imgsou RCE}); this.groupBox1.name = "groupbox1"; this.groupBox1.size = new system.drawing.size (288, 152); this.groupBox1.tabindex = 0; this.groupBox1.tabstop = false; this. Groupbox1.text = "Source Map"; // // Groupbox2 // this.groupBox2.controls.addrange (new system.windows.forms.control [] {this.imgobject}; this.groupbox2.location = new system. Drawing.point (304, 0); this.groupbox2.name = "groupbox2"; this.groupbox2.size = new system.drawing.size (352, 296); this.groupBox2.tabindex = 0; this.groupBox2.tabstop = false; this.groupbox2.text = "result map";
// // btn_Browser // this.btn_Browser.Location = new System.Drawing.Point (24, 312); this.btn_Browser.Name = "btn_Browser"; this.btn_Browser.TabIndex = 1; this.btn_Browser.Text = " Source map "; this.btn_browser.click = new system.eventhandler (this.btn_browser_click); // // Button2 // this.button2.location = new system.drawing.point (552, 312); this.button2. Name = "Button2"; this.button2.tabindex = 2; this.button2.text = "Generate"; this.button2.click = new system.eventhandler (this.button2_click); /////mgsource // this. Imgsource.location = new system.drawing.point (8, 16); this.imgsource.name = "imgsource"; this.imgsource.size = new system.drawing.size (272, 128); this.imgsource.tabindex = 0; this.imgsource.tabstop = false; // // imgobject // this.imgObject.location = new system.drawing.point (8, 16); this.imgobject.name = "imgobject"; this.imgobject.size = New System.drawing.size (336, 272); this.imgobject.tabindex = 0; this.imgobj Ect.Tabstop = false; // // imgwater // this.imgwater.location = new system.drawing.point (8, 16); this.imgwater.name = "imgwater"; this.imgwater.size = new system. Drawing.size (272, 120); this.imgwater.tabindex = 1; this.imgwater.tabstop = false; // // btn_water // this.btn_water.location = new system.drawing.point (120, 312); THIS.BTN_WATER.NAME = "btn_water"; this.btn_water.tabindex = 3; this.btn_water.text = "Watermarking"; this.btn_water.click = new system.eventhandler (this.btn_water_click);
// // local // this.local.location = new system.drawing.point (256, 312); this.local.name = "local"; this.local.size = new system.drawing.size (112, 20); this.local.tabindex = 4; this.local.text = "- Please select -"; /// alpha // this.drawing.location = new system.drawing.point (448, 312) This.alpha.Name = "alpha"; this.alpha.size = new system.drawing.size (72, 21); this.alpha.tabindex = 5; this.alpha.text = ""; /// Label1 // this.label1.location = new system.drawing.point (216, 312); this.Label1.name = "label1"; this.label1.size = new system.drawing.size (32, 23); this .label1.tabindex = 6; this.label1.text = "position"; this.label1.textalign = system.drawing.contentAlignment.middleright; // // label2 // this.label2.location = new system.drawing.point = new system.drawing.point (392, 312); this.label2.name = "label2"; this.label2.size = new system.drawing.size (48, 23); this.label2.tabindex = 6; this.Label2.Text = transparency "This.label2.textalign = system.drawing .ContentAlignment.MiddleRight; // // groupBox3 // this.groupBox3.Controls.AddRange (new System.Windows.Forms.Control [] {this.imgWater}); this.groupBox3.Location = new System.Drawing.Point ( 0, 160); this.groupBox3.name = "groupbox3"; this.groupbox3.size = new system.drawing.size (288, 136); this.groupBox3.tabindex = 7; this.groupBox3.tabstop = false; this .GroupBox3.text = "Watermark"; /// // Form1 // this.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize =
New system.drawing.size (664, 365); this.controls.addrange (new system.windows.forms.control [] {this.groupbox3, this.label1, this.alpha, this.local, this.btn_water, this .Button2, this.btn_browser, this.groupbox1, this.groupbox2, this.label2}; this.name = "form1"; this.text = "form1"; this.groupBox1.ResumeLayout (false); this.groupBox2. ResumeLayout (false); this.groupBox3.ResumeLayout (false); this.ResumeLayout (false);} #ENDREGION
///
/// The main entry point for the application. ///
[STAThread] static void Main () {Application.Run (new Form1 ());} private void btn_Browser_Click (object sender, System.EventArgs e) {using (OpenFileDialog dlg = new OpenFileDialog ()) {if (dlg.ShowDialog ( ) == DialogResult.ok) {string file = DLG.FileName; if (file.exists (file)) {this.SourceFile = file; imgsource.image = new bitmap (file);}} dlg.dispose (); }
Private void button2_click (object sender, system.eventargs e) {string wf = @ "e: /test.jpg"; this.savefile = wf; int local = (int)); int Al = 80 Try {al = int32.Pars (this.alpha.text.trim ());} catch {} if (MakewaterImage (this.sourcefile, this.waterfile, this.savefile, local, al)) {this.imgobject. Image = new bitmap (this.savefile);}}
///
/// Generate watermark, can generate files on top left, left, upper right, at, home, transparency, only valid for JPEG or JPG format! ///
/// bottom map /// Water printing /// To save file /// Location: left (1), lower left (2), upper right (3), right (4), center (5) /// Transparency (1-100) ///
bool, successful public static bool MakeWaterImage (string sourceFile, string waterMarkFile, string saveFile, int local, int alpha) {bool result; if (!! File.Exists (sourceFile) || File.Exists (waterMarkFile)) // if Source map or watermark map does not exist returnaf false; fileinfo fi = new fileinfo (sourcefile); // Decision file type is legal if (fi.extension.tolower ()! = ". Jpg" & fi.extension.tolower ()! = ". JPEG") RETURN FALSE; Try {// Original image Bitmap Simage = new bitmap (sourcefile); int swidth = simage.width; int sheight = simage.height; // Watermark Bitmap Wimage = New bitmark (Watermarkfile) ; Int wwidth = wiMage.width; int wheight = wiMage.height;
// make graphics. graphics g = graphics.fromImage (simage); int x; // Temporary variable int y; // Memach to invigible INT X1 = 0; // The width of the original map and the watermark map starts drawing X position int y1 = 0; // The high difference of the original map and the watermark map starts the Y position INT W = 0; // The width of the generated watermark map, that is, the X-position INT H = 0 ends the drawing; / The height of the generated watermark map, that is, the Y position int Al; // alpha int rl; // red int gl; // Green Int BL; // blue
// Verification Transparency IF (Alpha <1 || Alpha> 100) Al = 80; Else Al = Alpha; IF (SWIDTH> WWIDTH & Sheight> Wheight) // If the source map is larger {Switch (local) { Case 1: // = 0 on the left; Y1 = 0; Break; Case 2: // Left x1 = 0; IF (Sheight-wheight> 0) // Source Figure Solving Picture High Y1 = Sheight - wheight; ELSE Y1 = SWIDTH; Break; Case 3: // Right Y1 = 0; IF (SWIDTH - WWIDTH)> 0) // Source Figure Solving Wide X1 = SWIDTH - WWIDTH; ELSE X1 = Swidth; Break; Case 4 : // Right // Computational Height IF> 0) // Source Figure Split Printing Value High Y1 = Sheight - Wheight; Else Y1 = SWIDTH; / / Calculate Width IF ((Swidth - WWidth)> 0) // Source map than water printing diagram width x1 = swidth - wwidth; else x1 = swidth; break; case 5: // Current 高度 i ((Sheight-wheight)> 0) // Source map is a watermark High Y1 = (Sheight - wheight) / 2; ELSE Y1 = SWIDTH; / / Calculate Width IF ((SWIDTH - WWID) TH)> 0) // Source map is a width x1 = (SWIDTH - WWIDTH) / 2; Else X1 = swidth; break;} IF ((Sheight-wheight)> 0) H = WHEIGHT; Else H = Sheight; IF ((SWIDTH - WWIDTH)> 0) W = WWIDTH; Else W = SWIDTH;} ELSE // Source map is smaller {x1 = 0; Y1 = 0; w = swidth; h = sheight;} // Start Drawing for (x = 1; x IF (RL 25 <255) RL = 25; IF (GL 25 <255) GL = 25; IF (BL 25 <255) BL = 25; g.drawellipse (New Solidbrush (Color .Fromargb (al, rl, gl, bl)), x1 x, y1 y, 1, 1);}} g.save (); simage.save (savefile); result = true;} catch} = false;} return result;} private void btn_Water_Click (object sender, System.EventArgs e) {using (OpenFileDialog dlg = new OpenFileDialog ()) {if (dlg.ShowDialog () == DialogResult.OK) {string file = dlg .Filename; if (file.exists (file)) {this.waterfile = file; imgwater.image = new bitmap (file);}} DLG.Dispose ();}} }