<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default2.aspx. cs" Inherits = "Default2" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Input id = "File1" runat = "server" type = "file"/> </div> <asp: button ID = "Button1" runat = "server" OnClick = "button#click" Text = "Button"/>
</Form>
</Body>
</Html>
Protected void button#click (object sender, EventArgs e)
{
String a = this. UpLoadImage (this. File1, "UpLoad/", "thumb _", 118,118 );
}
/// <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 ";
}