ASP. NET uploads images and generates thumbnails,

Source: Internet
Author: User
Tags bmp image

ASP. NET uploads images and generates thumbnails,

This document describes how to upload images and generate thumbnails using ASP. NET. We will share this with you for your reference. The details are as follows:

Protected void bt_upload_Click (object sender, EventArgs e) {// check whether the format of the uploaded file is valid if (this. uploadFile. postedFile. contentType. toLower (). indexOf ("image") <0) {Response. write ("the format of the uploaded image is invalid! "); Return;} // generate the source image Byte [] oFileByte = new byte [this. uploadFile. postedFile. contentLength]; System. IO. stream oStream = this. uploadFile. postedFile. inputStream; System. drawing. image oImage = System. drawing. image. fromStream (oStream); int oWidth = oImage. width; // source Image Width int oHeight = oImage. height; // The source Image Height int tWidth = 100; // set the initial width of the thumbnail int tHeight = 100; // set the initial height of the thumbnail. // calculate the width and height of the thumbnail proportionally. if (oWidth> = oHeight) {tHeight = (int) Math. floor (Convert. toDouble (oHeight) * (Convert. toDouble (tWidth)/Convert. toDouble (oWidth);} else {tWidth = (int) Math. floor (Convert. toDouble (oWidth) * (Convert. toDouble (tHeight)/Convert. toDouble (oHeight);} // generates the thumbnail. Bitmap tImage = new Bitmap (tWidth, tHeight); Graphics g = Graphics. fromImage (tImage); g. interpolationMode = System. drawing. drawing2D. interpolationMode. high; // set the High quality interpolation method g. smoothingMode = System. drawing. drawing2D. smoothingMode. highQuality; // set high quality, low speed rendering smoothness g. clear (Color. transparent); // clear the canvas and fill g with a Transparent background color. drawImage (oImage, new Rectangle (0, 0, tWidth, tHeight), new Rectangle (0, 0, oWidth, oHeight), GraphicsUnit. pixel); string oFullName = Server. mapPath (". ") +"/image/"+" o "+ DateTime. now. tow.datestring (). replace ("-", "") + DateTime. now. hour. toString () + DateTime. now. minute. toString () + DateTime. now. second. toString () + DateTime. now. millisecond. toString () + ". jpg "; // the physical path for saving the source image string tFullName = Server. mapPath (". ") +"/image/"+" t "+ DateTime. now. tow.datestring (). replace ("-", "") + DateTime. now. hour. toString () + DateTime. now. minute. toString () + DateTime. now. second. toString () + DateTime. now. millisecond. toString () + ". jpg "; // the physical path for saving the thumbnail try {// Save the image oImage in JPG format. save (oFullName, System. drawing. imaging. imageFormat. jpeg); tImage. save (tFullName, System. drawing. imaging. imageFormat. jpeg);} catch (Exception ex) {throw ex;} finally {// release the resource oImage. dispose (); g. dispose (); tImage. dispose ();}}}

Here is another improvement method:

# Region upload an image and generate a thumbnail /// <summary> // upload the image to generate a thumbnail /// </summary> /// <param name = "originalImagePath"> Image Source path </param> /// <param name = "thumbnailPath"> thumbnail path (physical path) </param> /// <param name = "width"> thumbnail width </param> /// <param name = "height"> thumbnail height </param> // /<param name = "mode"> Create a thumbnail </param> public static void MakeThumbnail (string originalImagePath, string thumbnailPath, int width, int height, string mode) {// obtain the source Image File System from the path. drawing. image originalImage = System. drawing. image. fromFile (originalImagePath); int towidth = width; int toheight = height; int x = 0; int y = 0; // get the image width int ow = image. width; // get the image Height int oh = image. height; // generate the thumbnail mode switch (mode) {case "HW": break; case "W": // specify the width and Height based on the ratio toheight = originalImage. height * width/originalImage. width; break; case "H": // specify the image height and Width in proportion to towidth = originalImage. width * height/originalImage. height; break; case "Cut": // if it is in the Cut mode, if (double) originalImage is not deformed. width/(double) originalImage. height> (double) towidth/(double) toheight) {oh = originalImage. height; // The width of the thumbnail. ow = originalImage. height * towidth/toheight; y = 0; x = (originalImage. width-ow)/2;} else {ow = originalImage. width; // The height of the thumbnail. oh = originalImage. width * toheight/towidth; x = 0; y (originalImage. height-oh)/2;} break; default: break;} // create a new bmp image Bitmap bitmap = new Bitmap (towidth, toheight ); // create a new canvas with the BitMap width and height as the size of the canvas Graphics g = Graphics. fromImage (bitmap); // set the high quality interpolation method g. interpolationMode = InterpolationMode. high; // High quality and low speed presentation g. smoothingMode = SmoothingMode. highQuality; // clear the canvas and fill g with a white background color. clear (Color. transparent); // specify the position and size of the original image. drawImage (originalImage, new Rectangle (towidth, toheight), new Rectangle (x, y, ow, oh), GraphicsUnit. pixel); try {// Save the thumbnail bitmap in jpg format. save (thumbnailPath, System. drawing. imaging. imageFormat. jpeg);} catch (Exception ex) {throw ex;} finally {// release the resource originalImage. dispose (); bitmap. dispose (); g. dispose () ;}# endregion

I hope this article will help you design your asp.net program.

Articles you may be interested in:
  • ASP. NET stores images in binary format into the database
  • Asp.net saves an image as a binary value to the instance code in the Xml file.
  • Asp.net (c #) implements code for accessing binary images from sqlserver
  • Asp.net Remote Image Upload Method Based on Web Service
  • Implementation of ASP. NET gradient display images
  • Three typical ASP. NET image processing problems
  • Examples of Image Display Methods in ASP. NET
  • Asp.net implements two methods for outputting images in binary streams

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.