C#图片切割、图片压缩、缩略图生成代码汇总
本文为大家整理了C#图片切割、图片压缩、缩略图生成的实现代码,大家可以收藏,方便以后使用,具体内容如下
///图片切割函数 ///</summary> ///<paramname="sourceFile">原始图片文件</param> ///<paramname="xNum">在X轴上的切割数量</param> ///<paramname="yNum">在Y轴上的切割数量</param> ///<paramname="quality">质量压缩比</param> ///<paramname="outputFile">输出文件名,不带后缀</param> ///<returns>成功返回true,失败则返回false</returns> publicstaticboolimageCut(StringsourceFile,intxNum,intyNum,longquality,StringoutputFile) { try { longimageQuality=quality; BitmapsourceImage=newBitmap(sourceFile); ImageCodecInfomyImageCodecInfo=GetEncoderInfo("image/jpeg"); EncodermyEncoder=Encoder.Quality; EncoderParametersmyEncoderParameters=newEncoderParameters(1); EncoderParametermyEncoderParameter=newEncoderParameter(myEncoder,imageQuality); myEncoderParameters.Param[0]=myEncoderParameter; floatxWidth=sourceImage.Width/xNum; floatyWidth=sourceImage.Height/yNum; StringoutputImage=""; for(intcountY=0;countY<yNum;countY++) for(intcountX=0;countX<xNum;countX++) { RectangleFcloneRect=newRectangleF(countX*xWidth,countY*yWidth,xWidth,yWidth); BitmapnewImage=sourceImage.Clone(cloneRect,PixelFormat.Format24bppRgb); outputImage=outputFile+countX+countY+".jpg"; newImage.Save(outputImage,myImageCodecInfo,myEncoderParameters); } returntrue; } catch { returnfalse; } } #endregionimageCut imageCompress #region imageCompress /**////<summary> ///图片压缩函数 ///</summary> ///<paramname="sourceFile">原始图片文件</param> ///<paramname="quality">质量压缩比</param> ///<paramname="ouputFile">输出文件名,请用.jpg后缀</param> ///<returns>成功返回true,失败则返回false</returns> publicstaticboolimageCompress(StringsourceFile,longquality,StringoutputFile) { try { longimageQuality=quality; BitmapsourceImage=newBitmap(sourceFile); ImageCodecInfomyImageCodecInfo=GetEncoderInfo("image/jpeg"); EncodermyEncoder=Encoder.Quality; EncoderParametersmyEncoderParameters=newEncoderParameters(1); EncoderParametermyEncoderParameter=newEncoderParameter(myEncoder,imageQuality); myEncoderParameters.Param[0]=myEncoderParameter; sourceImage.Save(outputFile,myImageCodecInfo,myEncoderParameters); returntrue; } catch { returnfalse; } } #endregionimageCompress getThumImage#regiongetThumImage /**////<summary> ///生成缩略图 ///</summary> ///<paramname="sourceFile">原始图片文件</param> ///<paramname="quality">质量压缩比</param> ///<paramname="multiple">收缩倍数</param> ///<paramname="outputFile">输出文件名</param> ///<returns>成功返回true,失败则返回false</returns> publicstaticboolgetThumImage(StringsourceFile,longquality,intmultiple,StringoutputFile) { try { longimageQuality=quality; BitmapsourceImage=newBitmap(sourceFile); ImageCodecInfomyImageCodecInfo=GetEncoderInfo("image/jpeg"); EncodermyEncoder=Encoder.Quality; EncoderParametersmyEncoderParameters=newEncoderParameters(1); EncoderParametermyEncoderParameter=newEncoderParameter(myEncoder,imageQuality); myEncoderParameters.Param[0]=myEncoderParameter; floatxWidth=sourceImage.Width; floatyWidth=sourceImage.Height; BitmapnewImage=newBitmap((int)(xWidth/multiple),(int)(yWidth/multiple)); Graphicsg=Graphics.FromImage(newImage); g.DrawImage(sourceImage,0,0,xWidth/multiple,yWidth/multiple); g.Dispose(); newImage.Save(outputFile,myImageCodecInfo,myEncoderParameters); returntrue; } catch { returnfalse; } } #endregiongetThumImage ImageCodecInfo#regionImageCodecInfo /**////<summary> ///获取图片编码信息 ///</summary> privatestaticImageCodecInfoGetEncoderInfo(StringmimeType) { intj; ImageCodecInfo[]encoders; encoders=ImageCodecInfo.GetImageEncoders(); for(j=0;j<encoders.Length;++j) { if(encoders[j].MimeType==mimeType) returnencoders[j]; } returnnull; }
以上就是本文的全部内容,希望对大家的学习有所帮助。