Thumbnail code (by percentage and by size)

Source: Internet
Author: User
Using system;
Using system. IO;
Using system. drawing;
Using system. Drawing. imaging; public class imagethumbnail
{
Public Image resourceimage;
Private int imagewidth;
Private int imageheight;
Public String errormessage;

Public imagethumbnail (string imagefilename)
{
Resourceimage = image. fromfile (imagefilename );
Errormessage = "";
}

Public bool thumbnailcallback ()
{
Return false;
}

// Method 1, by size
Public bool performancedimage (INT width, int height, string targetfilepath)
{
Try
{
Image cecedimage;
Image. getthumbnailimageabort callb = new image. getthumbnailimageabort (thumbnailcallback );
Performancedimage = resourceimage. getthumbnailimage (width, height, callb, intptr. Zero );
Performancedimage. Save (@ targetfilepath, imageformat. JPEG );
Cecedimage. Dispose ();
Return true;
}
Catch (exception E)
{
Errormessage = E. message;
Return false;
}
}

// Method 2, scale down by percentage by 60% percent to 0.6 targetfilepath as the target path
Public bool performancedimage (double percent, string targetfilepath)
{
Try
{
Image cecedimage;
Image. getthumbnailimageabort callb = new image. getthumbnailimageabort (thumbnailcallback );
Imagewidth = convert. toint32 (resourceimage. Width * percent );
Imageheight = (resourceimage. Height) * imagewidth/resourceimage. width; // proportional Scaling
Performancedimage = resourceimage. getthumbnailimage (imagewidth, imageheight, callb, intptr. Zero );
Performancedimage. Save (@ targetfilepath, imageformat. JPEG );
Cecedimage. Dispose ();
Return true;
}
Catch (exception E)
{
Errormessage = E. message;
Return false;
}
}

}

Background code:
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;
Public partial class _ default: system. Web. UI. Page
{

Protected void page_load (Object sender, eventargs E)
{

}
Protected void bt_upload_click (Object sender, eventargs E)
{
Try
{
If (fileupload1.postedfile. filename = "")
{
This. lb_info.text = "select a file! ";
}
Else
{
String filepath = fileupload1.postedfile. filename;
String filename = filepath. substring (filepath. lastindexof ("\") + 1 );
String serverpath1 = server. mappath ("images/") + filename;
String serverpath2 = server. mappath ("images/") + system. datetime. now. tostring ("yyy-mm-dd-hh-mm-SS") + session. sessionid + filename;
Fileupload1.postedfile. saveas (serverpath1 );
Imagethumbnail IMG = new imagethumbnail (filepath );
IMG. performancedimage (0.4, serverpath2); // 0.4 indicates a reduction of 40%
This. lb_info.text = "Upload successful! ";
}
}
Catch (Exception error)
{
This. lb_info.text = "Upload error! Cause: "+ error. tostring ();
}
}

}

Related Article

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.