20 tips for ASP. net mvc 3 development (12) [20 recipes for programming MVC 3]: Scaling image sizes to create thumbnails

Source: Internet
Author: User

Topics

Most of the images uploaded by users to the website are large-sized photos. Generally, the website displays thumbnails of these images or photos before users want to see the complete images.

Solution

Use the following classes to adjust the width and height of the uploaded image files: filestream, image, bitmap, and graphics.

Discussion

In the following example, the previously created fileupload class is modified and reorganized. Create a new method called "resizeimage" to resize the image. The adjusted image file will be saved to the "thumbnails" sub-folder in the source file storage folder. You also need to modify the deletefile method to add and delete both the original image and thumbnail, and to avoid duplicationCodeCreate a method for deleting a function. The following shows the class code of the changed part:

 Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using System. IO;
Using System. drawing;
Using System. Drawing. drawing2d;
Namespace Mvcapplication4.utils
{
Public Static Class Fileupload
{
Public Static Char Dirseparator =
System. Io. Path. directoryseparatorchar;
Public Static String Filespath = " Content " +
Dirseparator + " Uploads " + Dirseparator;
Public Static String Uploadfile (httppostedfilebase file)
{
...
// Save thumbnail
Resizeimage (file, 150 , 100 );
...
}
Public Static Void Deletefile ( String Filename)
{
// If no file name is specified, nothing will be done.
If (Filename. Length = 0 ) Return ;
// Set the deletion path
String Path = filespath + dirseparator + filename;
String Thumbpath = filespath + dirseparator +
" Thumbnails " + Dirseparator + filename;
Removefile (PATH );
Removefile (thumbpath );
}
Private Static Void Removefile ( String Path)
{
// Check whether the file exists
If (File. exists (path. getfullpath (PATH )))
{
// Delete an object
File. Delete (path. getfullpath (PATH ));
}
}
Public Static Void Resizeimage (httppostedfilebase file,
Int Width, Int Height)
{
String Thumbnaildirectory =
String. Format ( @" {0} {1} {2} " , Filespath,
Dirseparator, " Thumbnails " );
// Check whether the target folder exists
If (! Directory. exists (thumbnaildirectory ))
{
// Create a folder if it does not exist.
Directory. createdirectory (thumbnaildirectory );
}
// Set the path for saving thumbnails
String ImagePath =
String. Format ( @" {0} {1} {2} " , Thumbnaildirectory,
Dirseparator, file. filename );
// Save the file stream to the disk
Filestream stream = New Filestream (path. getfullpath (
ImagePath), filemode. openorcreate );
// Zooming uploaded files
Image origimage = image. fromstream (file. inputstream );
// Create a thumbnail object
Bitmap tempbitmap = New Bitmap (width, height );
// Create thumbnail image quality
Graphics newimage = graphics. fromimage (tempbitmap );
Newimage. compositingquality =
Compositingquality. highquality;
Newimage. smoothingmode =
Smoothingmode. highquality;
Newimage. interpolationmode =
Interpolationmode. highqualitybicubic;
// Create a rectangle object to draw
Rectangle imagerectangle = New Rectangle ( 0 , 0 ,
Width, height );
Newimage. drawimage (origimage, imagerectangle );
// Save thumbnail
Tempbitmap. Save (stream, origimage. rawformat );
// Release resources
Newimage. Dispose ();
Tempbitmap. Dispose ();
Origimage. Dispose ();
Stream. Close ();
Stream. Dispose ();
}
}
}

In the above example, We made many modifications, especially the resizeimage method. First, determine whether the "thumbnails" folder exists and the Creation function. Next, a new filestream object is created and the edited image is saved to the "thumbnails" folder.

Create the original image object from the submitted inputstream. Then, a bitmap Bitmap bitmap instance is created based on the size of the thumbnail. Create a new graphics object based on this bitmap object, and set the image quality, smoothness, and interpolation mode. If these values are not set, thumbnails are difficult to see due to pixel and proportional deformation.

After setting these values, create a recangle object of the original size and scale the object to the previously created graphics object. Then, adjust the size. Finally, save the bitmap object and release all resources.

Reference

Filestream image bitmap graphics original book Address Book source code

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.