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

C#图片处理类分享

程序员文章站 2023-11-29 11:44:22
本文实例为大家分享了c#图片处理类的具体代码,供大家参考,具体内容如下 using system; using system.collections; usi...

本文实例为大家分享了c#图片处理类的具体代码,供大家参考,具体内容如下

using system;
using system.collections;
using system.io;
using system.drawing;
using system.drawing.imaging;
using system.drawing.drawing2d;

namespace dotnet.utilities
{
 public class imageclass
 {
  public imageclass()
  { }

  #region 缩略图
  /// <summary>
  /// 生成缩略图
  /// </summary>
  /// <param name="originalimagepath">源图路径(物理路径)</param>
  /// <param name="thumbnailpath">缩略图路径(物理路径)</param>
  /// <param name="width">缩略图宽度</param>
  /// <param name="height">缩略图高度</param>
  /// <param name="mode">生成缩略图的方式</param> 
  public static void makethumbnail(string originalimagepath, string thumbnailpath, int width, int height, string mode)
  {
   system.drawing.image originalimage = system.drawing.image.fromfile(originalimagepath);

   int towidth = width;
   int toheight = height;

   int x = 0;
   int y = 0;
   int ow = originalimage.width;
   int oh = originalimage.height;

   switch (mode)
   {
    case "hw": //指定高宽缩放(可能变形)    
     break;
    case "w": //指定宽,高按比例     
     toheight = originalimage.height * width / originalimage.width;
     break;
    case "h": //指定高,宽按比例
     towidth = originalimage.width * height / originalimage.height;
     break;
    case "cut": //指定高宽裁减(不变形)    
     if ((double)originalimage.width / (double)originalimage.height > (double)towidth / (double)toheight)
     {
      oh = originalimage.height;
      ow = originalimage.height * towidth / toheight;
      y = 0;
      x = (originalimage.width - ow) / 2;
     }
     else
     {
      ow = originalimage.width;
      oh = originalimage.width * height / towidth;
      x = 0;
      y = (originalimage.height - oh) / 2;
     }
     break;
    default:
     break;
   }

   //新建一个bmp图片
   system.drawing.image bitmap = new system.drawing.bitmap(towidth, toheight);

   //新建一个画板
   system.drawing.graphics g = system.drawing.graphics.fromimage(bitmap);

   //设置高质量插值法
   g.interpolationmode = system.drawing.drawing2d.interpolationmode.high;

   //设置高质量,低速度呈现平滑程度
   g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;

   //清空画布并以透明背景色填充
   g.clear(system.drawing.color.transparent);

   //在指定位置并且按指定大小绘制原图片的指定部分
   g.drawimage(originalimage, new system.drawing.rectangle(0, 0, towidth, toheight), new system.drawing.rectangle(x, y, ow, oh), system.drawing.graphicsunit.pixel);

   try
   {
    //以jpg格式保存缩略图
    bitmap.save(thumbnailpath, system.drawing.imaging.imageformat.jpeg);
   }
   catch (system.exception e)
   {
    throw e;
   }
   finally
   {
    originalimage.dispose();
    bitmap.dispose();
    g.dispose();
   }
  }
  #endregion

  #region 图片水印
  /// <summary>
  /// 图片水印处理方法
  /// </summary>
  /// <param name="path">需要加载水印的图片路径(绝对路径)</param>
  /// <param name="waterpath">水印图片(绝对路径)</param>
  /// <param name="location">水印位置(传送正确的代码)</param>
  public static string imagewatermark(string path, string waterpath, string location)
  {
   string kz_name = path.getextension(path);
   if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg")
   {
    datetime time = datetime.now;
    string filename = "" + time.year.tostring() + time.month.tostring() + time.day.tostring() + time.hour.tostring() + time.minute.tostring() + time.second.tostring() + time.millisecond.tostring();
    image img = bitmap.fromfile(path);
    image waterimg = image.fromfile(waterpath);
    graphics g = graphics.fromimage(img);
    arraylist loca = getlocation(location, img, waterimg);
    g.drawimage(waterimg, new rectangle(int.parse(loca[0].tostring()), int.parse(loca[1].tostring()), waterimg.width, waterimg.height));
    waterimg.dispose();
    g.dispose();
    string newpath = path.getdirectoryname(path) + filename + kz_name;
    img.save(newpath);
    img.dispose();
    file.copy(newpath, path, true);
    if (file.exists(newpath))
    {
     file.delete(newpath);
    }
   }
   return path;
  }

