private void Drawimagerectrect (String rawimgpath, string newimgpath, int width, int height) &NBS P { System.Drawing.Image imagefrom = System.Drawing.Image.FromFile (Rawimgpath); nbsp //source map width and height int imagefromwidth = Imagefro m.width; int imagefromheight = imagefrom.height; //position in the original canvas int X, y; //In the original Long width int bitmapwidth, bitmapheight; + in canvas nbsp Calculates the actual size of the thumbnail and its position on the canvas, based on the source map and the thumbnail size to be generated if (Imagefromwidth/width > I Magefromheight/height) { Bitmapwidth = (Width * imagefromheight)/height; Bitmapheight = Imagefromheig ht; X = (imagefromwidth-bitmapwidth)/2; &NB Sp Y = 0; } ELS e { Bitmapwidth = Imagefro mwidth; Bitmapheight = (height * imagefromwidth)/width; &NB Sp X = 0; Y = (Imagefromheig Ht-bitmapheight)/2; } //Create canvas & nbsp; Bitmap bmp = new Bitmap (width, height); & nbsp Graphics g = Graphics.fromimage (BMP); //White clear &NBS P G.clear (color.white); //Specify a high-quality double three-time interpolation method. Perform pre-screening to ensure high-quality shrinkage. This mode produces the highest quality conversion image. G.interpolationmode = interpolationmode.highqualitybicubic; //Specify high-quality, low-speed rendering. G.smoothingmode = smoothingmode.highquality; &N Bsp //Draws the specified portion of the specified Image at the specified position and at the specified size. G.drawimage (Imagefrom, New Rectangle (0, 0, width, height), new Rectangle (X , Y, Bitmapwidth, bitmapheight), GraphicsUnit.Pixel); try &NB Sp { //tested. jpg format thumbnail size and quality optimization BMP. Save (Newimgpath, imageforMat. Jpeg); } catch &NBSP ; { } finally & nbsp { //Show release resources &N Bsp Imagefrom.dispose (); BMP. Dispose (); g.dispose (); }& nbsp }
Source: http://www.cnblogs.com/tonymu/archive/2011/07/29/2121131.html;
Create a picture thumbnail method