Notes for generating thumbnails during Asp.net image uploading

Source: Internet
Author: User

Bitmap. Save (imgpath, imageformat. JPEG );
// This is a part of the thumbnail. Code The imageformat. JPEG file must not be omitted. Even if the saved file is in JPG format, it cannot be removed. If removed, the generated thumbnail is larger than the original image!

// In addition, imgpath must be created first; otherwise, a general error of GDI + will be generated.
Path = system. Web. httpcontext. Current. server. mappath (PATH );
Use if (! System. Io. Directory. exists (PATH) system. Io. directiory. createdirectory (PATH );

// Generate a thumbnail. Do not use the getthumbnailimage method. This method produces extremely bad thumbnails and cannot be used!

// The simple code is as follows:
String Path = system. Web. httpcontext. Current. server. mappath (strpath );
Sourcepath = system. Web. httpcontext. Current. server. mappath (sourcepath );

If (! System. Io. Directory. exists (PATH) system. Io. Directory. createdirectory (PATH );

String sourceimage = sourcepath + filename;
String thumbimage = path + filename;

// Source image (reference)
Image IMG = image. fromfile (sourceimage, true );

// Actual thumbnail size
System. Drawing. Size size = pic. imgsize (maxw, maxh, IMG. Width, IMG. Height );
Int W = size. width;
Int H = size. height;

// Draw a thumbnail
Bitmap bitmap = new Bitmap (W, H );
Graphics G = graphics. fromimage (Bitmap );

// Set the thumbnail rendering quality
G. compositingquality = system. Drawing. drawing2d. compositingquality. highquality;
G. smoothingmode = system. Drawing. drawing2d. smoothingmode. highquality;
// G. interpolationmode = system. Drawing. drawing2d. interpolationmode. highqualitybicubic;

// Clean the background
G. Clear (color. White );

Rectangle thumbrect = new rectangle (0, 0, W, H );
G. drawimage (IMG, thumbrect );

// Save the thumbnail
Bitmap. Save (thumbimage, imageformat. JPEG );

// Release the memory
Bitmap. Dispose ();
IMG. Dispose ();
G. Dispose ();

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.