  /// <summary>
  /// 图片水印位置处理方法
  /// </summary>
  /// <param name="location">水印位置</param>
  /// <param name="img">需要添加水印的图片</param>
  /// <param name="waterimg">水印图片</param>
  private static arraylist getlocation(string location, image img, image waterimg)
  {
   arraylist loca = new arraylist();
   int x = 0;
   int y = 0;

   if (location == "lt")
   {
    x = 10;
    y = 10;
   }
   else if (location == "t")
   {
    x = img.width / 2 - waterimg.width / 2;
    y = img.height - waterimg.height;
   }
   else if (location == "rt")
   {
    x = img.width - waterimg.width;
    y = 10;
   }
   else if (location == "lc")
   {
    x = 10;
    y = img.height / 2 - waterimg.height / 2;
   }
   else if (location == "c")
   {
    x = img.width / 2 - waterimg.width / 2;
    y = img.height / 2 - waterimg.height / 2;
   }
   else if (location == "rc")
   {
    x = img.width - waterimg.width;
    y = img.height / 2 - waterimg.height / 2;
   }
   else if (location == "lb")
   {
    x = 10;
    y = img.height - waterimg.height;
   }
   else if (location == "b")
   {
    x = img.width / 2 - waterimg.width / 2;
    y = img.height - waterimg.height;
   }
   else
   {
    x = img.width - waterimg.width;
    y = img.height - waterimg.height;
   }
   loca.add(x);
   loca.add(y);
   return loca;
  }
  #endregion

  #region 文字水印
  /// <summary>
  /// 文字水印处理方法
  /// </summary>
  /// <param name="path">图片路径(绝对路径)</param>
  /// <param name="size">字体大小</param>
  /// <param name="letter">水印文字</param>
  /// <param name="color">颜色</param>
  /// <param name="location">水印位置</param>
  public static string letterwatermark(string path, int size, string letter, color color, string location)
  {
   #region

   string kz_name = path.getextension(path);
   if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg")
   {
    datetime time = datetime.now;
    string filename = "" + time.year.tostring() + time.month.tostring() + time.day.tostring() + time.hour.tostring() + time.minute.tostring() + time.second.tostring() + time.millisecond.tostring();
    image img = bitmap.fromfile(path);
    graphics gs = graphics.fromimage(img);
    arraylist loca = getlocation(location, img, size, letter.length);
    font font = new font("宋体", size);
    brush br = new solidbrush(color);
    gs.drawstring(letter, font, br, float.parse(loca[0].tostring()), float.parse(loca[1].tostring()));
    gs.dispose();
    string newpath = path.getdirectoryname(path) + filename + kz_name;
    img.save(newpath);
    img.dispose();
    file.copy(newpath, path, true);
    if (file.exists(newpath))
    {
     file.delete(newpath);
    }
   }
   return path;

   #endregion
  }

  /// <summary>
  /// 文字水印位置的方法
  /// </summary>
  /// <param name="location">位置代码</param>
  /// <param name="img">图片对象</param>
  /// <param name="width">宽(当水印类型为文字时,传过来的就是字体的大小)</param>
  /// <param name="height">高(当水印类型为文字时,传过来的就是字符的长度)</param>
  private static arraylist getlocation(string location, image img, int width, int height)
  {
   #region

   arraylist loca = new arraylist(); //定义数组存储位置
   float x = 10;
   float y = 10;

   if (location == "lt")
   {
    loca.add(x);
    loca.add(y);
   }
   else if (location == "t")
   {
    x = img.width / 2 - (width * height) / 2;
    loca.add(x);
    loca.add(y);
   }
   else if (location == "rt")
   {
    x = img.width - width * height;
   }
   else if (location == "lc")
   {
    y = img.height / 2;
   }
   else if (location == "c")
   {
    x = img.width / 2 - (width * height) / 2;
    y = img.height / 2;
   }
   else if (location == "rc")
   {
    x = img.width - height;
    y = img.height / 2;
   }
   else if (location == "lb")
   {
    y = img.height - width - 5;
   }
   else if (location == "b")
   {
    x = img.width / 2 - (width * height) / 2;
    y = img.height - width - 5;
   }
   else
   {
    x = img.width - width * height;
    y = img.height - width - 5;
   }
   loca.add(x);
   loca.add(y);
   return loca;

   #endregion
  }
  #endregion

  #region 调整光暗
  /// <summary>
  /// 调整光暗
  /// </summary>
  /// <param name="mybm">原始图片</param>
  /// <param name="width">原始图片的长度</param>
  /// <param name="height">原始图片的高度</param>
  /// <param name="val">增加或减少的光暗值</param>
  public bitmap ldpic(bitmap mybm, int width, int height, int val)
  {
   bitmap bm = new bitmap(width, height);//初始化一个记录经过处理后的图片对象
   int x, y, resultr, resultg, resultb;//x、y是循环次数,后面三个是记录红绿蓝三个值的
   color pixel;
   for (x = 0; x < width; x++)
   {
    for (y = 0; y < height; y++)
    {
     pixel = mybm.getpixel(x, y);//获取当前像素的值
     resultr = pixel.r + val;//检查红色值会不会超出[0, 255]
     resultg = pixel.g + val;//检查绿色值会不会超出[0, 255]
     resultb = pixel.b + val;//检查蓝色值会不会超出[0, 255]
     bm.setpixel(x, y, color.fromargb(resultr, resultg, resultb));//绘图
    }
   }
   return bm;
  }
  #endregion

