Drag a file upload control and a button on the aspx front-end page
...
<Div>
<Input id = "file1" runat = "server" type = "file"/>
<Asp: button id = "btnupload" runat = "server" text = "upload an image and generate a thumbnail" onclick = "btnupload_click"/>
</Div>
...
The following is an important method for generating thumbnails in the background:
/// <Summary>
/// Asp.net upload the image and generate a thumbnail
/// </Summary>
/// <Param name = "upimage"> htmlinputfile Control </param>
/// <Param name = "ssavepath"> saved path, which is the folder under the server 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 ssavepath, 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 image uploaded ";
// Obtain the extension of the selected upimage File
String extendname = system. Io. Path. getextension (myfile. filename). tolower ();
// Determine whether the image format is used
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 there are images of the same name 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 ();
// Upload the source Image
Try
{
// Source image loading
Using (system. Drawing. Image sourceimage = system. Drawing. image. fromfile (system. Web. httpcontext. Current. server. mappath (ssavepath + sfilename )))
{
// Width and height of the source Image
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;
}
// Determine whether the thumbnail exists in the same name file in the current folder
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;
}
// Absolute path for saving the thumbnail
String smallimagepath = system. Web. httpcontext. Current. server. mappath (ssavepath) + sthumbfile;
// Create a new graphic board and compress the source image to the minimum scale.
Using (system. Drawing. Image bitmap = new system. Drawing. Bitmap (smallwidth, smallheight ))
{
// Draw an intermediate Diagram
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 palette to draw an intermediate diagram in the thumbnail size
Using (system. Drawing. Image bitmap1 = new system. Drawing. Bitmap (intthumbwidth, intthumbheight ))
{
// Draw a 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 if an error occurs
System. 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 ";
}
The above is a general method, and then you can call this method in the button:
Protected void btnupload_click (Object sender, eventargs E)
{
String A = This. uploadimage (this. file1, "upload/", "Thumb _", 118,118 );
Response. Write ("<SCRIPT> alert ('upload and generate thumbnails successfully... ') </SCRIPT> ");
}