Print? /// Generate a thumbnail
/// </Summary>
/// <Param name = "originalImagePath"> source image path </param>
/// <Param name = "thumbnailPath"> thumbnail path </param>
/// <Param name = "width"> thumbnail width </param>
/// <Param name = "height"> thumbnail height </param>
/// <Param name = "mode"> how to generate a thumbnail: HW specifies the High-width Scaling (may be deformed); W specifies the width, and H specifies the height based on the ratio, width proportional Cut specifies high width Cut (not deformed) </param>
/// <Param name = "mode"> the format (gif, jpg, bmp, and png) to be saved as jpg if it is null or an unknown type </param>
Public static void MakeThumbnail (string originalImagePath, string thumbnailPath, int width, int height, string mode, string imageType)
{
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 (possibly deformed)
Break;
Case "W": // specify the width, and the height is proportional.
Toheight = originalImage. Height * width/originalImage. Width;
Break;
Case "H": // specify the height. The width is proportional.
Towidth = originalImage. Width * height/originalImage. Height;
Break;
Case "Cut": // specify the height and width (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 and smooth Low Speed
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 the thumbnail in jpg format
Switch (imageType. ToLower ())
{
Case "gif ":
Bitmap. Save (thumbnailPath, System. Drawing. Imaging. ImageFormat. Gif );
Break;
Case "jpg ":
Bitmap. Save (thumbnailPath, System. Drawing. Imaging. ImageFormat. Jpeg );
Break;
Case "bmp ":
Bitmap. Save (thumbnailPath, System. Drawing. Imaging. ImageFormat. Bmp );
Break;
Case "png ":
Bitmap. Save (thumbnailPath, System. Drawing. Imaging. ImageFormat. Png );
Break;
Default:
Bitmap. Save (thumbnailPath, System. Drawing. Imaging. ImageFormat. Jpeg );
Break;
}
}
Catch (System. Exception e)
{
Throw e;
}
Finally
{
OriginalImage. Dispose ();
Bitmap. Dispose ();
G. Dispose ();
}
}
/// Generate a thumbnail
/// </Summary>
/// <Param name = "originalImagePath"> source image path </param>
/// <Param name = "thumbnailPath"> thumbnail path </param>
/// <Param name = "width"> thumbnail width </param>
/// <Param name = "height"> thumbnail height </param>
/// <Param name = "mode"> how to generate a thumbnail: HW specifies the High-width Scaling (may be deformed); W specifies the width, and H specifies the height based on the ratio, width proportional Cut specifies high width Cut (not deformed) </param>
/// <Param name = "mode"> the format (gif, jpg, bmp, and png) to be saved as jpg if it is null or an unknown type </param>
Public static void MakeThumbnail (string originalImagePath, string thumbnailPath, int width, int height, string mode, string imageType)
{
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 (possibly deformed)
Break;
Case "W": // specify the width, and the height is proportional.
Toheight = originalImage. Height * width/originalImage. Width;
Break;
Case "H": // specify the height. The width is proportional.
Towidth = originalImage. Width * height/originalImage. Height;
Break;
Case "Cut": // specify the height and width (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 and smooth Low Speed
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 the thumbnail in jpg format
Switch (imageType. ToLower ())
{
Case "gif ":
Bitmap. Save (thumbnailPath, System. Drawing. Imaging. ImageFormat. Gif );
Break;
Case "jpg ":
Bitmap. Save (thumbnailPath, System. Drawing. Imaging. ImageFormat. Jpeg );
Break;
Case "bmp ":
Bitmap. Save (thumbnailPath, System. Drawing. Imaging. ImageFormat. Bmp );
Break;
Case "png ":
Bitmap. Save (thumbnailPath, System. Drawing. Imaging. ImageFormat. Png );
Break;
Default:
Bitmap. Save (thumbnailPath, System. Drawing. Imaging. ImageFormat. Jpeg );
Break;
}
}
Catch (System. Exception e)
{
Throw e;
}
Finally
{
OriginalImage. Dispose ();
Bitmap. Dispose ();
G. Dispose ();
}
}