  #region 反色处理
  /// <summary>
  /// 反色处理
  /// </summary>
  /// <param name="mybm">原始图片</param>
  /// <param name="width">原始图片的长度</param>
  /// <param name="height">原始图片的高度</param>
  public bitmap repic(bitmap mybm, int width, int height)
  {
   bitmap bm = new bitmap(width, height);//初始化一个记录处理后的图片的对象
   int x, y, resultr, resultg, resultb;
   color pixel;
   for (x = 0; x < width; x++)
   {
    for (y = 0; y < height; y++)
    {
     pixel = mybm.getpixel(x, y);//获取当前坐标的像素值
     resultr = 255 - pixel.r;//反红
     resultg = 255 - pixel.g;//反绿
     resultb = 255 - pixel.b;//反蓝
     bm.setpixel(x, y, color.fromargb(resultr, resultg, resultb));//绘图
    }
   }
   return bm;
  }
  #endregion

  #region 浮雕处理
  /// <summary>
  /// 浮雕处理
  /// </summary>
  /// <param name="oldbitmap">原始图片</param>
  /// <param name="width">原始图片的长度</param>
  /// <param name="height">原始图片的高度</param>
  public bitmap fd(bitmap oldbitmap, int width, int height)
  {
   bitmap newbitmap = new bitmap(width, height);
   color color1, color2;
   for (int x = 0; x < width - 1; x++)
   {
    for (int y = 0; y < height - 1; y++)
    {
     int r = 0, g = 0, b = 0;
     color1 = oldbitmap.getpixel(x, y);
     color2 = oldbitmap.getpixel(x + 1, y + 1);
     r = math.abs(color1.r - color2.r + 128);
     g = math.abs(color1.g - color2.g + 128);
     b = math.abs(color1.b - color2.b + 128);
     if (r > 255) r = 255;
     if (r < 0) r = 0;
     if (g > 255) g = 255;
     if (g < 0) g = 0;
     if (b > 255) b = 255;
     if (b < 0) b = 0;
     newbitmap.setpixel(x, y, color.fromargb(r, g, b));
    }
   }
   return newbitmap;
  }
  #endregion

  #region 拉伸图片
  /// <summary>
  /// 拉伸图片
  /// </summary>
  /// <param name="bmp">原始图片</param>
  /// <param name="neww">新的宽度</param>
  /// <param name="newh">新的高度</param>
  public static bitmap resizeimage(bitmap bmp, int neww, int newh)
  {
   try
   {
    bitmap bap = new bitmap(neww, newh);
    graphics g = graphics.fromimage(bap);
    g.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
    g.drawimage(bap, new rectangle(0, 0, neww, newh), new rectangle(0, 0, bap.width, bap.height), graphicsunit.pixel);
    g.dispose();
    return bap;
   }
   catch
   {
    return null;
   }
  }
  #endregion

  #region 滤色处理
  /// <summary>
  /// 滤色处理
  /// </summary>
  /// <param name="mybm">原始图片</param>
  /// <param name="width">原始图片的长度</param>
  /// <param name="height">原始图片的高度</param>
  public bitmap filpic(bitmap mybm, int width, int height)
  {
   bitmap bm = new bitmap(width, height);//初始化一个记录滤色效果的图片对象
   int x, y;
   color pixel;

   for (x = 0; x < width; x++)
   {
    for (y = 0; y < height; y++)
    {
     pixel = mybm.getpixel(x, y);//获取当前坐标的像素值
     bm.setpixel(x, y, color.fromargb(0, pixel.g, pixel.b));//绘图
    }
   }
   return bm;
  }
  #endregion

  #region 左右翻转
  /// <summary>
  /// 左右翻转
  /// </summary>
  /// <param name="mybm">原始图片</param>
  /// <param name="width">原始图片的长度</param>
  /// <param name="height">原始图片的高度</param>
  public bitmap revpiclr(bitmap mybm, int width, int height)
  {
   bitmap bm = new bitmap(width, height);
   int x, y, z; //x,y是循环次数,z是用来记录像素点的x坐标的变化的
   color pixel;
   for (y = height - 1; y >= 0; y--)
   {
    for (x = width - 1, z = 0; x >= 0; x--)
    {
     pixel = mybm.getpixel(x, y);//获取当前像素的值
     bm.setpixel(z++, y, color.fromargb(pixel.r, pixel.g, pixel.b));//绘图
    }
   }
   return bm;
  }
  #endregion

