C # Generate thumbnails

Source: Internet
Author: User

A lot of times. The user uploads a picture. Need to generate thumbnails of different sizes, of course. Thumbnails cannot be larger than the currently uploaded image. Otherwise the resolution is changed. The avatar's not clear.

The following code is attached.

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Drawing;6 usingSystem.Drawing.Imaging;7 8 namespaceado.net019 {Ten     class Program One     { A         Static voidMain (string[] args) -         { -             //Get Picture Path the             stringy =System.AppDomain.CurrentDomain.BaseDirectory; -             stringimg = y +"1.jpg"; -  -Bitmap r = Createthumbnail (IMG, -, -); +R.save (y +"12.jpg");//Save thumbnail image -         } +         //bitmap the owning namespace system. Drawing A  at         /// <summary> -         ///  -         /// </summary> -         /// <param name= "Lcfilename" >need to change the size of the picture position</param> -         /// <param name= "Lnwidth" >the width of the thumbnail image</param> -         /// <param name= "Lnheight" >the height of the thumbnail image</param> in         /// <returns></returns> -          Public StaticBitmap Createthumbnail (stringLcfilename,intLnwidth,intlnheight) to         { +System.Drawing.Bitmap bmpout =NULL; -             Try the             { *Bitmap lobmp =NewBitmap (lcfilename); $ImageFormat Loformat =Lobmp.rawformat;Panax Notoginseng  -                 decimalLnratio; the                 intLnnewwidth =0; +                 intLnnewheight =0; A  the                 //If the image is smaller than the thumbnail, return to the original picture, because upfront +                 if(Lobmp.width < lnwidth && Lobmp.height <lnheight) -                     returnlobmp; $  $                 if(Lobmp.width >lobmp.height) -                 { -Lnratio = (decimal) Lnwidth/Lobmp.width; theLnnewwidth =Lnwidth; -                     decimalLntemp = Lobmp.height *Lnratio;WuyiLnnewheight = (int) lntemp; the                 } -                 Else Wu                 { -Lnratio = (decimal) Lnheight/Lobmp.height; AboutLnnewheight =Lnheight; $                     decimalLntemp = Lobmp.width *Lnratio; -Lnnewwidth = (int) lntemp; -                 } -Bmpout =NewBitmap (Lnnewwidth, lnnewheight); AGraphics g =graphics.fromimage (bmpout); +G.interpolationmode =System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; theG.fillrectangle (Brushes.white,0,0, Lnnewwidth, lnnewheight); -G.drawimage (Lobmp,0,0, Lnnewwidth, lnnewheight); $  the lobmp.dispose (); the             } the             Catch the             { -                 return NULL; in             } the  the             returnbmpout; About         } the     } the}

After you run the project. A successful thumbnail image is generated

C # Generate thumbnails

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.