This article implements the upload of the picture file by means of the fileupload control in VS2005 and generates a thumbnail.
Implementation process: After selecting the image upload success, obtain the file that already exists in the existing server generates thumbnails, and determines whether it is a picture type file, this judgment can be modified in the program, this program is just judging "image / bmp", "image / gif "," image / pjpeg "three types.
code show as below:
Upfile.aspx file
<%
@ Page language = "c #" autoeventwireup = "true" codefile = "upfile.aspx.cs" inherits = "upfile_upfile"
%>
DOCTYPE HTML PUBLIC "- // w3c // DTD XHTML 1.0 Transitional // En" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"
>
<
HTML
XMLNS
= "http://www.w3.org/1999/xhtml"
>
<
HEAD
Runat
= "Server"
>
<
Title
>
Untitled Page
Title
>
HEAD
>
<
Body
>
<
FORM
id
= "Form1"
Runat
= "Server"
>
<
Div
>
<
ASP: FileUpload
Id
= "FileUpload1"
Runat
= "Server"
/>
<
ASP: Button
Id
= "Button1"
Runat
= "Server"
Onclick
= "Button1_click"
TEXT
= "Upload"
/> <
Br
/>
<
ASP: Label
Id
= "Label1"
Runat
= "Server"
>
ASP: Label
>
Div
>
FORM
>
Body
>
HTML
>
Upfile.aspx.cs file
Using
System;
Using
System.data;
Using
System.configuration;
Using
System.collections;
Using
System.Web;
Using
System.Web.security;
Using
System.Web.ui;
Using
System.Web.ui.WebControls;
Using
System.Web.ui.WebControls.WebParts;
Using
System.Web.ui.htmlControls;
Using
System.IO;
public
Partial
Class
Upfile_upfile: system.Web.ui.page
{Protected void Page_Load (object sender, EventArgs e) {} protected void Button1_Click (object sender, EventArgs e) {if (FileUpload1.HasFile) {string fileContentType = FileUpload1.PostedFile.ContentType; if (fileContentType == "image / bmp" || fileContentType == "image / gif" || fileContentType == "image / pjpeg") {string name = FileUpload1.PostedFile.FileName; // client file path FileInfo file = new FileInfo (name); string fileName = file .Name; // file name string filename_s = "s_" file.name; // thumbnail file name string WebFilePath = Server.mappath ("file /" filename); // server-side file path string WebFilePath_s = Server. Mappath ("file /" filename_s); // server-side thumbnail path if (! File.exists (webfilepath)) {TRY { FILEUPLOAD1.SAVEAS (WebFilePath); // Save File MakethumbNail (WebFilePath, WebFilePath_s, 130, 130, "CUT"); // Generate Thumbnail Method Label1.Text = "Tip: File" FileName "" Successfully uploaded, and generated " filename_s " thumbnail, file type: " fileupload1.postedfile.contentType ", file size is: " fileupload1.postedFile.contentLength " B ";} catch (Exception EX) ) {Label1.text = "
Tip: Document Upload Failure, Failure Cause: " EX.MESSAGE;}} Else {Label1.Text =" Tip: The file already exists, please rename ";}} else {label1.text =" Tip: File type Do not match ";}}} / ** ////
if ((double) originalImage.Width / (double) originalImage.Height> (double) towidth / (double) toheight) {oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = ( OriginalImage.width - OW) / 2;} else {OW = OriginalImage.Width; OH = OriginalImage.width * Height / Towidth; x = 0; Y = (OriginalImage.Height - OH) / 2;} Break; Default: BREAK ;} // Create a bmp picture System.Drawing.Image bitmap = new System.Drawing.Bitmap (towidth, toheight); // Create a artboards System.Drawing.Graphics g = System.Drawing.Graphics.FromImage (bitmap); // Set high quality interpolation g.interpolationmode = system.drawing.drawing2d.interpolationmode.high; // Set high quality, low speed rendering smoothing degree g.smoothingMode = system.draw ing.drawing2d.smoothingmode.highquality; // Empty canvas and populate g.clear (system.drawing.color.transparent) in transparent background; // Draw the specified section of the original picture in the specified position and press the specified size G.DrawImage OriginalImage, New System.drawing.Rectangle (0, 0, Towidth, Toheight), New System.drawing.Rectangle (X, Y, OW, OH), System.drawing.graphicsUnit.pixel; try {// Taking JPG format Save thumbnail Bitmap.Save (thumbnailpath, system.drawing.imaging.imageformat.jpeg);} catch (system.exception e) {throw e;} finally {OriginalImage.dispose (); bitmap.dispose (); g.dispose ();