. Net upload image size thumbnail

Source: Internet
Author: User
Tags bmp image

Public String getsqlfile (fileupload FP, int width, int height)
{
String sqlfile = "";
String tempsqlpath = "";
String Path = httpcontext. Current. Request. physicalapplicationpath. tostring () + "upload /";
Path + = "uploadfile/" + datetime. Now. tostring ("yyyymm ");
Tempsqlpath = "\ upload \ uploadfile \" + datetime. Now. tostring ("yyyymm ");
If (directory. exists (PATH ))
{

}
Else
{
Directory. createdirectory (PATH );
}
If (directory. exists (path + "/small "))
{
}
Else
{
Directory. createdirectory (path + "/small ");
}
If (directory. exists (path + "/Temp "))
{
}
Else
{
Directory. createdirectory (path + "/Temp ");
}
// Httpfilecollection files = httpcontext. Current. Request. files;

If (FP. filename. tostring (). length> 0)
{
String filename = FP. filename. tostring ();
String datestr = datetime. Now. tostring ("yyyymmddhmmssfff ");
String ext = filename. substring (filename. lastindexof ("."). tolower ();
If (EXT! = ". BMP" & ext! = ". Jpg" & ext! = ". GIF" & ext! = ". Jpeg ")
{
Httpcontext. Current. response. Write ("<SCRIPT> alert('file uploaded is not in. GIF, JPG, JPEG, BMP format ') </SCRIPT> ");
Return "";
}
String tempfilepath = path + "/" + "Temp/" + datestr + ext;
FP. saveas (tempfilepath );
# Region generate a thumbnail
String originalfilename = path + "/" + datestr + ext;
// FP. filebytes
// Multiples of the zoom-out
Int iscale = 1;
// Obtain an image object from a file
System. Drawing. Image image = NULL;
Try
{
Image = system. Drawing. image. fromfile (tempfilepath );

}
Catch
{
//
Try
{
File. Delete (tempfilepath );
Image. Dispose ();

}
Catch
{
}
Httpcontext. Current. response. Write ("<SCRIPT> alert('file uploaded is not in the standard format of .gif, JPG, JPEG, and BMP ') </SCRIPT> ");
Return "";

}
Int HI = 0;
Int Wi = 0;
Wi = width;
Hi = height;
Size size = new size (WI, hi );
// Create a BMP Image
System. Drawing. Image bitmap = new Bitmap (size. Width, size. Height );
// Create a canvas
Graphics G = graphics. fromimage (Bitmap );
// Set a high quality Interpolation Method
G. interpolationmode = interpolationmode. High;
// Set high quality and smooth Low Speed
G. smoothingmode = smoothingmode. highquality;
// Clear the canvas
G. Clear (color. Blue );
// Draw a picture at a specified position
G. drawimage (image, new rectangle (0, 0, bitmap. width, bitmap. height), new rectangle (0, 0, image. width, image. height), graphicsunit. pixel );
If (EXT = ". jpg" | ext = ". jpeg ")
Bitmap. Save (path + "/small/" + datestr + Ext, imageformat. JPEG );
If (EXT = ". GIF ")
Bitmap. Save (path + "/small/" + datestr + Ext, imageformat. GIF );
If (EXT = ". BMP ")
Bitmap. Save (path + "/small/" + datestr + Ext, imageformat. BMP );
/// Large image
If (FP. filebytes. Length & gt; 300000 ){
// Image. Width
// Image. Height

Wi = 800;

Hi = convert. toint32 (image. Height * (convert. todouble (WI)/convert. todouble (image. width )));
Size = new size (WI, hi );
// Create a BMP Image
Bitmap = new Bitmap (size. Width, size. Height );
// Create a canvas
G = graphics. fromimage (Bitmap );
// Set a high quality Interpolation Method
G. interpolationmode = interpolationmode. High;
// Set high quality and smooth Low Speed
G. smoothingmode = smoothingmode. highquality;
// Clear the canvas
G. Clear (color. Blue );
// Draw a picture at a specified position
G. drawimage (image, new rectangle (0, 0, bitmap. width, bitmap. height), new rectangle (0, 0, image. width, image. height), graphicsunit. pixel );
If (EXT = ". jpg" | ext = ". jpeg ")
Bitmap. Save (originalfilename, imageformat. JPEG );
If (EXT = ". GIF ")
Bitmap. Save (originalfilename, imageformat. GIF );
If (EXT = ". BMP ")
Bitmap. Save (originalfilename, imageformat. BMP );

}< br> else {
FP. saveas (originalfilename);
}< br> image. dispose ();
bitmap. dispose ();
G. dispose ();
# endregion
sqlfile = datestr + ext;

Try
{
Image. Dispose ();
Bitmap. Dispose ();
G. Dispose ();
File. Delete (tempfilepath );
}
Catch (exception ex)
{
String exc = ex. Message. tostring ();
Httpcontext. Current. response. Write ("<SCRIPT> alert ('" + exc + "'); </SCRIPT> ");
}
}
Else
{
Sqlfile = "";
}
Sqlfile = sqlfile. length> 0? (Tempsqlpath + "\" + sqlfile ):("");
Return sqlfile;
}

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.