用于图片切割,图片压缩,缩略图的生成
用于图片切割,图片压缩,缩略图的生成
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
imageCut#region imageCut
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 图片切割函数
/// </summary>
/// <param name="sourceFile">原始图片文件</param>
/// <param name="xNum">在X轴上的切割数量</param>
/// <param name="yNum">在Y轴上的切割数量</param>
/// <param name="quality">质量压缩比</param>
/// <param name="outputFile">输出文件名,不带后缀</param>
/// <returns>成功返回true,失败则返回false</returns>
public static bool imageCut(String sourceFile, int xNum, int yNum, long quality, String outputFile)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
return true;
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
#endregion imageCut
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
imageCompress#region imageCompress
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 图片压缩函数
/// </summary>
/// <param name="sourceFile">原始图片文件</param>
/// <param name="quality">质量压缩比</param>
/// <param name="ouputFile">输出文件名,请用 .jpg 后缀 </param>
/// <returns>成功返回true,失败则返回false</returns>
public static bool imageCompress(String sourceFile,long quality,String outputFile)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
#endregion imageCompress
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
getThumImage#region getThumImage
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 生成缩略图
/// </summary>
/// <param name="sourceFile">原始图片文件</param>
/// <param name="quality">质量压缩比</param>
/// <param name="multiple">收缩倍数</param>
/// <param name="outputFile">输出文件名</param>
/// <returns>成功返回true,失败则返回false</returns>
public static bool getThumImage(String sourceFile, long quality, int multiple, String outputFile)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
}
#endregion getThumImage
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
ImageCodecInfo#region ImageCodecInfo
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取图片编码信息
/// </summary>
private static ImageCodecInfo GetEncoderInfo(String mimeType)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
#endregion ImageCodeInfo
posted @
2007-08-23 16:44
天纯蓝
阅读(
645)
评论()
编辑
收藏
举报