C#图片切割、图片压缩、缩略图生成代码汇总

C#图片切割、图片压缩、缩略图生成代码汇总,第1张

概述本文为大家整理了C#图片切割、图片压缩、缩略图生成的实现代码,大家可以收藏,方便以后使用,具体内容如下

本文为大家整理了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,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,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,int multiple,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,xWIDth / multiple,yWIDth / multiple);   g.dispose();   newImage.Save(outputfile,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; }

以上就是本文的全部内容,希望对大家的学习有所帮助。

总结

以上是内存溢出为你收集整理的C#图片切割、图片压缩、缩略图生成代码汇总全部内容,希望文章能够帮你解决C#图片切割、图片压缩、缩略图生成代码汇总所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1259242.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存