C Picture Cutting Picture Compression Thumbnail Generation Code Summary
- 2021-09-20 21:20:48
- OfStack
This article for everyone to sort out the C # picture cutting, picture compression, thumbnail generation of the implementation code, you can collect, convenient for later use, the specific content is as follows
/// Picture cutting function
/// </summary>
/// <param name="sourceFile"> Original picture file </param>
/// <param name="xNum"> Number of cuts on X axis </param>
/// <param name="yNum"> Number of cuts on Y axis </param>
/// <param name="quality"> Mass compression ratio </param>
/// <param name="outputFile"> Output file name, without suffix </param>
/// <returns> Successful return true If it fails, it returns false</returns>
public static bool imageCut(String sourceFile, int xNum, int yNum, long quality, String outputFile)
{
try
{
long imageQuality = quality;
Bitmap sourceImage = new Bitmap(sourceFile);
ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");
Encoder myEncoder = Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality);
myEncoderParameters.Param[0] = myEncoderParameter;
float xWidth = sourceImage.Width / xNum;
float yWidth = sourceImage.Height / yNum;
String outputImage = "";
for (int countY = 0; countY < yNum; countY++)
for (int countX = 0; countX < xNum; countX++)
{
RectangleF cloneRect = new RectangleF(countX * xWidth, countY * yWidth, xWidth, yWidth);
Bitmap newImage = sourceImage.Clone(cloneRect, PixelFormat.Format24bppRgb);
outputImage = outputFile + countX + countY + ".jpg";
newImage.Save(outputImage, myImageCodecInfo, myEncoderParameters);
}
return true;
}
catch
{
return false;
}
}
#endregion imageCut
imageCompress
#region
imageCompress
/**//// <summary>
/// Picture compression function
/// </summary>
/// <param name="sourceFile"> Original picture file </param>
/// <param name="quality"> Mass compression ratio </param>
/// <param name="ouputFile"> Output file name , Please use .jpg Suffix </param>
/// <returns> Successful return true If it fails, it returns false</returns>
public static bool imageCompress(String sourceFile,long quality,String outputFile)
{
try
{
long imageQuality = quality;
Bitmap sourceImage = new Bitmap(sourceFile);
ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");
Encoder myEncoder = Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality);
myEncoderParameters.Param[0] = myEncoderParameter;
sourceImage.Save(outputFile, myImageCodecInfo, myEncoderParameters);
return true;
}
catch
{
return false;
}
}
#endregion imageCompress
getThumImage#region getThumImage
/**//// <summary>
/// Generate thumbnails
/// </summary>
/// <param name="sourceFile"> Original picture file </param>
/// <param name="quality"> Mass compression ratio </param>
/// <param name="multiple"> Shrinkage multiple </param>
/// <param name="outputFile"> Output file name </param>
/// <returns> Successful return true, Failure returns false</returns>
public static bool getThumImage(String sourceFile, long quality, int multiple, String outputFile)
{
try
{
long imageQuality = quality;
Bitmap sourceImage = new Bitmap(sourceFile);
ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");
Encoder myEncoder = Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality);
myEncoderParameters.Param[0] = myEncoderParameter;
float xWidth = sourceImage.Width;
float yWidth = sourceImage.Height;
Bitmap newImage = new Bitmap((int)(xWidth /multiple), (int)(yWidth /multiple));
Graphics g = Graphics.FromImage(newImage);
g.DrawImage(sourceImage, 0, 0, xWidth / multiple, yWidth / multiple);
g.Dispose();
newImage.Save(outputFile,myImageCodecInfo,myEncoderParameters);
return true;
}
catch
{
return false;
}
}
#endregion getThumImage
ImageCodecInfo#region ImageCodecInfo
/**//// <summary>
/// Get picture coding information
/// </summary>
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}