When my friend at the front end time, he wanted to send me some pictures, all of which were large pictures. Considering the internet speed limit, he asked him to handle the size of the picture before giving it to me. This guy didn’t know what tools he used.
For entertainment, I wrote a screenshot and compressed pictures.
1. Screenshot by percentage
View Code
/// <summary>
/// Scaling the picture according to the scale
/// </summary>
/// <param name="srcImage">Image to be reduced</param>
/// <param name="percent">Reduce the ratio</param>
/// <returns>Result after shrink</returns>
public static Bitmap PercentImage(Image srcImage, double percent)
{
// Reduced height
int newH = (( * percent).ToString());
// Reduced width
int newW = (( * percent).ToString());
try
{
// The picture to be saved to
Bitmap b = new Bitmap(newW, newH);
Graphics g = (b);
// Quality of interpolation algorithm
= ;
(srcImage, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, , ), );
();
return b;
}
catch (Exception)
{
return null;
}
}
2. Screenshot according to the specified pixel size
View Code
/// <summary>
/// Scale the picture 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 picture to be saved to
Bitmap b = new Bitmap(iWidth, iHeight);
Graphics g = (b);
// Quality of interpolation algorithm
= ;
(srcImage, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(0, 0, , ), );
();
return b;
}
catch (Exception)
{
return null;
}
}
3. Take screenshots according to the specified pixel size (but in order to ensure the original proportion of the picture, the picture will be screened from the center to achieve the effect that the picture is not stretched)
View Code
/// <summary>
/// Scale the picture according to the specified size, but to ensure the image aspect ratio is automatically intercepted
/// </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
{
// To capture the width of the image (temporary image)
int newW = ;
// The height of the image to be captured (temporary image)
int newH = ;
// Snap the start horizontal coordinate (temporary picture)
int newX = 0;
// Intercept the starting vertical coordinate (temporary picture)
int newY = 0;
// Intercept ratio (temporary picture)
double whPercent = 1;
whPercent = ((double)iWidth / (double)iHeight) * ((double) / (double));
if (whPercent > 1)
{
// When the current image width is too large for the intercepting ratio
newW = (( / whPercent).ToString());
}
else if (whPercent < 1)
{
// When the current image height is too large for the intercepting ratio
newH = (( * whPercent).ToString());
}
if (newW != )
{
// When the width changes, adjust the horizontal axis that starts to be intercepted
newX = (((((double) - newW) / 2).ToString()));
}
else if (newH == )
{
// When the height changes, adjust the vertical coordinate that starts to be intercepted
newY = (((((double) - (double)newH) / 2).ToString()));
}
// Obtain temporary documents that meet the proportion
Bitmap cutedImage = CutImage(srcImage, newX, newY, newW, newH);
// File saved to
Bitmap b = new Bitmap(iWidth, iHeight);
Graphics g = (b);
// Quality of interpolation algorithm
= ;
(cutedImage, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(0, 0, , ), );
();
return b;
}
catch (Exception)
{
return null;
}
}
Image quality compression, the compression ratio parameters are between 1-100. (The appropriate amount of compression is not obvious to the naked eye, but it can greatly reduce the size of the picture.)
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)
{
iSource = (sFile);
ImageFormat tFormat = ;
//The following code sets the compression quality when saving the picture
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = flag;//Set the compression ratio 1-100
EncoderParameter eParam = new EncoderParameter(, qy);
[0] = eParam;
try
{
ImageCodecInfo[] arrayICI = ();
ImageCodecInfo jpegICIinfo = null;
for (int x = 0; x < ; x++)
{
if (arrayICI[x].("JPEG"))
{
jpegICIinfo = arrayICI[x];
break;
}
}
if (jpegICIinfo != null)
{
(outPath, jpegICIinfo, ep);//dFile is the compressed new path
}
else
{
(outPath, tFormat);
}
return true;
}
catch
{
return false;
}
finally
{
();
();
}
}
PS: Supplement to the CutImage method used above
View Code
/// <summary>
/// Clipping -- 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>Cropped Bitmap</returns>
public static Bitmap CutImage(Image b, int StartX, int StartY, int iWidth, int iHeight)
{
if (b == null)
{
return null;
}
int w = ;
int h = ;
if (StartX >= w || StartY >= h)
{
// If the coordinates are too large, the processing will be completed
return null;
}
if (StartX + iWidth > w)
{
// Only the maximum size is intercepted when the width is too large
iWidth = w - StartX;
}
if (StartY + iHeight > h)
{
// Only the maximum size is intercepted when the height is too large
iHeight = h - StartY;
}
try
{
Bitmap bmpOut = new Bitmap(iWidth, iHeight);
Graphics g = (bmpOut);
(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), );
();
return bmpOut;
}
catch
{
return null;
}
}
Record the intercepted code again, although it is simple, it still takes time to rewrite it.
For entertainment, I wrote a screenshot and compressed pictures.
1. Screenshot by percentage
Copy the codeThe code is as follows:
View Code
/// <summary>
/// Scaling the picture according to the scale
/// </summary>
/// <param name="srcImage">Image to be reduced</param>
/// <param name="percent">Reduce the ratio</param>
/// <returns>Result after shrink</returns>
public static Bitmap PercentImage(Image srcImage, double percent)
{
// Reduced height
int newH = (( * percent).ToString());
// Reduced width
int newW = (( * percent).ToString());
try
{
// The picture to be saved to
Bitmap b = new Bitmap(newW, newH);
Graphics g = (b);
// Quality of interpolation algorithm
= ;
(srcImage, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, , ), );
();
return b;
}
catch (Exception)
{
return null;
}
}
2. Screenshot according to the specified pixel size
Copy the codeThe code is as follows:
View Code
/// <summary>
/// Scale the picture 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 picture to be saved to
Bitmap b = new Bitmap(iWidth, iHeight);
Graphics g = (b);
// Quality of interpolation algorithm
= ;
(srcImage, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(0, 0, , ), );
();
return b;
}
catch (Exception)
{
return null;
}
}
3. Take screenshots according to the specified pixel size (but in order to ensure the original proportion of the picture, the picture will be screened from the center to achieve the effect that the picture is not stretched)
Copy the codeThe code is as follows:
View Code
/// <summary>
/// Scale the picture according to the specified size, but to ensure the image aspect ratio is automatically intercepted
/// </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
{
// To capture the width of the image (temporary image)
int newW = ;
// The height of the image to be captured (temporary image)
int newH = ;
// Snap the start horizontal coordinate (temporary picture)
int newX = 0;
// Intercept the starting vertical coordinate (temporary picture)
int newY = 0;
// Intercept ratio (temporary picture)
double whPercent = 1;
whPercent = ((double)iWidth / (double)iHeight) * ((double) / (double));
if (whPercent > 1)
{
// When the current image width is too large for the intercepting ratio
newW = (( / whPercent).ToString());
}
else if (whPercent < 1)
{
// When the current image height is too large for the intercepting ratio
newH = (( * whPercent).ToString());
}
if (newW != )
{
// When the width changes, adjust the horizontal axis that starts to be intercepted
newX = (((((double) - newW) / 2).ToString()));
}
else if (newH == )
{
// When the height changes, adjust the vertical coordinate that starts to be intercepted
newY = (((((double) - (double)newH) / 2).ToString()));
}
// Obtain temporary documents that meet the proportion
Bitmap cutedImage = CutImage(srcImage, newX, newY, newW, newH);
// File saved to
Bitmap b = new Bitmap(iWidth, iHeight);
Graphics g = (b);
// Quality of interpolation algorithm
= ;
(cutedImage, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(0, 0, , ), );
();
return b;
}
catch (Exception)
{
return null;
}
}
Image quality compression, the compression ratio parameters are between 1-100. (The appropriate amount of compression is not obvious to the naked eye, but it can greatly reduce the size of the picture.)
Copy the 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)
{
iSource = (sFile);
ImageFormat tFormat = ;
//The following code sets the compression quality when saving the picture
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = flag;//Set the compression ratio 1-100
EncoderParameter eParam = new EncoderParameter(, qy);
[0] = eParam;
try
{
ImageCodecInfo[] arrayICI = ();
ImageCodecInfo jpegICIinfo = null;
for (int x = 0; x < ; x++)
{
if (arrayICI[x].("JPEG"))
{
jpegICIinfo = arrayICI[x];
break;
}
}
if (jpegICIinfo != null)
{
(outPath, jpegICIinfo, ep);//dFile is the compressed new path
}
else
{
(outPath, tFormat);
}
return true;
}
catch
{
return false;
}
finally
{
();
();
}
}
PS: Supplement to the CutImage method used above
Copy the codeThe code is as follows:
View Code
/// <summary>
/// Clipping -- 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>Cropped Bitmap</returns>
public static Bitmap CutImage(Image b, int StartX, int StartY, int iWidth, int iHeight)
{
if (b == null)
{
return null;
}
int w = ;
int h = ;
if (StartX >= w || StartY >= h)
{
// If the coordinates are too large, the processing will be completed
return null;
}
if (StartX + iWidth > w)
{
// Only the maximum size is intercepted when the width is too large
iWidth = w - StartX;
}
if (StartY + iHeight > h)
{
// Only the maximum size is intercepted when the height is too large
iHeight = h - StartY;
}
try
{
Bitmap bmpOut = new Bitmap(iWidth, iHeight);
Graphics g = (bmpOut);
(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), );
();
return bmpOut;
}
catch
{
return null;
}
}
Record the intercepted code again, although it is simple, it still takes time to rewrite it.