Use .NET to generate watermark files according to transparency

xiaoxiao2021-03-06  60

Using system;

Using system.drawing;

Using system.drawing.image;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

Using system.io;

A summary description of Namespace Test {///

/// Form1. /// public class form1: system.windows.Forms.form {// Declare the number of bits of the random number generated by the encrypted character Generated Public const length = 32; // Declaration of the separation length before and after 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; /// < Summary> // The required designer variable. /// private system.componentmodel.container component = null; public form1 () {// // Windows Form Designer Support for // InitializationComponent (); 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; this.local.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 (4);

#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.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.imgsource}; this.groupbox1.n AME = "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 =" generated "; this.button2.click = new system.eventhandler (this.button2_click); /// ivsource // 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.imgobject.tabstop = false; // // imgwater // this.imgwater.location = new system.draw ING.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 = "Water Printing"; 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. Alpha.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 (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 = "water print"; // // Form1 // this.drawing.size = 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.group1, this .ghoupBox2, this.label2}; this.name = "form1"; this.text = "form1"; this.groupBox1.ResumeLayout (false); this.groupBox2.ResumeLayout; this.groupbox3.ResumeLayout (False) "This.ResumeLayout (false);} #ENDREGION

///

/// The primary entry point of 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, bottom, center, transparency, only valid for JPEG or JPG format! /// /// Basemap /// Watermark /// The file to be saved /// Location: top left (1), lower left (2), upper right (3), upper right (4), center (5) /// Transparency (1-100) /// BOOL, whether it is successful public static bool makewaterImage (String Sourcefile, String Savefile, String Savefile , int local, int alpha) {bool result; if (! file.exists (sourcefile) ||! file.exists (watermarkfile)) // If the source map or watermark does not exist returnaf fi = new fileinfo (Sourcefile ); // determine if the file type is legal IF (FI.EXtension.tolower ()! = ")! =" Jpeg "Return False; try {// original picture Bitmap Simage = new Bitmap (sourceFile); int sWidth = sImage.Width; int sHeight = sImage.Height; // the watermark Bitmap wImage = new Bitmap (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 - Wwidth> 0) // Source Figure Solving Wide 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 Small {x1 = 0; y1 = 0; w = Swidth; h = height;} // Start drawing for (x = 1; x

} 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 ();}}

}

转载请注明原文地址:https://www.9cbs.com/read-114100.html

New Post(0)