Build thumbnail program written by C #

Source: Internet
Author: User
Tags datetime file size tostring root directory

if (fileupload. Postedfile!=null)

{

AddTo for the property to be added, Aboutfile for the file description

String nam = FileUpload. Postedfile.filename;

Gets the last "." in the filename (enclosing path). The index

int i= Nam. LastIndexOf (".");

Get file name extension

String Newext =nam. Substring (i);

Here I automatically name the file according to the date and file size to ensure that the filename is not duplicated

DateTime now = DateTime.Now;

String Newname=now. Dayofyear.tostring () +fileupload. PostedFile.ContentLength.ToString ();

Save the file to the directory you want, this is the upload directory under the IIS root directory. You can change.

Note: I use Server.MapPath () to take the relative directory of the current file. In asp.net "/" must use "//" instead, "upload//" to "//upload//" to take the current file absolute directory

FileUpload. Postedfile.saveas (Server.MapPath ("upload//" +newname+newext));

Get the related attributes of this file: file name, file type, file size

FName. Text=myfile.postedfile.filename;

Fenc. Text=myfile.postedfile.contenttype;

Fsize. Text=myfile.postedfile.contentlength.tostring ();

Here is the build thumbnail

System.Drawing.Image Image,anewimage;

int width,height,newwidth,newheight;

Image=system.drawing.image.fromfile (Server.MapPath) ("upload/" +newname. ToString () +newext. ToString ()));

System.Drawing.Image.GetThumbnailImageAbort callb =new System.Drawing.Image.GetThumbnailImageAbort ( Thumbnailcallback);

Width=image. Width;

Height=image. Height;

if (firstpageshow. selectedvalue.tostring () = = "1" &&fenlei. selectedvalue.tostring () = = "5")

{

newwidth=203;newheight=86;

Newheight=height*newwidth/width;

}

else if (Isweekman. selectedvalue.tostring () = = "1")

{

newwidth=171;newheight=111;

Newheight=height*newwidth/width;

}

else if (firstpageshow. selectedvalue.tostring () = = "1" &&fenlei. selectedvalue.tostring () = = "3")

{

newwidth=171;newheight=111;

Newheight=height*newwidth/width;

}

Else

{

newwidth=62;newheight=80;

Newheight=height*newwidth/width;

}

Anewimage=image. Getthumbnailimage (Newwidth,newheight,callb,new system.intptr ());

Anewimage.save (Server.MapPath ("upload//" + "Small_" +newname+newext));

Image. Dispose ();

Build and save the thumbnail, save name is added a Small_ before the original name.

Pic= "upload/" +newname. ToString () +newext. ToString ();

Smallpic= "Upload/small_" +newname. ToString () +newext. ToString ();

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.