/*
Note: generate a watermark. You can generate a file based on the top left, bottom left, top right, bottom right, center, and transparency. This file is only valid for JPEG or JPG formats!
Author: Blue. Dream
Disclaimer: Recently, many websites have quoted my articles and omitted the author's information. Please refer to this article and do not omit the author's information.
*/
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 ();
}
}
}
}