C # adding watermarks to images,
Let's talk about how to add a watermark to an uploaded image today.
Protected void button#click (object sender, EventArgs e)
{
Int location = Convert. ToInt32 (this. DropDownList1.SelectedValue); // obtain the watermark placement location
// Determine whether there is a file address in FileUpload
If (FileUpload1.HasFile)
{
If (FileUpload1.PostedFile. contentType = "image/pjpeg") | (FileUpload1.PostedFile. contentType = "image/jpeg") | (FileUpload1.PostedFile. contentType = "image/gif") | (FileUpload1.PostedFile. contentType = "image/bmp") | (FileUpload1.PostedFile. contentType = "image/x-png") | (FileUpload1.PostedFile. contentType = "image/png") // obtain the MIME content type of the file sent by the client.
{
// The total size of the uploaded file
Int fileLength = 0;
FileLength = fileLength + FileUpload1.PostedFile. ContentLength;
// The size cannot exceed maxLengthk
Int maxLength = 2048;
Int sysLength = maxLength * 1024;
If (fileLength> sysLength)
{
Response. Write ("<script> alert (''' the image size exceeds the limit of 2 MB) </script> ");
}
Else
{
String [] strSpil = FileUpload1.FileName. Split (''''. '''); // use the. Number to separate the address (img/1.jpg)
String strEnd = strSpil [strSpil. length-1]. toLower (); // obtain the following ("jpg", "gif", "bmp", "png", "jpeg", "JPG", "GIF ", "BMP", "PNG", "JPEG ")
String [] strPic = new string [] {"jpg", "gif", "bmp", "png", "jpeg", "JPG", "GIF ", "BMP", "PNG", "JPEG"}; // defines the file format in an array.
List <string> arry = new List <string> ();
Arry. AddRange (strPic); // defines a variable array for storing the file format
If (arry. contains (strEnd) // determines whether the array Contains ("jpg", "gif", "bmp", "png", "jpeg", "JPG ", "GIF", "BMP", "PNG", "JPEG ")
{
Random rand = new Random (); // defines a Random number, to prevent you from uploading images with duplicate names
String strName = DateTime. Now. ToString ("yyyymmmddhhss") + rand. Next (100,999 9). ToString (); // get a different name.
String strPointEnd = "." + strEnd;
String strFile = Server. MapPath ("~ /Upfile "); // obtain the relative address
FileUpload1.SaveAs (strFile + "/" + strName + strPointEnd); // Save the original image
String src = strFile + "/" + strName + strPointEnd;
// Add the watermark --------------------------------------------
// Watermark image
String shuiyin = "~ /Watermark/logo.png ";
// Load the file
System. Drawing. Image Cover;
Cover = System. Drawing. Image. FromFile (src );
// Load the watermark File
System. Drawing. Image water = System. Drawing. Image. FromFile (Request. MapPath (shuiyin ));
// Create a canvas
System. Drawing. Graphics g = System. Drawing. Graphics. FromImage (Cover );
If (location = 1) // top left
{
// Draw a watermark on the image
G. DrawImage (water, new Rectangle (0, 0, water. Width, water. Height), 0, 0, water. Width, water. Height, GraphicsUnit. Pixel );
}
Else if (location = 2) // lower left
{
// Draw a watermark on the image
G. drawImage (water, new Rectangle (0, Cover. height-water. height, water. width, water. height), 0, 0, water. width, water. height, GraphicsUnit. pixel );
}
Else if (location = 3) // upper right corner
{
// Draw a watermark on the image
G. drawImage (water, new Rectangle (Cover. width-water. width, 0, water. width, water. height), 0, 0, water. width, water. height, GraphicsUnit. pixel );
}
Else if (location = 4) // lower right
{
// Draw a watermark on the image
G. drawImage (water, new Rectangle (Cover. width-water. width, Cover. height-water. height, water. width, water. height), 0, 0, water. width, water. height, GraphicsUnit. pixel );
}
Else if (location = 5) // The Center
{
// Draw a watermark on the image
G. drawImage (water, new Rectangle (Cover. width-water. width)/2, (Cover. height-water. height)/2, water. width, water. height), 0, 0, water. width, water. height, GraphicsUnit. pixel );
}
Else
{
// Draw a watermark on the image
G. drawImage (water, new Rectangle (Cover. width-water. width, Cover. height-water. height, water. width, water. height), 0, 0, water. width, water. height, GraphicsUnit. pixel );
}
// Release the canvas
G. Dispose ();
// Release the watermark image
Water. Dispose ();
Cover. Save (HttpContext. Current. Server. MapPath ("~ /Upfile/") + strName +" s "+". "+ strEnd); // Save the watermark image
Cover. Dispose ();
String src1 = "upfile/" + strName + "s" + "." + strEnd;
Response. Write ("<a href =" + src1 + "> click to view the watermark image </a> ");
}
Else
{
Response. Write ("<script> alert (''' this image format cannot be uploaded! ''') </Script> ");
}
}
}
Else
{
Response. Write ("<script> alert (''' this image format cannot be uploaded! ''') </Script> ");
}
}
Else
{
Response. Write ("<script> alert (''' select the image path! ''') </Script> ");
}
}
Address: http://www.aspnetjia.com/Cont-39.html