There are two parts: one is the operation class and the other is the test. Code directly
I. Test code
Private void button#click (object sender, EventArgs e)
{
String newSourcePath = ImgPath; // source image storage directory
String newNewDir = MakePath; // directory where the new graph is stored
String sourceFile = Path. Combine (ImgPath, "app1.jpg"); // obtain the source image Path.
String newFile = string. Empty; // path of the New Graph
ImgThumbnail iz = new ImgThumbnail ();
Action Thumbnail = (type =>
{
// Generate quality
For (int I = 100; I> = 10; I-= 10)
{
// Calculate the path of the New Graph. Name the test: original image name _new_scaling 1__compression quality .jpg
NewFile = Path. Combine (newNewDir, string. Format ("appenders new_000011__000000000.jpg", I, type. ToString ()));
// Compress the image
Iz. Thumbnail (sourceFile, newFile, 100,100, I, type );
}
});
Thumbnail (ImgThumbnail. ImgThumbnailType. Cut); // cropping and compression (cropping and compression are not easy to use yet, to be adjusted !)
Thumbnail (ImgThumbnail. ImgThumbnailType. H); // Compress With reference height without deformation
Thumbnail (ImgThumbnail. ImgThumbnailType. W); // Compress With the width as the reference, without deformation
Thumbnail (ImgThumbnail. ImgThumbnailType. WH); // fixed width and height compression, Deformation
MessageBox. Show ("finished ");
}
Ii. Operation
Using System. Drawing;
Using System. Drawing. Drawing2D;
Using System. Drawing. Imaging;
Namespace MagickNet. App
{
/// <Summary>
/// Image Compression
/// </Summary>
Public class ImgThumbnail
{
/// <Summary>
/// Specify the scaling type
/// </Summary>
Public enum ImgThumbnailType
{
/// <Summary>
/// Specify high-width Scaling (may be deformed)
/// </Summary>
WH = 0,
/// <Summary>
/// Specify the width, and the height is proportional.
/// </Summary>
W = 1,
/// <Summary>
/// Specify the height, and the width is proportional
/// </Summary>
H = 2,
/// <Summary>
/// Specify the height and width reduction (not deformed)
/// </Summary>
Cut = 3
}
# Region Thumbnail
/// <Summary>
/// Lossless compression
/// </Summary>
/// <Param name = "sFile"> original image </param>
/// <Param name = "dFile"> storage location after compression </param>
/// <Param name = "height"> height </param>
/// <Param name = "width"> </param>
/// <Param name = "flag"> Compression Quality: 1-100 </param>
/// <Param name = "type"> compression and scaling type </param>
/// <Returns> </returns>
Public bool Thumbnail (string sFile, string dFile, int height, int width, int flag, ImgThumbnailType type)
{
System. Drawing. Image iSource = System. Drawing. Image. FromFile (sFile );
ImageFormat tFormat = iSource. RawFormat;
// Width and height after scaling
Int towidth = width;
Int toheight = height;
//
Int x = 0;
Int y = 0;
Int ow = iSource. Width;
Int oh = iSource. Height;
Switch (type)
{
Case ImgThumbnailType. WH: // specify high-width Scaling (may be deformed)
{
Break;
}
Case ImgThumbnailType. W: // specify the width, and the height is proportional.
{
Toheight = iSource. Height * width/iSource. Width;
Break;
}
Case ImgThumbnailType. H: // specify the height. The width is proportional.
{
Towidth = iSource. Width * height/iSource. Height;
Break;
}
Case ImgThumbnailType. Cut: // specify the height and width (not deformed)
{
If (double) iSource. Width/(double) iSource. Height> (double) towidth/(double) toheight)
{
Oh = iSource. Height;
Ow = iSource. Height * towidth/toheight;
Y = 0;
X = (iSource. Width-ow)/2;
}
Else
{
Ow = iSource. Width;
Oh = iSource. Width * height/towidth;
X = 0;
Y = (iSource. Height-oh)/2;
}
Break;
}
Default:
Break;
}
Bitmap ob = new Bitmap (towidth, toheight );
Graphics g = Graphics. FromImage (ob );
G. Clear (System. Drawing. Color. WhiteSmoke );
G. CompositingQuality = CompositingQuality. HighQuality;
G. SmoothingMode = SmoothingMode. HighQuality;
G. InterpolationMode = InterpolationMode. HighQualityBicubic;
G. DrawImage (iSource
, New Rectangle (x, y, towidth, toheight)
, New Rectangle (0, 0, iSource. Width, iSource. Height)
, GraphicsUnit. Pixel );
G. Dispose ();
// The following Code sets the compression quality when saving the image
EncoderParameters ep = new EncoderParameters ();
Long [] qy = new long [1];
Qy [0] = flag; // set the compression ratio to 1-100.
EncoderParameter eParam = new EncoderParameter (System. Drawing. Imaging. Encoder. Quality, qy );
Ep. Param [0] = eParam;
Try
{
ImageCodecInfo [] arrayICI = ImageCodecInfo. GetImageEncoders ();
ImageCodecInfo policiciinfo = null;
For (int I = 0; I <arrayICI. Length; I ++)
{
If (arrayICI [I]. FormatDescription. Equals ("JPEG "))
{
Required iciinfo = arrayICI [I];
Break;
}
}
If (policiciinfo! = Null)
{
Ob. Save (dFile, jpegICIinfo, ep); // dFile Is The New Compressed path.
}
Else
{
Ob. Save (dFile, tFormat );
}
Return true;
}
Catch
{
Return false;
}
Finally
{
ISource. Dispose ();
Ob. Dispose ();
}
}
# Endregion
}
}
Author: xxj_jing