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