/** // <Summary>
/// Upload an image with a thumbnail
/// </Summary>
/// <Param name = "fileupload1"> ID of the uploaded Control </param>
/// <Param name = "savepath"> Save the path, such as "/images/picture/". Use the configuration file </param>
/// <Param name = "refname"> name of the returned file, without path </param>
/// <Param name = "width"> width of the thumbnail </param>
/// <Param name = "height"> height of the thumbnail </param>
/// <Param name = "maxlength"> Maximum image size </param>
/// <Returns> </returns>
Public bool uploadimage (htmlinputfile fileupload1, string savepath,
Ref string refname, int width, int height, int maxlength)
{
// Use the system path when converting
Savepath = "~ "+ Savepath;
/** // Success status
Bool ret = true;
/** // The name Group of the uploaded file
String filenames = string. empty;
// The total size of the uploaded file
Int filelength = 0;
// Image type
String imgtype = string. empty;
// Image type set
Icollection Ic = new string [] {". GIF", ". jpg", ". GIF", ". jpg "};
Arraylist imgtypes = new arraylist (IC );
/** // Determine the validity of the uploaded file
/// Check the file extension name
String name = system. Io. Path. getfilename (fileupload1.value );
If (name = "")
{
/** // Status information
Ret = false;
Refname = "the image does not exist ";
Return false;
}
Imgtype = system. Io. Path. getextension (fileupload1.value );
If (! Imgtypes. Contains (imgtype ))
{
/** // Status information
Ret = false;
Refname = "incorrect image format ";
Return false;
}
Filelength = filelength + fileupload1.postedfile. contentlength;
// The size specified by the System
Int syslength = maxlength * 1024;
If (filelength> syslength)
{
/** // Status information
Ret = false;
Refname = "the maximum size of the image exceeds kb! ";
Return false;
}
/** // Upload an image
Try
{
String picpath = savepath;
String filename, fileextension;
/** // File Name of the uploaded file
Filename = system. Io. Path. getfilename (fileupload1.value );
If (filename! = "")
{
/** // The extension of the uploaded file
Fileextension = system. Io. Path. getextension (filename );
/** // Generate a new file name
Filename = datetime. Now. tostring ("yyyymmddhhmmffff") + fileextension;
/** // You can save the name to a different folder.
String date = datetime. Now. tostring ("yyyymm ");
Picpath = server. mappath (picpath + date + "/");
System. Io. directoryinfo di = new system. Io. directoryinfo (picpath );
If (! Di. exists)
{
Di. Create ();
}
/** // Note: you may need to modify the anonymous write permission for your folder.
Fileupload1.postedfile. saveas (picpath + filename );
Refname = date + "/" + filename;
Getthumbnailimage (savepath, date + "/", filename, width, height );
}
}
Catch
{
Ret = false;
Refname = "Upload Failed! ";
Return false;
}
Return ret;
}
Code for generating thumbnails:
/** // <Param name = "savepath"> original path </param>
/// <Param name = "pathdate"> path of the added month </param>
/// <Param name = "picfilepath"> saved file name </param>
/// <Param name = "width"> width </param>
/// <Param name = "height"> height </param>
Public void getthumbnailimage (string savepath, string pathdate, string picfilepath, int width, int height)
{
// Add the size of the small100 prefix
// Small image of the relative server path in the program
String strrelativesmallpath = savepath + commenum. pictures_small100 + pathdate;
String smallpath100 = server. mappath (strrelativesmallpath + picfilepath );
String machpath = server. mappath (savepath + pathdate + picfilepath );
String isdir = smallpath100.substring (0, smallpath100.lastindexof ('\'));
System. Io. directoryinfo di = new system. Io. directoryinfo (isdir );
If (! Di. exists)
{
Di. Create ();
}
Bitmap IMG = new Bitmap (machpath); // read picture to memory
Int H = IMG. height;
Int W = IMG. width;
Int SS, OS; // source side and objective side
Double temp1, temp2;
// Compute the picture's proportion
Temp1 = (H * 1.0d)/height;
Temp2 = (w * 1.0d)/width;
If (temp1 <temp2)
{
Ss = W;
OS = width;
}
Else
{
Ss = h;
OS = height;
}
Double per = (OS * 1.0d)/SS;
If (per <1.0d)
{
H = (INT) (H * per );
W = (INT) (W * per );
}
// Create the thumbnail image
System. Drawing. Image imag2 = IMG. getthumbnailimage (W, H,
New system. Drawing. image. getthumbnailimageabort (imageabort ),
Intptr. Zero );
Bitmap tempbitmap = new Bitmap (W, H );
System. Drawing. Image tempimg = system. Drawing. image. fromhbitmap (tempbitmap. gethbitmap ());
System. Drawing. Graphics G = system. Drawing. Graphics. fromimage (tempimg );
G. Clear (color. White );
Int X, Y;
X = (tempimg. Width-imag2.width)/2;
Y = (tempimg. Height-imag2.height)/2;
G. drawimage (imag2, X, Y, imag2.width, imag2.height );
Try
{
If (IMG! = NULL)
IMG. Dispose ();
If (imag2! = NULL)
Imag2.dispose ();
If (tempbitmap! = NULL)
Tempbitmap. Dispose ();
String fileextension = system. Io. Path. getextension (machpath). tolower ();
// Save the thumbnail based on the original image type. If the image is not in the original format, the image may be blurred or jagged.
Switch (fileextension)
{
Case ". GIF": tempimg. Save (smallpath100, imageformat. GIF); break;
Case ". jpg": tempimg. Save (smallpath100, imageformat. JPEG); break;
Case ". BMP": tempimg. Save (smallpath100, imageformat. BMP); break;
Case ". PNG": tempimg. Save (smallpath100, imageformat. PNG); break;
}
}
Catch
{
Throw new exception ("Image Upload Failed ");
}
Finally
{
// Release the memory
If (tempimg! = NULL)
Tempimg. Dispose ();
If (G! = NULL)
G. Dispose ();
}
}
Appendix:Image. getthumbnailimage method:
I
In general, using getthumbnailimage to generate image thumbnails still works. Generally, the JPG format is good, but sometimes it may cause problems. For example
When the source image size is too large, the quality of the generated thumbnail will be very low ", and" when the source image is a GIF image and contains transparent colors, the generated thumbnail will fill the transparent color with black ", if
This is not a good result, especially when it exceeds 120. "According to msdn:
Image. getthumbnailimage Method
Returns the thumbnail of the image object.
[Visual
Basic] public function getthumbnailimage (_ byval thumbwidth
Integer, _ byval thumbheight as integer, _ byval callback
Image. getthumbnailimageabort, _ byval callbackdata as intptr _)
Image [C #] public image getthumbnailimage (INT thumbwidth, int
Thumbheight, image. getthumbnailimageabort callback, intptr
Callbackdata); [C ++] public: Image * getthumbnailimage (int
Thumbwidth, int thumbheight, image. getthumbnailimageabort *
Callback, intptr callbackdata); [JScript] Public Function
Getthumbnailimage (thumbwidth: int, thumbheight: int,
Callback: image. getthumbnailimageabort, callbackdata: intptr ):
Image;
Parameters
Thumbwidth
The width of the requested Thumbnail (in pixels ).
Thumbheight
The height (in pixels) of the requested thumbnail ).
Callback
An image. getthumbnailimageabort delegate. This delegate is not used in GDI + 1.0. Even so, you must create a delegate and pass a reference to the delegate in this parameter.
Callbackdata
It must be intptr. Zero.
Return Value
The image object used to represent the thumbnail.
Remarks
If an image object contains an embedded thumbnail image, this method retrieves the embedded thumbnail and scales it to the desired size. If the image object does not contain an embedded thumbnail image, this method creates a thumbnail image by zooming the main image.
When
When the requested thumbnail is about 120x120, getthumbnailimage works normally. If an image with an embedded thumbnail
When a large thumbnail image (such as 300x300) is requested, the quality of the thumbnail image is significantly reduced. Call drawimage
Zooming the main image (rather than the embedded thumbnail), the effect may be better.
Example
[C #] the following example creates and displays a thumbnail image. Never call this delegate.
[C #]
Public bool thumbnailcallback () {return false;} public void
Example_getthumb (painteventargs e) {image. getthumbnailimageabort
Mycallback = new image. getthumbnailimageabort (thumbnailcallback); bitmap
Mybitmap = new Bitmap ("climber.jpg"); image mythumbnail =
Mybitmap. getthumbnailimage (40, 40, mycallback,
Intptr. Zero); E. Graphics. drawimage (mythumbnail, 150, 75 );}
This article is reprinted