欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-11 14:23:32
本文为大家整理了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;
 }

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