C # image capture compression (percentage compression/size compression) implementation code

Source: Internet
Author: User

A friend at the front-end time needs to send some pictures to me, all of which are big pictures. Considering the speed limit, I don't know what tools to use.

Write a small tool for capturing and compressing pictures for entertainment.
1. By percentage
Copy codeThe Code is as follows:
View Code
/// <Summary>
/// Scale down the image according to the proportion
/// </Summary>
/// <Param name = "srcImage"> image to be reduced </param>
/// <Param name = "percent"> scale down </param>
/// <Returns> reduced result </returns>
Public static Bitmap PercentImage (Image srcImage, double percent)
{
// The Reduced Height
Int newH = int. Parse (Math. Round (srcImage. Height * percent). ToString ());
// The REDUCED WIDTH
Int newW = int. Parse (Math. Round (srcImage. Width * percent). ToString ());
Try
{
// The image to save
Bitmap B = new Bitmap (newW, newH );
Graphics g = Graphics. FromImage (B );
// Quality of interpolation algorithms
G. InterpolationMode = InterpolationMode. Default;
G. DrawImage (srcImage, new Rectangle (0, 0, newW, newH), new Rectangle (0, 0, srcImage. Width, srcImage. Height), GraphicsUnit. Pixel );
G. Dispose ();
Return B;
}
Catch (Exception)
{
Return null;
}
}

2. Based on the specified pixel size
Copy codeThe Code is as follows:
View Code
/// <Summary>
/// Scale the image according to the specified size
/// </Summary>
/// <Param name = "srcImage"> </param>
/// <Param name = "iWidth"> </param>
/// <Param name = "iHeight"> </param>
/// <Returns> </returns>
Public static Bitmap SizeImage (Image srcImage, int iWidth, int iHeight)
{
Try
{
// The image to save
Bitmap B = new Bitmap (iWidth, iHeight );
Graphics g = Graphics. FromImage (B );
// Quality of interpolation algorithms
G. InterpolationMode = InterpolationMode. HighQualityBicubic;
G. DrawImage (srcImage, new Rectangle (0, 0, iWidth, iHeight), new Rectangle (0, 0, srcImage. Width, srcImage. Height), GraphicsUnit. Pixel );
G. Dispose ();
Return B;
}
Catch (Exception)
{
Return null;
}
}

3. Specify the pixel size (however, to ensure the original proportion of the image, the image will be taken from the center to achieve the effect that the image will not be stretched)
Copy codeThe Code is as follows:
View Code
/// <Summary>
/// Scale the image according to the specified size, but to ensure that the image width to height ratio is automatically captured
/// </Summary>
/// <Param name = "srcImage"> </param>
/// <Param name = "iWidth"> </param>
/// <Param name = "iHeight"> </param>
/// <Returns> </returns>
Public static Bitmap SizeImageWithOldPercent (Image srcImage, int iWidth, int iHeight)
{
Try
{
// Width of the image to be captured (temporary image)
Int newW = srcImage. Width;
// Height of the image to be captured (temporary image)
Int newH = srcImage. Height;
// Capture the starting abscissa (temporary image)
Int newX = 0;
// Capture the start ordinate (temporary image)
Int newY = 0;
// Screenshot ratio (temporary image)
Double whPercent = 1;
WhPercent = (double) iWidth/(double) iHeight) * (double) srcImage. Height/(double) srcImage. Width );
If (whPercent> 1)
{
// When the width of the current image is too large
NewW = int. Parse (Math. Round (srcImage. Width/whPercent). ToString ());
}
Else if (whPercent <1)
{
// When the height of the current image is too large to be captured
NewH = int. Parse (Math. Round (srcImage. Height * whPercent). ToString ());
}
If (newW! = SrcImage. Width)
{
// Adjust the abscissa of the start part when the width changes.
NewX = Math. Abs (int. Parse (Math. Round (double) srcImage. Width-newW)/2). ToString ()));
}
Else if (newH = srcImage. Height)
{
// When the height changes, adjust the ordinate coordinates of the start part.
NewY = Math. Abs (int. Parse (Math. Round (double) srcImage. Height-(double) newH)/2). ToString ()));
}
// Obtain a temporary file that matches the proportion
Bitmap cutedImage = CutImage (srcImage, newX, newY, newW, newH );
// Saved file
Bitmap B = new Bitmap (iWidth, iHeight );
Graphics g = Graphics. FromImage (B );
// Quality of interpolation algorithms
G. InterpolationMode = InterpolationMode. Default;
G. DrawImage (cutedImage, new Rectangle (0, 0, iWidth, iHeight), new Rectangle (0, 0, cutedImage. Width, cutedImage. Height), GraphicsUnit. Pixel );
G. Dispose ();
Return B;
}
Catch (Exception)
{
Return null;
}
}

4.jpeg image quality compression. The compression ratio is 1. (There is no obvious difference for the naked eye, but it can greatly reduce the image size)
Copy codeThe Code is as follows:
View Code
/// <Summary>
/// Jpeg Image Compression
/// </Summary>
/// <Param name = "sFile"> </param>
/// <Param name = "outPath"> </param>
/// <Param name = "flag"> </param>
/// <Returns> </returns>
Public static bool GetPicThumbnail (string sFile, string outPath, int flag)
{
System. Drawing. Image iSource = System. Drawing. Image. FromFile (sFile );
ImageFormat tFormat = iSource. RawFormat;
// 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 x = 0; x <arrayICI. Length; x ++)
{
If (arrayICI [x]. FormatDescription. Equals ("JPEG "))
{
Required iciinfo = arrayICI [x];
Break;
}
}
If (policiciinfo! = Null)
{
ISource. Save (outPath, jpegICIinfo, ep); // dFile is the new path after compression.
}
Else
{
ISource. Save (outPath, tFormat );
}
Return true;
}
Catch
{
Return false;
}
Finally
{
ISource. Dispose ();
ISource. Dispose ();
}
}

PS: Supplement of the CutImage method used above
Copy codeThe Code is as follows:
View Code
/// <Summary>
/// Crop -- use GDI +
/// </Summary>
/// <Param name = "B"> original Bitmap </param>
/// <Param name = "StartX"> Start coordinate X </param>
/// <Param name = "StartY"> Start coordinate Y </param>
/// <Param name = "iWidth"> width </param>
/// <Param name = "iHeight"> height </param>
/// <Returns> the cropped Bitmap </returns>
Public static Bitmap CutImage (Image B, int StartX, int StartY, int iWidth, int iHeight)
{
If (B = null)
{
Return null;
}
Int w = B. Width;
Int h = B. Height;
If (StartX> = w | StartY> = h)
{
// End processing when the starting screenshot coordinate is too large
Return null;
}
If (StartX + iWidth> w)
{
// When the width is too large, only the maximum size is truncated.
IWidth = w-StartX;
}
If (StartY + iHeight> h)
{
// When the height is too large, only the maximum size is captured.
IHeight = h-StartY;
}
Try
{
Bitmap bmpOut = new Bitmap (iWidth, iHeight );
Graphics g = Graphics. FromImage (bmpOut );
G. DrawImage (B, new Rectangle (0, 0, iWidth, iHeight), new Rectangle (StartX, StartY, iWidth, iHeight), GraphicsUnit. Pixel );
G. Dispose ();
Return bmpOut;
}
Catch
{
Return null;
}
}

Record the intercepted code again. Although simple, it takes time to rewrite the code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.