C#图片切割、图片压缩、缩略图生成
C#图片切割、图片压缩、缩略图生成的实现代码
/// 图片切割函数/// /// 原始图片文件/// 在X轴上的切割数量/// 在Y轴上的切割数量/// 质量压缩比/// 输出文件名,不带后缀/// 成功返回true,失败则返回falsepublic 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#regionimageCompress/**//// /// 图片压缩函数/// /// 原始图片文件/// 质量压缩比/// 输出文件名,请用 .jpg 后缀 /// 成功返回true,失败则返回falsepublic 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/**//// /// 生成缩略图/// /// 原始图片文件/// 质量压缩比/// 收缩倍数/// 输出文件名/// 成功返回true,失败则返回falsepublic 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/**//// /// 获取图片编码信息/// 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;}
龙腾一族至尊龙骑
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~