ASP. net2.0 uploads images (fileupload control) and generates thumbnails

Source: Internet
Author: User
Tags bmp image

Record, Memo:

(1)

UsingSystem. IO;// Add

(2)

Put a fileupload control on the page with the ID "fileupload1" and an upload button with the ID "btnupload ".

///


/// " Upload "button
///
///
//
protected void btnupload_click ( Object sender, eventargs E)
{< br> string serverpath = server. mappath ( "~ /Images/product "); // set the image path

If(This. Fileupload1.hasfile)
{
StringName = fileupload1.filename;
StringFname =This. Txtimagename. text;// New image name. If this parameter is not specified, the original image name is used.
// String type = fname. substring (fname. lastindexof (".") + 1). tolower ();

StringType = Name. substring (name. lastindexof (".") + 1). tolower ();

If(String. Isnullorempty (fname ))

{
Fname = Name;
}
Else

Fname + = "." + type;

 

If (Type = "Jpg" | Type = "PNG" | Type = "BMP" | Type = "GIF" | Type = "Jpeg" )
{
Uploadimage (serverpath, fname ); // Upload the image and generate a thumbnail
}
Else
{
Httpcontext . Current. response. Write ("<SCRIPT> alert ('the image format is incorrect! '); </SCRIPT>" );
Return ;
}
}
Else
{
Httpcontext . Current. response. Write ( "<SCRIPT> alert ('select the file to upload ') </SCRIPT>" );
Return ;
}
Httpcontext . Current. response. Write ( "<SCRIPT> alert ('file uploaded successfully') </SCRIPT>" );
}

/// <Summary>
/// Upload images and generate thumbnails
/// </Summary>
/// <Param name = "serverpath"> </param>
/// <Param name = "fname"> </param>
Private void Uploadimage ( String Serverpath, String Fname)
{
If (! File . Exists (serverpath ))
{
Directory . Createdirectory (serverpath );
Directory . Createdirectory (serverpath + "_ Thumb" );
}
String Fpath = serverpath + "\\" + Fname; // Actual file path

StringTpath = serverpath +"_ Thumb \\"+ Fname;// Thumbnail path

If (! File . Exists (tpath )&&! File . Exists (fpath ))
{
This . Fileupload1.saveas (fpath ); // Upload to image path
Imageclass. Makethumbnail (fpath, tpath, 180,117,"W"); // Call the makethumbnail () method of the imageclass classTo generate a thumbnail.
This . Myimage. imageurl = "~ /Images/product_thumb" + "\\" + Fname;
}
Else
{
Httpcontext . Current. response. Write ( "<SCRIPT> alert ('the image already exists! '); </SCRIPT>" );
Return ;
}
}

(3) imageclass class (if you forget the URL)

// Add
UsingSystem. Data;
UsingSystem. configuration;
UsingSystem. Web. UI;
UsingSystem. IO;

/// <Summary>
/// Generate a thumbnail
/// </Summary>
/// <Param name = "originalimagepath"> Source image path (physical 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"> How to generate a thumbnail </Param>
Public static void Makethumbnail (String Originalimagepath, String Thumbnailpath, Int Width, Int Height, String Mode)
{
System. Drawing. Image Originalimage = system. Drawing. Image . Fromfile (originalimagepath );

IntTowidth = width;
IntToheight = height;

IntX = 0;
IntY = 0;
IntOw = originalimage. width;
IntOh = originalimage. height;

If(Ow <towidth & OH <toheight)
{
Originalimage. Save (thumbnailpath );
}
Else
{

Switch (Mode. toupper ())
{
Case "HW" : // Specify high-width Scaling (may be 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 reduction (without deformation)
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 ;
Case "Auto" : // Automatically adapts to the height
If (Ow> OH)
{
// Newwidth = 200;
Toheight = ( Int )((Double ) OH /( Double ) Ow *( Double ) Towidth );
}
Else
{
// Newheight = 200;
Towidth = (Int )(( Double ) Ow /( Double ) Oh *( Double ) Toheight );
}
Break ;
Default :
Break ;
}

// Create a BMP Image
System. Drawing.ImageBitmap =NewSystem. Drawing.Bitmap(Towidth, toheight );

// Create a canvas
System. Drawing.GraphicsG = 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 );

// specify the position and size of the original image.
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.ExceptionE)
{
ThrowE;
}
Finally
{

Bitmap. Dispose ();
G. Dispose ();
}
}
Originalimage. Dispose ();
}

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.