Asp.net generates a specified size thumbnail

Source: Internet
Author: User

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. 

  1. /// </Summary> 
  2. /// <Param name = "path"> </param> 
  3. /// <Returns> </returns> 
  4. PrivateSystem. Drawing. Image getimage (StringPath)
  5. {
  6. If(Path. startswith ("HTTP"))
  7. {
  8. System. net. webrequest request = system. net. webrequest. Create (PATH );
  9. Request. Timeout = 10000;
  10. System. net. httpwebresponse httpresponse = (system. net. httpwebresponse) request. getresponse ();
  11. Stream S = httpresponse. getresponsestream ();
  12.  
  13. ReturnSystem. Drawing. image. fromstream (s );
  14. }
  15. Else 
  16. {
  17. ReturnSystem. Drawing. image. fromfile (PATH );
  18. }
  19. }
  20.  
  21. /// <Summary> create an image of the specified size 
  22. /// </Summary> 
  23. /// <Param name = "opath"> absolute path of the source image </param> 
  24. /// <Param name = "tpath"> Generate the absolute path of the image </param> 
  25. /// <Param name = "width"> width of the generated image </param> 
  26. /// <Param name = "height"> height of the generated image </param> 
  27. Public VoidCreateimageoutput (IntWidth,IntHeight,StringOpath)
  28. {
  29. Bitmap originalbmp =Null;// New Bitmap (opath ); 
  30. Originalbmp =NewBitmap (getimage (opath ));
  31. // Position of the source image in the new image 
  32. IntLeft, top;
  33.  
  34.  
  35. If(Originalbmp. width <= width & originalbmp. height <= height)
  36. {
  37. // The width and height of the original image are smaller than the size of the generated image. 
  38. Left = (Int) Math. Round ((Decimal) (Width-originalbmp. width)/2 );
  39. Top = (Int) Math. Round ((Decimal) (Height-originalbmp. Height)/2 );
  40.  
  41.  
  42. // The final generated image 
  43. Bitmap bmpout =NewBitmap (width, height );
  44. Using(Graphics graphics = graphics. fromimage (bmpout ))
  45. {
  46. // Set a high quality Interpolation Method 
  47. Graphics. interpolationmode = interpolationmode. highqualitybicubic;
  48. // Clear the canvas and fill it with a white background color 
  49. Graphics. Clear (color. White );
  50. // Add a border 
  51. // Pen = new pen (colortranslator. fromhtml ("# cccccc ")); 
  52. // Graphics. drawrectangle (pen, 0, 0, width-1, height-1 ); 
  53. // Map the source image to the new canvas. 
  54. Graphics. drawimage (originalbmp, left, top );
  55. }
  56. // Bmpout. Save (tpath); // save as a file, and tpath as the path to save 
  57. This. Outputimgtopage (bmpout );// Output directly to the page 
  58. Bmpout. Dispose ();
  59. Return;
  60. }
  61.  
  62.  
  63. // The width and height of the new image, such as an image of 400*200. to generate an image of 160*120 without deformation, 
  64. // The generated image should be 160*80, and then draw the 160*80 image to the canvas of 160*120 
  65. IntNewwidth, newheight;
  66. If(Width * originalbmp. height
  67. {
  68. Newwidth = width;
  69. Newheight = (Int) Math. Round ((Decimal) Originalbmp. Height * width/originalbmp. width );
  70. // Scale to the same width as the predefined width, that is, Left = 0. Calculate the top 
  71. Left = 0;
  72. Top = (Int) Math. Round ((Decimal) (Height-newheight)/2 );
  73. }
  74. Else 
  75. {
  76. Newwidth = (Int) Math. Round ((Decimal) Originalbmp. Width * Height/originalbmp. Height );
  77. Newheight = height;
  78. // Scale to the same height as the predefined height, that is, Top = 0. Calculate left 
  79. Left = (Int) Math. Round ((Decimal) (Width-newwidth)/2 );
  80. Top = 0;
  81. }
  82.  
  83.  
  84. // Generate a scaled chart, for example, a 160*80 graph. 
  85. Bitmap bmpout2 =NewBitmap (newwidth, newheight );
  86. Using(Graphics graphics = graphics. fromimage (bmpout2 ))
  87. {
  88. Graphics. interpolationmode = interpolationmode. highqualitybicubic;
  89. Graphics. fillrectangle (brushes. White, 0, 0, newwidth, newheight );
  90. Graphics. drawimage (originalbmp, 0, 0, newwidth, newheight );
  91. }
  92. // Add the picture to the pre-defined canvas with a width and height, for example, 160*120. 
  93. Bitmap lastbmp =NewBitmap (width, height );
  94. Using(Graphics graphics = graphics. fromimage (lastbmp ))
  95. {
  96. // Set a high quality Interpolation Method 
  97. Graphics. interpolationmode = interpolationmode. highqualitybicubic;
  98. // Clear the canvas and fill it with a white background color 
  99. Graphics. Clear (color. White );
  100. // Add a border 
  101. // Pen = new pen (colortranslator. fromhtml ("# cccccc ")); 
  102. // Graphics. drawrectangle (pen, 0, 0, width-1, height-1 ); 
  103. // Map the source image to the new canvas. 
  104. Graphics. drawimage (bmpout2, left, top );
  105. }
  106. // Lastbmp. Save (tpath); // save as a file, and tpath as the path to save 
  107. This. Outputimgtopage (lastbmp );// Output directly to the page 
  108. Lastbmp. Dispose ();
  109. }
  110.  
  111.  
  112. Private VoidOutputimgtopage (system. Drawing. bitmap BMP)
  113. {
  114. // Output to the page 
  115. Memorystream MS =NewMemorystream ();
  116. BMP. Save (MS, system. Drawing. imaging. imageformat. JPEG );
  117.  
  118. Response. clearcontent ();// The HTTP header needs to be modified for the output image information 
  119. Byte[] Buffer = Ms. toarray ();
  120.  
  121. Response. addheader ("Content-Type","Image/JPEG");
  122. Response. binarywrite (buffer );
  123. BMP. Dispose ();
  124. }
Related Article

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.