In asp.net, the image upload function is commonly used to generate thumbnails. There are many methods for baidu, google, and c #, but when I use them, I find that the thumbnails are not clear and the thumbnails are too large. below is my code for processing images, the results are good, so share them out (the results can achieve the effects of some drawing software)
The Code is as follows:
/// <Summary> /// asp.net uploads an image and generates a thumbnail /// </summary> /// <param name = "upImage"> HtmlInputFile Control </param>/ // <param name = "sSavePath"> saved path, </param> // <param name = "sThumbExtension"> thumb of the thumbnail </param> /// <param name = "intThumbWidth "> width of the generated thumbnail </param> /// <param name =" intThumbHeight "> height of the generated thumbnail </param> /// <returns> thumbnail name </returns> public string UpLoadImage (HtmlInputFile upImage, string sSaveP Ath, string sThumbExtension, int intThumbWidth, int intThumbHeight) {string sThumbFile = ""; string sFilename = ""; if (upImage. PostedFile! = Null) {HttpPostedFile myFile = upImage. postedFile; int nFileLen = myFile. contentLength; if (nFileLen = 0) return "No upload image selected"; // obtain the extension string extendName = System of the selected upImage file. IO. path. getExtension (myFile. fileName ). toLower (); // determines whether the image format is if (extendName! = ". Jpg" & extendName! = ". Jpge" & extendName! = ". Gif" & extendName! = ". Bmp" & extendName! = ". Png ") return" incorrect image format "; byte [] myData = new Byte [nFileLen]; myFile. inputStream. read (myData, 0, nFileLen); sFilename = System. IO. path. getFileName (myFile. fileName); int file_append = 0; // check whether an image with the same name exists in the current folder. If yes, the file name is + 1 while (System. IO. file. exists (System. web. httpContext. current. server. mapPath (sSavePath + sFilename) {file_append ++; sFilename = System. IO. path. getFileNameWithoutExtension (myFile. fileName) + file_append.ToString () + extendName;} System. IO. fileStream newFile = new System. IO. fileStream (System. web. httpContext. current. server. mapPath (sSavePath + sFilename), System. IO. fileMode. create, System. IO. fileAccess. write); newFile. write (myData, 0, myData. length); newFile. close (); // The above is the upload source image try {// The Source image loads using (System. drawing. image sourceImage = System. drawing. image. fromFile (System. web. httpContext. current. server. mapPath (sSavePath + sFilename) {// source image width and height int width = sourceImage. width; int height = sourceImage. height; int smallWidth; int smallHeight; // obtain the size of the first drawing image (compare the width of the source image/the width of the thumbnail and the Height of the source image/the Height of the thumbnail) if (decimal) width)/height <= (decimal) intThumbWidth)/intThumbHeight) {smallWidth = intThumbWidth; smallHeight = intThumbWidth * height/width ;} else {smallWidth = intThumbHeight * width/height; smallHeight = intThumbHeight;} // you can determine whether the file with the same name in the current folder contains file_append = 0; sThumbFile = sThumbExtension + System. IO. path. getFileNameWithoutExtension (myFile. fileName) + extendName; while (System. IO. file. exists (System. web. httpContext. current. server. mapPath (sSavePath + sThumbFile) {file_append ++; sThumbFile = sThumbExtension + System. IO. path. getFileNameWithoutExtension (myFile. fileName) + file_append.ToString () + extendName;} // the absolute path string smallImagePath = System. web. httpContext. current. server. mapPath (sSavePath) + sThumbFile; // create a new graphic board and draw the original image using (System. drawing. image bitmap = new System. drawing. bitmap (smallWidth, smallHeight) {// draw the intermediate figure using (System. drawing. graphics g = System. drawing. graphics. fromImage (bitmap) {// HD, smooth g. interpolationMode = System. drawing. drawing2D. interpolationMode. high; g. smoothingMode = System. drawing. drawing2D. smoothingMode. highQuality; g. clear (Color. black); g. drawImage (sourceImage, new System. drawing. rectangle (0, 0, smallWidth, smallHeight), new System. drawing. rectangle (0, 0, width, height), System. drawing. graphicsUnit. pixel);} // create a new graphic board and draw the intermediate image using (System. drawing. image bitmap1 = new System. drawing. bitmap (intThumbWidth, intThumbHeight) {// draw the thumbnail using (System. drawing. graphics g = System. drawing. graphics. fromImage (bitmap1) {// HD, smooth g. interpolationMode = System. drawing. drawing2D. interpolationMode. high; g. smoothingMode = System. drawing. drawing2D. smoothingMode. highQuality; g. clear (Color. black); int lwidth = (smallWidth-intThumbWidth)/2; int bheight = (smallHeight-intThumbHeight)/2; g. drawImage (bitmap, new Rectangle (0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit. pixel); g. dispose (); bitmap1.Save (smallImagePath, System. drawing. imaging. imageFormat. jpeg) ;}}}} catch {// delete System if an error occurs. IO. file. delete (System. web. httpContext. current. server. mapPath (sSavePath + sFilename); return "incorrect image format";} // return the thumbnail name return sThumbFile;} return "no image selected ";}
What I think everyone should know about the HtmlInputFile control is that input type = file... this will be explained in asp.net later.