Code
Using System;
Using System. Web;
Using System. Web. UI;
Using System. IO;
Using System. Drawing;
Using System. Drawing. Imaging;
Using System. Drawing. Drawing2D;
Namespace {
/// <Summary>
/// Summary of UploadFile.
/// </Summary>
Public class UploadFile
{
Public UploadFile ()
{
//
// TODO: add the constructor logic here
//
}
/// <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 + = 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 ("yyyyMMddHmmssfff ");
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 + "/" + datestr + ext;
// Multiples of the zoom-out
Int iScale = 1;
// Obtain an image object from a file
Image image = null;
Try
{
Image = Image. FromFile (originalFilename );
}
Catch
{
//
Try
{
File. Delete (originalFilename );
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
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
{
Sqlfile = "";
}
Return sqlfile;
}
}
}
Upload image processing C #