1 Add watermark to original image
1.1 Encapsulates a class for getting the file path
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingsystem.web;namespace_06_ picture Processing { Public Static classFilehelper { Public Static stringGetFilePath () {//02 Creating a File Save path stringSavepath = Path.Combine (AppDomain.CurrentDomain.BaseDirectory +"upload\\"); //02-01 Creating folders based on datesDateTime dt =DateTime.Now; Savepath+ = dt. Year +"\\"+ dt. Month +"\\"+dt. Day; if(!directory.exists (Savepath)) { //Create a folderdirectory.createdirectory (Savepath); } //02-02 file name is current time//Savepath + = "\" + dt. ToString (). Replace (': ', '-') + '. gif ';Savepath + ="\\"+ dt. ToString (). Replace (':','-') ; returnSavepath; } }}
Filehelper
1.2 HTML pages and ASHX pages
<! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Content-type"Content="text/html; Charset=utf-8"/> <title></title>"addlogo.ashx"Method="Post"Enctype="Multipart/form-data"> <input type="file"Name="orimg"/> <input type="Submit"Value="Add Watermark"/> </form></body>HTMLusingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Linq;usingsystem.web;namespace_06_ picture Processing {/// <summary> ///Summary description of Addlogo/// </summary> Public classAddlogo:ihttphandler { Public voidProcessRequest (HttpContext context) {context. Response.ContentType="text/html"; //01 getting uploaded imagesHttppostedfile PF = context. request.files["orimg"]; #region02 Adding watermarks//02-01 Creating a canvasBitmap BM =NewBitmap (PF. InputStream); //02-02 Creating a drawing toolGraphics GS =Graphics.fromimage (BM); //02-03 Get the logo imageBitmap Bmlogo =NewBitmap (AppDomain.CurrentDomain.BaseDirectory +"/images/logoyk.gif"); //02-04 Start DrawingGs. DrawImage (BMLOGO,BM. WIDTH-BMLOGO.WIDTH,BM. height-bmlogo.height,bmlogo.width,bmlogo.height); #endregion #region03 Save//03-01 Get file name extensions stringExtname = PF. Filename.substring (PF. Filename.lastindexof ('.')); //03-02 Get file path stringph =Filehelper.getfilepath (); stringSavepath = ph +Extname; //03-03 saveAsBM. Save (Savepath); #endregion //04 ShowContext. Response.Write (""+ savepath.substring (Savepath.indexof ("Upload")) +"'/>"); } Public BOOLisreusable {Get { return false; } } }}
addlogo.ashx1.3 Operating Effect
Simple practice of step-by-step -71-asp.net (image processing)