. Net upload image encapsulation class

Source: Internet
Author: User
Tags bmp image

Using System;
Using System. Web;
Using System. Web. UI;
Using System. IO;
Using System. Drawing;
Using System. Drawing. Imaging;
Using System. Drawing. Drawing2D;

Namespace webdb. classes
{
/// <Summary>
/// Summary of UploadFile.
/// </Summary>
Public class UploadFile
{
Public UploadFile ()
{
//
// TODO: add the constructor logic here
//
}
/// <Summary>
/// Upload a file without Parameters
/// </Summary>
/// <Returns> </returns>
Public string GetSqlFile ()
{

String sqlfile = "";
String path = HttpContext. Current. Request. PhysicalApplicationPath. ToString () + "/upload ";
If (Directory. Exists (path ))
{

}
Else
{
Directory. CreateDirectory (path );
}
HttpFileCollection files = HttpContext. Current. Request. Files;
If (files [0]. FileName. ToString (). Length> 0)
{
String filename = files [0]. FileName. ToString ();
String datestr = DateTime. Now. ToString ("yyyyMMddHmmss") + DateTime. Now. Millisecond;
String ext = filename. Substring (filename. LastIndexOf ("."));
Files [0]. SaveAs (path + "file: //" + datestr + ext /);
Sqlfile = datestr + ext;
}
Else
{

}
Return sqlfile;
}
Public string GetSqlFile (string dir)
{
String sqlfile = "";
String path = HttpContext. Current. Request. PhysicalApplicationPath. ToString () + "/upload /";
Path + = "file: //" + dir /;
If (Directory. Exists (path ))
{

}
Else
{
Directory. CreateDirectory (path );
}
HttpFileCollection files = HttpContext. Current. Request. Files;


If (files [0]. FileName. ToString (). Length> 0)
{
String filename = files [0]. FileName. ToString ();
String datestr = DateTime. Now. ToString ("yyyyMMddHmmss") + DateTime. Now. Millisecond;
String ext = filename. Substring (filename. LastIndexOf ("."));
Files [0]. SaveAs (path + "/" + datestr + ext );
Sqlfile = datestr + ext;
}
Else
{

}
Return sqlfile;
}
/// <Summary>
/// Specify the path image size
/// </Summary>
/// <Param name = "dir"> </param>
/// <Param name = "width"> </param>
/// <Param name = "height"> </param>
/// <Returns> </returns>
Public string GetSqlFile (string dir, int width, int height)
{
String sqlfile = "";
String path = HttpContext. Current. Request. PhysicalApplicationPath. ToString () + "/upload /";
Path + = "file: //" + dir /;
If (Directory. Exists (path ))
{

}
Else
{
Directory. CreateDirectory (path );
}
If (Directory. Exists (path + "/small "))
{

}
Else
{
Directory. CreateDirectory (path + "/small ");
}
HttpFileCollection files = HttpContext. Current. Request. Files;


If (files [0]. FileName. ToString (). Length> 0)
{
String filename = files [0]. FileName. ToString ();
String datestr = DateTime. Now. ToString ("yyyyMMddHmmss") + DateTime. Now. Millisecond;
String ext = filename. Substring (filename. LastIndexOf ("."));
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 "";
}
Files [0]. SaveAs (path + "/" + datestr + ext );
# Region generate a thumbnail
String originalFilename = path + "file: //" + datestr + ext /;
// Generated high-quality image name
String strGoodFile = path + "file: //" + datestr + ext /;
// Generated low-quality image name
// String strBadFile = mPath + "\" + newname;
// Multiples of the zoom-out
Int iScale = 1;
// Obtain an image object from a file
Image image = Image. FromFile (originalFilename );
// Obtain the image size
// Size size = new Size (image. Width/iScale, image. Height/iScale );
Int hi = 0;
Int wi = 0;
// If (image. Width> 100)
//{
Wi = width;
//}
// Else
//{

//}
// If (image. Height> 55)
//{
Hi = height;
//}
// Else
//{

//}
Size size = new Size (wi, hi );
// Create a bmp Image
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 );
Image. Dispose ();
Bitmap. Dispose ();
G. Dispose ();
# Endregion
Sqlfile = datestr + ext;

Try
{
Image. Dispose ();
Bitmap. Dispose ();
G. Dispose ();

}
Catch (Exception ex)
{
String exc = ex. Message. ToString ();
HttpContext. Current. Response. Write ("<script> alert ('" + exc + "'); </script> ");
}
}
Else
{

}
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.