Recently, due to the need for C # image processing applications, I wrote the following code for your reference on the Internet.
Generally, you need to scale the image to a thumbnail with the specified width and height. If you only scale the original image, the scaled image will be stretched and deformed when the aspect ratio before and after the image is different. You can crop the image according to the aspect ratio of the target image, and then scale it to the height and width of the target image.
The principle is simple:
1) Calculate the cropped rectangular area of the original image based on the aspect ratio of the target image.
2) copy the image in the rectangle area to the rectangle area with the target height and width. The code below is as follows:
C # code
/// <Summary>
/// Image conversion (cropping and scaling)
/// </Summary>
/// <Param name = "ASrcFileName"> source file name </param>
/// <Param name = "ADestFileName"> target file name </param>
/// <Param name = "AWidth"> converted width (pixel) </param>
/// <Param name = "AHeight"> converted height (pixel) </param>
/// <Param name = "AQuality"> Storage Quality (value range: 1-) </param>
Public static void DoConvert (string ASrcFileName, string ADestFileName, int AWidth, int AHeight, int AQuality)
{
Image ASrcImg = Image. FromFile (ASrcFileName );
If (ASrcImg. Width <= AWidth & ASrcImg. Height <= AHeight)
{// The image height and width are smaller than the target height and can be saved directly.
ASrcImg. Save (ADestFileName );
Return;
}
Double ADestRate = AWidth * 1.0/AHeight;
Double ASrcRate = ASrcImg. Width * 1.0/ASrcImg. Height;
// Specifies the cropped width.
Double ACutWidth = ASrcRate> ADestRate? (ASrcImg. Height * ADestRate): ASrcImg. Width;
// Height after Cropping
Double ACutHeight = ASrcRate> ADestRate? ASrcImg. Height: (ASrcImg. Width/ADestRate );
// Specify the area of the rectangle to be cropped Based on the center of the original image.
Rectangle AFromRect = new Rectangle (Convert. toInt32 (ASrcImg. width-ACutWidth)/2), Convert. toInt32 (ASrcImg. height-ACutHeight)/2), (int) ACutWidth, (int) ACutHeight );
// Target rectangular area
Rectangle AToRect = new Rectangle (0, 0, AWidth, AHeight );
Image ADestImg = new Bitmap (AWidth, AHeight );
Graphics ADestGraph = Graphics. FromImage (ADestImg );
ADestGraph. InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. HighQualityBicubic;
ADestGraph. SmoothingMode = System. Drawing. Drawing2D. SmoothingMode. HighQuality;
ADestGraph. DrawImage (ASrcImg, AToRect, AFromRect, GraphicsUnit. Pixel );
// Obtain the system image/jpeg encoding information
ImageCodecInfo [] AInfos = ImageCodecInfo. GetImageEncoders ();
ImageCodecInfo AInfo = null;
Foreach (ImageCodecInfo I in AInfos)
{
If (I. MimeType = "image/jpeg ")
{
AInfo = I;
Break;
}
}
// Sets the image quality parameters after conversion.
EncoderParameters AParams = new EncoderParameters (1 );
AParams. Param [0] = new EncoderParameter (System. Drawing. Imaging. Encoder. Quality, (long) AQuality );
// Save
ADestImg. Save (ADestFileName, AInfo, AParams );
}