Asp.net instance: Adjust the Image Upload ratio to the optimum.

Source: Internet
Author: User

  ASP. NET documentWhen developing website programs, we often encounter Image Upload functions, especially in CMS applications. How can we make the uploaded images adaptive, this does not affect image quality. Today, we will share a code that will reduce the proportion of uploaded images and will not affect resolution.

# Region generating thumbnails
   /// <Summary>
   /// Generate a thumbnail
   /// </Summary>
   /// <Param name = "filePath"> input image path </param>
   Private void SaveImage (string filePath)
   {
       String fileName = Path. GetExtension (File1.PostedFile. FileName). ToLower ();
       String _ filename = DateTime. Now. ToString ("yyMMddHHmm ");

       System. Drawing. Image image = null, aNewImage = null;

       // Below is the thumbnail generated
       Int newwidth = 0, newheight = 0;
       Image = System. Drawing. Image. FromFile (filePath );
       System. Drawing. Image. GetThumbnailImageAbort callb = new System. Drawing. Image. GetThumbnailImageAbort (ThumbnailCallback );

       // Take the height and width 
       Int phWidth = image. Width;
       Int phHeight = image. Height;

       // Set the thumbnail width and height
       If (phWidth> 110)
       {
           // Specify the width
           Newwidth = 110;
           // Calculate the height based on the width
           Newheight = phHeight * newwidth/phWidth;
       }
       ANewImage = image. GetThumbnailImage (newwidth, newheight, callb, new System. IntPtr ());
       // Rename and save the thumbnail
       ANewImage. Save (Server. MapPath ("~ /Upload/") +" Avaster _ "+ DateTime. Now. ToString (" yyMMddHHmm ") + fileName );
       Image. Dispose ();
   }

   # Endregion

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.