C image capture compression of percentage compression and size compression implementation code
- 2020-05-24 05:25:15
- OfStack
Front time friend to send me some pictures, all are big pictures, considering the limitation of network speed, let him deal with the size of the picture and give me, the si did not know what tool to use.
Write a widget to capture and compress images for fun
1. Screenshot by percentage
2. Take the screenshot according to the specified pixel size
3. Take the screenshot according to the specified pixel size (however, in order to ensure the original proportion of the image, the image will be captured from the center so that the image will not be stretched)
4. Image quality compression of jpeg, with compression ratio between 1 and 100. (the right amount of compression doesn't make much difference to the naked eye, but it can greatly reduce the size of the image.)
PS: supplement to the CutImage method used above
Record the intercepted code again, simple as it is, but it will take time if you rewrite it.
Write a widget to capture and compress images for fun
1. Screenshot by percentage
View Code
/// <summary>
/// Scale down the picture
/// </summary>
/// <param name="srcImage"> To reduce the image </param>
/// <param name="percent"> Narrow the proportion </param>
/// <returns> The result of the reduction </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 to
Bitmap b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b);
// Quality of interpolation algorithm
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. Take the screenshot according to the specified pixel size
View Code
/// <summary>
/// Scale the image 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 to
Bitmap b = new Bitmap(iWidth, iHeight);
Graphics g = Graphics.FromImage(b);
// Quality of interpolation algorithm
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. Take the screenshot according to the specified pixel size (however, in order to ensure the original proportion of the image, the image will be captured from the center so that the image will not be stretched)
View Code
/// <summary>
/// Scale the image to the specified size, but to ensure the image width to height ratio is automatically truncated
/// </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 = srcImage.Width;
// To capture the height of the image (temporary image)
int newH = srcImage.Height;
// Intercept the start abscissa (temporary image)
int newX = 0;
// Intercept the start ordinate (temporary image)
int newY = 0;
// Capture scale (temporary image)
double whPercent = 1;
whPercent = ((double)iWidth / (double)iHeight) * ((double)srcImage.Height / (double)srcImage.Width);
if (whPercent > 1)
{
// The current image width is too large to intercept
newW = int.Parse(Math.Round(srcImage.Width / whPercent).ToString());
}
else if (whPercent < 1)
{
// The current image height is too large to capture
newH = int.Parse(Math.Round(srcImage.Height * whPercent).ToString());
}
if (newW != srcImage.Width)
{
// When the width changes, adjust the abscissa that you start to intercept
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 from which the intercept begins
newY = Math.Abs(int.Parse(Math.Round(((double)srcImage.Height - (double)newH) / 2).ToString()));
}
// Obtain pro rata provisional documents
Bitmap cutedImage = CutImage(srcImage, newX, newY, newW, newH);
// Save to file
Bitmap b = new Bitmap(iWidth, iHeight);
Graphics g = Graphics.FromImage(b);
// Quality of interpolation algorithm
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. Image quality compression of jpeg, with compression ratio between 1 and 100. (the right amount of compression doesn't make much difference to the naked eye, but it can greatly reduce the size of the image.)
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 ratio of compression 1-100
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
try
{
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICIinfo = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICIinfo = arrayICI[x];
break;
}
}
if (jpegICIinfo != null)
{
iSource.Save(outPath, jpegICIinfo, ep);//dFile Is the compressed new path
}
else
{
iSource.Save(outPath, tFormat);
}
return true;
}
catch
{
return false;
}
finally
{
iSource.Dispose();
iSource.Dispose();
}
}
PS: supplement to the CutImage method used above
View Code
/// <summary>
/// clipping -- with GDI+
/// </summary>
/// <param name="b"> The original Bitmap</param>
/// <param name="StartX"> To coordinate X</param>
/// <param name="StartY"> To coordinate Y</param>
/// <param name="iWidth"> The width of the </param>
/// <param name="iHeight"> highly </param>
/// <returns> After clipping 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)
{
// When the interception coordinates are too large, the processing ends
return null;
}
if (StartX + iWidth > w)
{
// When the width is too large, only the maximum size is intercepted
iWidth = w - StartX;
}
if (StartY + iHeight > h)
{
// When the height is too high, only the maximum size is intercepted
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, simple as it is, but it will take time if you rewrite it.