Using System;
Using System. Drawing;
Using System. Drawing. Imaging;
Using System. Collections;
Using System. ComponentModel;
Using System. Windows. Forms;
Using System. Data;
Using System. IO;
Namespace test
{
///
/// Summary of Form1.
///
Public class Form1: System. Windows. Forms. Form
{
// Declare the number of digits of the random number generated by the encrypted character
Public const int LENGTH = 32;
// Declare the separator length before and after encryption characters
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;
///
/// Required designer variables.
///
Private System. ComponentModel. Container components = null;
Public Form1 ()
{
//
// Required for Windows Form Designer support
//
InitializeComponent ();
This. Init ();
//
// TODO: add Any constructor code after InitializeComponent calls
//
}
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] = "top left ";
Row [1] = 1;
Ht. Rows. Add (row );
Row = ht. NewRow ();
Row [0] = "bottom left ";
Row [1] = 2;
Ht. Rows. Add (row );
Row = ht. NewRow ();
Row [0] = "Top right ";
Row [1] = 3;
Ht. Rows. Add (row );
Row = ht. NewRow ();
Row [0] = "bottom right ";
Row [1] = 4;
Ht. Rows. Add (row );
Row = ht. NewRow ();
Row [0] = "center ";
Row [1] = 5;
Ht. Rows. Add (row );
This. local. DataSource = ht;
This. local. DisplayMember = "Keys ";
This. local. ValueMember = "Value ";
}
///
/// Clear all resources in use.
///
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region Windows Form Designer generated code
///
/// The designer supports the required methods-do not use the code editor to modify
/// Content of 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. imgSource });
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 image ";
//
// 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 graph ";
//
// 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 image ";
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 );
//
// ImgSource
//
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. 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 = "watermark image ";
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 = "-- 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 = "location ";
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 = "watermark image ";
//
// 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
///
/// Main 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) (this. local. SelectedValue );
Int al = 80;
Try
{
Al = Int32.Parse (this. alpha. Text. Trim ());
}
Catch
{
}
If (MakeWaterImage (this. SourceFile, this. WaterFile, this. SaveFile, local, al ))
{
This. imgObject. Image = new Bitmap (this. SaveFile );
}
}
///
/// Generate a watermark. You can generate a file based on the top left, bottom left, top right, bottom right, center, and transparency. It is only valid for jpeg or jpg format!
///
///Basemap
///Watermark image
///File to be saved
///Position: Top left (1), bottom left (2), top right (3), bottom 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 the source image or watermark image does not exist
Return false;
FileInfo fi = new FileInfo (sourceFile );
// Determine whether the file type is valid
If (fi. Extension. ToLower ()! = ". Jpg" & fi. Extension. ToLower ()! = ". Jpeg ")
Return false;
Try
{
// Source Image
Bitmap sImage = new Bitmap (sourceFile );
Int sWidth = sImage. Width;
Int sHeight = sImage. Height;
// Watermark image
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; // monitoring variable
Int x1 = 0; // The width difference between the source image and the watermark image, that is, the X position of the start drawing.
Int y1 = 0; // the height difference between the source image and the watermark image, that is, the Y position of the Start plot.
Int w = 0; // The width of the watermark image generated, that is, the X position of the ending drawing.
Int h = 0; // The height of the watermark image generated, that is, the Y position of the ending plot.
Int al; // alpha
Int rl; // Red
Int gl; // Green
Int bl; // Blue
// Verify transparency
If (alpha <1 || alpha> 100)
Al = 80;
Else
Al = alpha;
If (sWidth> wWidth & sHeight> wHeight) // if the source image is larger than the watermark image
{
Switch (local)
{
Case 1: // top left
X1 = 0;
Y1 = 0;
Break;
Case 2: // bottom left
X1 = 0;
If (sHeight-wHeight)> 0) // The Source image is higher than the watermark image.
Y1 = sHeight-wHeight;
Else
Y1 = sWidth;
Break;
Case 3: // top right
Y1 = 0;
If (sWidth-wWidth)> 0) // The Source image is wider than the watermark image.
X1 = sWidth-wWidth;
Else
X1 = sWidth;
Break;
Case 4: // bottom right
// Computing height
If (sHeight-wHeight)> 0) // The Source image is higher than the watermark image.
Y1 = sHeight-wHeight;
Else
Y1 = sWidth;
// Calculate the width
If (sWidth-wWidth)> 0) // The Source image is wider than the watermark image.
X1 = sWidth-wWidth;
Else
X1 = sWidth;
Break;
Case 5: // center
// Computing height
If (sHeight-wHeight)> 0) // The Source image is higher than the watermark image.
Y1 = (sHeight-wHeight)/2;
Else
Y1 = sWidth;
// Calculate the width
If (sWidth-wWidth)> 0) // The Source image is wider than the watermark image.
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 // The Source image is smaller than the watermark image.
{
X1 = 0;
Y1 = 0;
W = sWidth;
H = sHeight;
}
// Start plotting
For (x = 1; x <w; x ++)
{
For (y = 1; y {
Al = alpha;
Rl = wImage. GetPixel (x, y). R;
Gl = wImage. GetPixel (x, y). G;
Bl = wImage. GetPixel (x, y). B;
Al = 70;
If (rl + 25 <255)
Rl + = 25;
If (gl + 25 <255)
Gl + = 25;
If (bl + 25 <255)
Bl + = 25;
G. DrawEllipse (new Pen (new SolidBrush (Color. FromArgb (al, rl, gl, bl), x1 + x, y1 + y, 1, 1, 1 );
}
}
G. Save ();
SImage. Save (saveFile );
Result = true;
}
Catch
{
Result = 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 ();
}
}
}
}