In the previous article, we introduced the specified image file and added watermark to the specified file
- In this paper, watermark is applied to the project. When the user uploads the picture file, the corresponding watermark is added to the picture
- Still using a generic handler (. ashx)
Users add picture files to database and corresponding storage folder through upload control
Therefore, this function must have such a process
File upload → control receiving → get the name of the file in the control → instantiate the canvas object to write the watermark content → release the canvas to save the drawn picture file → output the picture to show to the user → stop http response
Add a small function to this item
System.Guid.NewGuid() format
Solve the problem that users fail to upload files with the same name when uploading
Remember to turn ToString() down
Display page replace server control with label (same usage)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Watermark a picture.WebForm2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <form id="form1" runat="server" action="Handler2.ashx" method="post" enctype="multipart/form-data"> <div> <input type="file" name="filew" /> <input type="submit" name="sub" value="Upload pictures" /> </div> </form> </body> </html>
Submit data as a form (seamless switch to server)
using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Web; namespace Watermark a picture { /// <summary> ///Summary description of Handler2 /// </summary> public class Handler2 : IHttpHandler { public void ProcessRequest(HttpContext context) { //Get uploaded files HttpPostedFile file = context.Request.Files[0]; if (file.ContentLength > 0) { //Check the uploaded file (whether it is the required image type) //(determine the suffix) get the name of the uploaded file (including the suffix) 1.jpg string filename = Path.GetFileName(file.FileName); //Get the suffix of the uploaded file.jpg string fileExit = Path.GetExtension(filename); //Determine whether the suffix is. jpg if (fileExit == ".jpg" || fileExit == ".png") { //Rename the uploaded file string newfilename = Guid.NewGuid().ToString(); //Put the uploaded file in the directory string dirupload = "~/ImgUpLoad/"; //Create a folder (determine whether there is a current path on the disk, if not, create one) if (!Directory.Exists(context.Request.MapPath(dirupload))) { Directory.CreateDirectory(context.Request.MapPath(dirupload)); } //Find the full path to the file string allDir = dirupload + newfilename + fileExit; //Save path file.SaveAs(context.Request.MapPath(allDir)); //Create a picture watermark //Create an Image using (Image tp = Image.FromFile(context.Request.MapPath(allDir))) { //Create canvas using (Bitmap map = new Bitmap(tp.Width, tp.Height)) { //Create brush using (Graphics grap = Graphics.FromImage(map)) { //Draw at specified location grap.DrawImage(tp, 0, 0, tp.Width, tp.Height); //Create font object Font ft = new Font("Microsoft YaHei", 18); //Specifies the drawn string grap.DrawString("Fresh meitao.com", ft, Brushes.Black, new Point(map.Width - 150, map.Height - ft.Height)); // string WYImagename = Guid.NewGuid().ToString(); map.Save(context.Request.MapPath("/ImgUpLoad/" + WYImagename + ".jpg"), ImageFormat.Jpeg); //output context.Response.Write("<html><body><img src='/ImgUpLoad/" + WYImagename + ".jpg' /></body></html>"); context.Response.ContentType = "text/html"; } } //Release pictures tp.Dispose(); } } } //Stop http response context.Response.End(); } public bool IsReusable { get { return false; } } } }
Explain why System.Guid.NewGuid() can ensure that the file name is not duplicate
string str = System.Guid.NewGuid().ToString();
In general, you do not need to consider the following format usage
Concept:
Guid: that is, the global unique identifier is also called UUID. A GUID is a 128 bit binary number identifier generated by a specific algorithm to indicate the uniqueness of the product. A GUID is primarily used in a network or system that has multiple nodes, computers, and assignments must have unique identifiers.
On the Windows platform, GUID is widely used in Microsoft products to identify such objects as registry keys, classes and interface identifiers, databases, system directories, etc.
Format:
The format of the GUID is "XXXX xxxx-xxxx-xxxx-xxxx-xxxxxxxx", where each x is a 32-bit hexadecimal number in the range 0-9 or a-f. For example, 6F9619FF-8B86-D011-B42D-00C04FC964FF is a valid guid value.
System.Guid.NewGuid().ToString("N"); 32 Bit string
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
System.Guid.NewGuid().ToString("D"); Hyphenated 32-bit string
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
System.Guid.NewGuid().ToString("B"); 32-bit string separated by hyphens in braces
{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
System.Guid.NewGuid().ToString("P"); A 32-bit string separated by hyphens in parentheses
(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)