Asp.net simple image compression and thumbnail implementation

Source: Internet
Author: User

1. Create an image compression class.

Public Image resourceimage;
Public String errormessage;
Public imagethumbnail (string imagefilename)
{
Resourceimage = image. fromfile (imagefilename );
Errormessage = string. empty;
}
Public bool thumbnailcallback ()
{
Return false;
}


/// <Summary>
/// Method 1: You can customize the image height and width.
/// </Summary>
/// <Param name = "width"> image width </param>
/// <Param name = "height"> image height </param>
/// <Param name = "targetfilepath"> image path </param>
/// <Returns> </returns>
Public bool performancedimage (INT width, int height, string targetfilepath)
{
# Region // method for processing thumbnails
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;
Systemerrorlogbll. insererrorlog (E. Message + ":" + E. Source + ":" + E. targetsite + ": imagethumbnail. cecedimage () method exception in processing thumbnails ");
Return false;
}
# Endregion
}

# Region

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

2. Call: imagethumbnail imgy = new imagethumbnail (image path );
Imgy. cecedimage (, generating location );

Through tests, it is found that the effect of images generated in a lifetime is better than that of method 2. If you need images to achieve better results, you can use graphics to draw thumbnails for further modification.

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.