A simple practice of step-by-step -71-asp.net (image processing)

Source: Internet
Author: User

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>HTML
usingSystem;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.ashx

1.3 Operating Effect

Simple practice of step-by-step -71-asp.net (image processing)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.