/**/ /**/ /**/ /// <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)
{
Image originalimage = Image. fromfile (originalimagepath );
Int Towidth = Width;
Int Toheight = Height;
Int X = 0 ;
Int Y = 0 ;
Int Ow = Originalimage. width;
Int Oh = Originalimage. height;
Switch (Mode)
{
Case " HW " : // Specify high-width Scaling (may be deformed)
Break ;
Case " W " : // Specify width and height in proportion
Toheight = Originalimage. Height * Width / Originalimage. width;
Break ;
Case " H " : // Specify height, width by proportion
Towidth = Originalimage. Width * Height / Originalimage. height;
Break ;
Case " Cut " : // Specify height and width reduction (not deformed)
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 ;
Default :
Break ;
}
// Create a BMP Image
Image bitmap = New System. Drawing. Bitmap (towidth, toheight );
// Create a canvas
Graphics g = System. Drawing. Graphics. fromimage (Bitmap );
// Set a high quality Interpolation Method
G. interpolationmode = System. Drawing. drawing2d. interpolationmode. High;
// Set high quality, low speed rendering Smoothness
G. smoothingmode = System. Drawing. drawing2d. smoothingmode. highquality;
// Clear the canvas and fill it with a transparent background color
G. Clear (color. Transparent );
// Draw the specified part of the original image at the specified position and in the specified size
G. drawimage (originalimage, New Rectangle ( 0 , 0 , Towidth, toheight ),
New Rectangle (X, Y, ow, oh ),
Graphicsunit. pixel );
Try
{
//Save thumbnails in JPG format
Bitmap. Save (thumbnailpath, system. Drawing. imaging. imageformat. JPEG );
}
Catch (System. Exception E)
{
ThrowE;
}
Finally
{
Originalimage. Dispose ();
Bitmap. Dispose ();
G. Dispose ();
}
}
For the key method graphics. drawimage, see
MS-help: // Ms. netframeworksdkv1.1.chs/cpref/html/frlrfsystemdrawinggraphicsclassdrawimagetopic11.htm