asp.net picture upload to generate thumbnails of attention _ practical skills

Source: Internet
Author: User
Bitmap. Save (Imgpath,imageformat.jpeg);
This is a piece of code to save the thumbnail, where the imageformat.jpeg must not be omitted, even if you save the file is JPG format, it can not be removed. Because if removed, the resulting thumbnail is larger than the original picture!


In addition, Imgpath must be created first, or it will produce a generic error for GDI +.
Path=system.web.httpcontext.current.server.mappath (path);
Use if (! System.IO.Directory.Exists (path)) System.IO.Directiory.CreateDirectory (path);


Generate thumbnails, do not use the Getthumbnailimage method, this method produces a thumbnail image quality is extremely poor, can not 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;

Original artwork (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;

Drawing thumbnails
Bitmap bitmap=new Bitmap (w,h);
Graphics g=graphics.fromimage (bitmap);

Set 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 background
G.clear (Color.White);

Rectangle thumbrect=new Rectangle (0,0,w,h);
G.drawimage (Img,thumbrect);

Save thumbnails
Bitmap. Save (Thumbimage,imageformat.jpeg);

Free 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.