  #region 上下翻转
  /// <summary>
  /// 上下翻转
  /// </summary>
  /// <param name="mybm">原始图片</param>
  /// <param name="width">原始图片的长度</param>
  /// <param name="height">原始图片的高度</param>
  public bitmap revpicud(bitmap mybm, int width, int height)
  {
   bitmap bm = new bitmap(width, height);
   int x, y, z;
   color pixel;
   for (x = 0; x < width; x++)
   {
    for (y = height - 1, z = 0; y >= 0; y--)
    {
     pixel = mybm.getpixel(x, y);//获取当前像素的值
     bm.setpixel(x, z++, color.fromargb(pixel.r, pixel.g, pixel.b));//绘图
    }
   }
   return bm;
  }
  #endregion

  #region 压缩图片
  /// <summary>
  /// 压缩到指定尺寸
  /// </summary>
  /// <param name="oldfile">原文件</param>
  /// <param name="newfile">新文件</param>
  public bool compress(string oldfile, string newfile)
  {
   try
   {
    system.drawing.image img = system.drawing.image.fromfile(oldfile);
    system.drawing.imaging.imageformat thisformat = img.rawformat;
    size newsize = new size(100, 125);
    bitmap outbmp = new bitmap(newsize.width, newsize.height);
    graphics g = graphics.fromimage(outbmp);
    g.compositingquality = compositingquality.highquality;
    g.smoothingmode = smoothingmode.highquality;
    g.interpolationmode = interpolationmode.highqualitybicubic;
    g.drawimage(img, new rectangle(0, 0, newsize.width, newsize.height), 0, 0, img.width, img.height, graphicsunit.pixel);
    g.dispose();
    encoderparameters encoderparams = new encoderparameters();
    long[] quality = new long[1];
    quality[0] = 100;
    encoderparameter encoderparam = new encoderparameter(system.drawing.imaging.encoder.quality, quality);
    encoderparams.param[0] = encoderparam;
    imagecodecinfo[] arrayici = imagecodecinfo.getimageencoders();
    imagecodecinfo jpegici = null;
    for (int x = 0; x < arrayici.length; x++)
     if (arrayici[x].formatdescription.equals("jpeg"))
     {
      jpegici = arrayici[x]; //设置jpeg编码
      break;
     }
    img.dispose();
    if (jpegici != null) outbmp.save(newfile, system.drawing.imaging.imageformat.jpeg);
    outbmp.dispose();
    return true;
   }
   catch
   {
    return false;
   }
  }
  #endregion

  #region 图片灰度化
  public color gray(color c)
  {
   int rgb = convert.toint32((double)(((0.3 * c.r) + (0.59 * c.g)) + (0.11 * c.b)));
   return color.fromargb(rgb, rgb, rgb);
  }
  #endregion

  #region 转换为黑白图片
  /// <summary>
  /// 转换为黑白图片
  /// </summary>
  /// <param name="mybt">要进行处理的图片</param>
  /// <param name="width">图片的长度</param>
  /// <param name="height">图片的高度</param>
  public bitmap bwpic(bitmap mybm, int width, int height)
  {
   bitmap bm = new bitmap(width, height);
   int x, y, result; //x,y是循环次数,result是记录处理后的像素值
   color pixel;
   for (x = 0; x < width; x++)
   {
    for (y = 0; y < height; y++)
    {
     pixel = mybm.getpixel(x, y);//获取当前坐标的像素值
     result = (pixel.r + pixel.g + pixel.b) / 3;//取红绿蓝三色的平均值
     bm.setpixel(x, y, color.fromargb(result, result, result));
    }
   }
   return bm;
  }
  #endregion

  #region 获取图片中的各帧
  /// <summary>
  /// 获取图片中的各帧
  /// </summary>
  /// <param name="ppath">图片路径</param>
  /// <param name="psavepath">保存路径</param>
  public void getframes(string ppath, string psavedpath)
  {
   image gif = image.fromfile(ppath);
   framedimension fd = new framedimension(gif.framedimensionslist[0]);
   int count = gif.getframecount(fd); //获取帧数(gif图片可能包含多帧,其它格式图片一般仅一帧)
   for (int i = 0; i < count; i++) //以jpeg格式保存各帧
   {
    gif.selectactiveframe(fd, i);
    gif.save(psavedpath + "\\frame_" + i + ".jpg", imageformat.jpeg);
   }
  }
  #endregion
 }
}

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