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 ();
}
}
}