Generate small images for images-generate thumbnails

Source: Internet
Author: User
Tags bmp image

In the past few days, I have to upload two identical images with different sizes. I used two <input type = "file"/> controls to upload images separately; A colleague (zendic [http://www.z-endic.info/]) said it seems to be able to generate a small picture for a large image; then I went online to find some information, found really can; copy the classes implemented on the Internet and modify the results;

Now let's write down the entire implementation process:

Create a publicmethods class and write the following in the class:Code:

Public static void makethumbnail (string originalimagepath, string thumbnailpath, int width, int height, string Mode)
{
System. Drawing. Image originalimage = system. Drawing. image. fromfile (originalimagepath );

Int towidth = width;
Int toheight = height;

Int x = 0;
Int y = 0;
Int ow = originalimage. width;
Int Oh = originalimage. height;

Switch (Mode)
{
Case "HW": // specify high-width Scaling (possibly deformed)
Break;
Case "W": // specify the width, and the height is proportional.
Toheight = originalimage. Height * width/originalimage. width;
Break;
Case "H": // specify the height. The width is proportional.
Towidth = originalimage. Width * Height/originalimage. height;
Break;
Case "cut": // specify the height and width (not deformed)
If (double) originalimage. width/(double) originalimage. Height> (double) towidth/(double) toheight)
{
Oh = originalimage. height;
Ow = originalimage. Height * towidth/toheight;
Y = 0;
X = (originalimage. Width-ow)/2;
}
Else
{
Ow = originalimage. width;
Oh = originalimage. Width * Height/towidth;
X = 0;
Y = (originalimage. Height-OH)/2;
}
Break;
Default:
Break;
}

// Create a BMP Image
System. Drawing. Image bitmap = new system. Drawing. Bitmap (towidth, toheight );

// Create a canvas
System. Drawing. Graphics G = system. Drawing. Graphics. fromimage (Bitmap );

// Set a high quality Interpolation Method
G. interpolationmode = system. Drawing. drawing2d. interpolationmode. High;

// Set high quality and smooth Low Speed
G. smoothingmode = system. Drawing. drawing2d. smoothingmode. highquality;

// Clear the canvas and fill it with a transparent background color
G. Clear (system. Drawing. color. Transparent );

// Draw the specified part of the original image at the specified position and in the specified size
G. drawimage (originalimage, new system. Drawing. rectangle (0, 0, towidth, toheight ),
New system. Drawing. rectangle (X, Y, ow, oh ),
System. Drawing. graphicsunit. pixel );

Try
{
// Save the thumbnail in JPG format
Bitmap. Save (thumbnailpath, system. Drawing. imaging. imageformat. JPEG );
}
Catch (system. Exception E)
{
Throw E;
}
Finally
{
Originalimage. Dispose ();
Bitmap. Dispose ();
G. Dispose ();
}
}

Write an upload function in the background:

Public void uploadfile (htmlinputfile imgfile)
{
String STR = imgfile. postedfile. filename;
If (STR = string. Empty)
{
Return;
}
Else
{
String filename = system. Io. Path. getfilename (STR );
String filename_s = datetime. Now. tostring ("yyyymmddhhmmss") + filename; // name of the thumbnail File

string webfilepath = server. mappath (".. \ images \ "+ filename); // upload a large image
string webfilepath_s = server. mappath (".. \ images \ "+ filename_s); // upload a small image

If (! File. exists (webfilepath)
{< br> imgfile. postedfile. saveas (webfilepath);
publicmethods. makethumbnail (webfilepath, webfilepath_s, 50, 50, "cut"); // call the thumbnail method
}

// The following operations are performed to write data to the database
string Path = ".. \ images \ "+ filename;
string path_s = ".. \ images \ "+ filename_s;
string imgurl = path. tostring (). replace ("\", "/");
string imgurl_s = path_s.tostring (). replace ("\", "/");

// Save the relative paths of the two images in the project to the two label controls.
Label. Text = imgurl;
Label_s.text = imgurl_s;
}
}

When writing the upload control at the front end, you must write the client as the server

<Input name = "imgfile" type = "file" id = "imgfile" runat = "server" size = "40"/>

Finally, call it in the background:

Uploadfile (imgfile); then OK;

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.