Asp.net generates a specified size thumbnail. If the image size is smaller than the specified size, it is displayed in the middle of the specified size canvas. If the image size is greater than the specified size, the image is scaled proportionally, on the canvas of the specified size. It can be output directly to the page or saved as a file. KeyCodeFor details about the improvements, please submit them. Thank you.
You can directly copy the three namespaces and use them:
Using system. drawing; <br/> using system. drawing. drawing2d; <br/> using system. io; <br/> using system; </P> <p> Public Class A <br/> {<br/> /// <summary> <br/> /// read the file by path, supports remote files, local file <br/> /// </Summary> <br/> // <Param name = "path"> </param> <br/> // <returns> </returns> <br/> private system. drawing. image getimage (string path) <br/>{< br/> If (path. startswith ("HTTP") <br/>{< br/> system. net. webrequest request = system. net. webrequest. create (PATH); <br/> request. timeout = 10000; <br/> system. net. httpwebresponse httpresponse = (system. net. httpwebresponse) request. getresponse (); <br/> stream S = httpresponse. getresponsestream (); <br/> return system. drawing. image. fromstream (s); <br/>}< br/> else <br/>{< br/> return system. drawing. image. fromfile (PATH ); <br/>}</P> <p> // <summary> create an image of the specified size </Summary> <br/> // <param name = "opath"> absolute path of the source image </param> <br/> // <Param name = "tpath"> Generate an absolute path of the image </param> <br />/// <Param name = "width"> width of the generated image </param> <br/> /// <Param name = "height"> height of the generated image </param> <br/> Public void createimageoutput (INT width, int height, string opath) <br/>{< br/> bitmap originalbmp = NULL; // new Bitmap (opath ); <br/> originalbmp = new Bitmap (getimage (opath); // the position of the source image in the new image <br/> int left, top; <br/> If (originalbmp. width <= width & originalbmp. height <= height) <br/>{< br/> // the width and height of the original image are smaller than the size of the generated image <br/> left = (INT) math. round (decimal) (width-originalbmp. width)/2); <br/> Top = (INT) math. round (decimal) (height-originalbmp. height)/2); <br/> // The final generated image <br/> bitmap bmpout = new Bitmap (width, height ); <br/> using (Graphics graphics = graphics. fromimage (bmpout) <br/>{< br/> // set a high quality interpolation method <br/> graphics. interpolationmode = interpolationmode. highqualitybicubic; <br/> // clear the canvas and fill it with a white background color. <br/> graphics. clear (color. white); <br/> // Add a border <br/> // Pen = new pen (colortranslator. fromhtml ("# cccccc"); <br/> // graphics. drawrectangle (pen, 0, 0, width-1, height-1); <br/> // source image to the new canvas <br/> graphics. drawimage (originalbmp, left, top); <br/>}< br/> // bmpout. save (tpath); <br/> // save as a file, and tpath as the path to save <br/> This. outputimgtopage (bmpout); // directly output to the page <br/> bmpout. dispose (); <br/> return; <br/>}< br/> // width and height of the new image, such as an image of 400*200, to generate a 160*120 image without deformation, <br/> // The generated image should be 160*80, then draw an image of 160*80 to the canvas of 160*120 <br/> int newwidth, newheight; <br/> If (width * originalbmp. height <peight * originalbmp. width) <br/>{< br/> newwidth = width; <br/> newheight = (INT) math. round (decimal) originalbmp. height * width/originalbmp. width); <br/> // scale to the same width as the predefined width, that is, Left = 0. Calculate top <br/> left = 0; <br/> Top = (INT) math. round (decimal) (height-newheight)/2); <br/>}< br/> else <br/>{< br/> newwidth = (INT) math. round (decimal) originalbmp. width * Height/originalbmp. height); <br/> newheight = height; <br/> // scale to the same height as the predefined height, that is, Top = 0, calculate left <br/> left = (INT) math. round (decimal) (width-newwidth)/2); <br/> Top = 0; <br/>}< br/> // generate a scaled chart, example: 160*80 graph <br/> bitmap bmpout2 = new Bitmap (newwidth, newheight); <br/> using (Graphics graphics = graphics. fromimage (bmpout2) <br/>{< br/> graphics. interpolationmode = interpolationmode. highqualitybicubic; <br/> graphics. fillrectangle (brushes. white, 0, 0, newwidth, newheight); <br/> graphics. drawimage (originalbmp, 0, 0, newwidth, newheight); <br/>}< br/> // you can specify the width and height of the picture on the pre-defined canvas, for example, 160*120 <br/> bitmap lastbmp = new Bitmap (width, height); <br/> using (Graphics graphics = graphics. fromimage (lastbmp) <br/>{< br/> // set a high quality interpolation method <br/> graphics. interpolationmode = interpolationmode. highqualitybicubic; <br/> // clear the canvas and fill it with a white background color. <br/> graphics. clear (color. white); <br/> // Add a border <br/> // Pen = new pen (colortranslator. fromhtml ("# cccccc"); <br/> // graphics. drawrectangle (pen, 0, 0, width-1, height-1); <br/> // source image to the new canvas <br/> graphics. drawimage (bmpout2, left, top); <br/>}< br/> // lastbmp. save (tpath); // save as a file, and tpath as the path to save <br/> This. outputimgtopage (lastbmp); <br/> // output directly to the page <br/> lastbmp. dispose (); <br/>}</P> <p> private void outputimgtopage (system. drawing. bitmap BMP) <br/>{< br/> // output to the page <br/> memorystream MS = new memorystream (); <br/> BMP. save (MS, system. drawing. imaging. imageformat. JPEG); <br/> response. clearcontent (); // The HTTP header needs to be modified to output image information <br/> byte [] buffer = Ms. toarray (); <br/> response. addheader ("Content-Type", "image/JPEG"); <br/> response. binarywrite (buffer); <br/> BMP. dispose (); <br/>}</P> <p>}
Using system. drawing;
Using system. Drawing. drawing2d;
Using system. IO;
/// <Summary>
/// Read files by path. remote files and local files are supported.
-
- /// </Summary>
-
- /// <Param name = "path"> </param>
- /// <Returns> </returns>
-
- PrivateSystem. Drawing. Image getimage (StringPath)
-
- {
-
- If(Path. startswith ("HTTP"))
-
- {
-
- System. net. webrequest request = system. net. webrequest. Create (PATH );
- Request. Timeout = 10000;
-
- System. net. httpwebresponse httpresponse = (system. net. httpwebresponse) request. getresponse ();
-
- Stream S = httpresponse. getresponsestream ();
-
-
-
- ReturnSystem. Drawing. image. fromstream (s );
-
- }
- Else
-
- {
-
- ReturnSystem. Drawing. image. fromfile (PATH );
-
- }
-
- }
-
-
-
- /// <Summary> create an image of the specified size
- /// </Summary>
-
- /// <Param name = "opath"> absolute path of the source image </param>
-
- /// <Param name = "tpath"> Generate the absolute path of the image </param>
-
- /// <Param name = "width"> width of the generated image </param>
- /// <Param name = "height"> height of the generated image </param>
-
- Public VoidCreateimageoutput (IntWidth,IntHeight,StringOpath)
-
- {
-
- Bitmap originalbmp =Null;// New Bitmap (opath );
- Originalbmp =NewBitmap (getimage (opath ));
-
- // Position of the source image in the new image
-
- IntLeft, top;
-
-
-
-
-
- If(Originalbmp. width <= width & originalbmp. height <= height)
-
- {
- // The width and height of the original image are smaller than the size of the generated image.
-
- Left = (Int) Math. Round ((Decimal) (Width-originalbmp. width)/2 );
-
- Top = (Int) Math. Round ((Decimal) (Height-originalbmp. Height)/2 );
-
-
-
-
- // The final generated image
-
- Bitmap bmpout =NewBitmap (width, height );
-
- Using(Graphics graphics = graphics. fromimage (bmpout ))
-
- {
- // Set a high quality Interpolation Method
-
- Graphics. interpolationmode = interpolationmode. highqualitybicubic;
-
- // Clear the canvas and fill it with a white background color
-
- Graphics. Clear (color. White );
- // Add a border
-
- // Pen = new pen (colortranslator. fromhtml ("# cccccc "));
-
- // Graphics. drawrectangle (pen, 0, 0, width-1, height-1 );
- // Map the source image to the new canvas.
-
- Graphics. drawimage (originalbmp, left, top );
-
- }
-
- // Bmpout. Save (tpath); // save as a file, and tpath as the path to save
- This. Outputimgtopage (bmpout );// Output directly to the page
-
- Bmpout. Dispose ();
-
- Return;
-
- }
-
-
-
-
-
- // The width and height of the new image, such as an image of 400*200. to generate an image of 160*120 without deformation,
- // The generated image should be 160*80, and then draw the 160*80 image to the canvas of 160*120
-
- IntNewwidth, newheight;
-
- If(Width * originalbmp. height
-
- {
-
- Newwidth = width;
- Newheight = (Int) Math. Round ((Decimal) Originalbmp. Height * width/originalbmp. width );
-
- // Scale to the same width as the predefined width, that is, Left = 0. Calculate the top
-
- Left = 0;
-
- Top = (Int) Math. Round ((Decimal) (Height-newheight)/2 );
- }
-
- Else
-
- {
-
- Newwidth = (Int) Math. Round ((Decimal) Originalbmp. Width * Height/originalbmp. Height );
-
- Newheight = height;
- // Scale to the same height as the predefined height, that is, Top = 0. Calculate left
-
- Left = (Int) Math. Round ((Decimal) (Width-newwidth)/2 );
-
- Top = 0;
-
- }
-
-
-
-
- // Generate a scaled chart, for example, a 160*80 graph.
-
- Bitmap bmpout2 =NewBitmap (newwidth, newheight );
-
- Using(Graphics graphics = graphics. fromimage (bmpout2 ))
-
- {
-
- Graphics. interpolationmode = interpolationmode. highqualitybicubic;
- Graphics. fillrectangle (brushes. White, 0, 0, newwidth, newheight );
-
- Graphics. drawimage (originalbmp, 0, 0, newwidth, newheight );
-
- }
-
- // Add the picture to the pre-defined canvas with a width and height, for example, 160*120.
-
- Bitmap lastbmp =NewBitmap (width, height );
- Using(Graphics graphics = graphics. fromimage (lastbmp ))
-
- {
-
- // Set a high quality Interpolation Method
-
- Graphics. interpolationmode = interpolationmode. highqualitybicubic;
-
- // Clear the canvas and fill it with a white background color
- Graphics. Clear (color. White );
-
- // Add a border
-
- // Pen = new pen (colortranslator. fromhtml ("# cccccc "));
-
- // Graphics. drawrectangle (pen, 0, 0, width-1, height-1 );
- // Map the source image to the new canvas.
-
- Graphics. drawimage (bmpout2, left, top );
-
- }
-
- // Lastbmp. Save (tpath); // save as a file, and tpath as the path to save
-
- This. Outputimgtopage (lastbmp );// Output directly to the page
- Lastbmp. Dispose ();
-
- }
-
-
-
-
-
- Private VoidOutputimgtopage (system. Drawing. bitmap BMP)
-
- {
-
- // Output to the page
-
- Memorystream MS =NewMemorystream ();
- BMP. Save (MS, system. Drawing. imaging. imageformat. JPEG );
-
-
-
- Response. clearcontent ();// The HTTP header needs to be modified for the output image information
-
- Byte[] Buffer = Ms. toarray ();
-
-
-
- Response. addheader ("Content-Type","Image/JPEG");
-
- Response. binarywrite (buffer );
- BMP. Dispose ();
-
- }