我是靠谱客的博主 鳗鱼路灯,最近开发中收集的这篇文章主要介绍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
#region
imageCompress
 /**//// <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;
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。

最后

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

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(120)

评论列表共有 0 条评论

立即
投稿
返回
顶部