时间:2021-05-20
本文为大家整理了C#图片切割、图片压缩、缩略图生成的实现代码,大家可以收藏,方便以后使用,具体内容如下
/// 图片切割函数 /// </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) { 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 /// <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) { 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> /// 生成缩略图 /// </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) { 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> /// 获取图片编码信息 /// </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; }以上就是本文的全部内容,希望对大家的学习有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
复制代码代码如下://////为图片生成缩略图//////原图片的路径///缩略图宽///缩略图高///publicSystem.Drawing.ImageGe
本文实例讲述了C#简单生成缩略图的方法。分享给大家供大家参考。具体实现方法如下://////生成缩略图//////源图路径(物理路径)///缩略图路径(物理路径
复制代码代码如下:///生成缩略图//////源图路径///缩略图路径///缩略图宽度///缩略图高度///生成缩略图的方式:HW指定高宽缩放(可能变形);W指
这样的话,如果图片的宽高比例不合适,那么缩略图将会被压缩得很不好看,在这里,我是先对图片进行按宽或高进行放缩,超过缩略图大小的部分再进行裁剪,这样生成的缩略图的
本文实例讲解了php上传图片并压缩的实现方法,之前一篇《PHP实现图片上传并压缩》已经为大家进行了简单介绍,此次实现上传图片然后按照比例缩略图,指定缩略图的最大