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 |