打印

[asp.net教程] 编程实现qq表情文件cfc格式(2)

编程实现qq表情文件cfc格式(2)

我们再建一个类,命名为:facehelper
  代码如下:
   public class facehelper
   {
   internal static faceblock getfaceblockfromimage(string file)
   {
   faceblock fb = new faceblock();
   //打开文件流
   filestream fs = new filestream(file, filemode.open, fileaccess.read);
   //获取图像
   image img = image.fromstream(fs);
   //获得一个20*20的缩略图
   image thumbnail = img.getthumbnailimage(20, 20, null, intptr.zero);
   memorystream ms = new memorystream();
   //将缩图图转化数byte数组
   thumbnail.save(ms, system.drawing.imaging.imageformat.bmp);
   byte[] thumbnaildata = ms.toarray();
   ms.close();
   ms.dispose();
   thumbnail.dispose();
   
   //得到一个唯一的md5字符串
   string md5 = getmd5(fs);
   //文件名,格式为:md5 + 扩展名
   string filename = string.format("{0}{1}", md5, path.getextension(file));
   //缩略图文件名,格式为:md5 + fixed.bmp
   string thumbnailname = string.format("{0}fixed.bmp", md5);
   //随机设置一个快捷键
   string uintcuts = "qq.5inet.net_" + randomnum(6);
   fs.close();
   fs.dispose();
   
   //取得总的帧数
  system.drawing.imaging.framedimension fd = system.drawing.imaging.framedimension.resolution;
   int framecount = img.framedimensionslist.length;
   img.dispose();
   
   fb.md5 = md5;

   fb.md5length = (uint)md5.length;
   fb.uintcuts = uintcuts;
   fb.uintcutlength = (uint)uintcuts.length;
   fb.facename = uintcuts;
   fb.facenamelength = (uint)uintcuts.length;
   fb.facefilename = filename;
   fb.facefilenamelength = (uint)filename.length;
   fb.thumbnailfilename = thumbnailname;
   fb.thumbnailfilenamelength = (uint)thumbnailname.length;
   fb.facedata = file.readallbytes(file);
   fb.filelength = (uint)fb.facedata.length;
   fb.thumbnaildata = thumbnaildata;
   fb.thumbnailfilelength = (uint)thumbnaildata.length;
   fb.framelength = (uint)framecount;
   
   return fb;
   }
   
   helper#region helper
   //随机方法
   internal static string randomnum(int n) //
   {
   string strchar = "0,1,2,3,4,5,6,7,8,9";
   string[] vcarray = strchar.split(',');
   string vnum = "";//由于字符串很短,f77pclw,c络g|?,业,e'b就不用stringbuilder了
   int temp = -1; //记录上次随机数值,尽量避免产生几个一样的随机数
   //采用一个简单的算法以保证生成随机数的不同
   random rand = new random();
   for (int i = 1; i < n + 1; i++)
   {
   if (temp != -1)
   {
   rand = new random(i * temp * unchecked((int)
   
   datetime.now.ticks));
   }
   //int t = rand.next(35) ;
   int t = rand.next(10);
   if (temp != -1 && temp == t)
   {
   return randomnum(n);
   }
   temp = t;
   vnum += vcarray[t];
   }
   return vnum;//返回生成的随机数
   }
   
   //从文件名获得md5
   internal static string getmd5(filestream fs)
   {
   md5cryptoserviceprovider md5 = new md5cryptoserviceprovider();
   byte[] md5byte = md5.computehash(fs);
   string str = string.empty;
   int i, j;
   foreach (byte b in md5byte)
   {
   i = convert.toint32(b);
   j = i >> 4;
   str += (convert.tostring(j, 16));
   j = ((i << 4) & 0x00ff) >> 4;
   str += (convert.tostring(j, 16));
   }
   
   return str.toupper();
   }
   #endregion
   
   //从一个目录生成一个cfc文件集合
   public static void
  buildcfcfilefromdirectory(string directory)
   {
   list bytes = new list();
   foreach (string file in directory.getfiles(directory))
   {
   if (!isimagefile(file))
   continue;
   
   bytes.addrange(faceblock.fromimage(file).tobytes());
   }
   
   string fname = path.combine(directory, path.getdirectoryname(directory) + ".cfc");
   filestream fs = file.create(fname);
   fs.write(bytes.toarray(), 0, bytes.count);
   fs.close();
   }
   
   //判断是否为图像文件,方法比较简陋。
   private static bool isimagefile(string file)
   {
   list validext = new list(new string[]{
   ".bmp",
   ".jpg",
   ".jpeg",
   ".gif",
   ".png",
   });
   
   return validext.contains(path.getextension(file).tolower());
   }
   }
  好,有了上面的方法,我们就可以调用了。
  调用方法实在是有些简单。
   
  facehelper.buildcfcfilefromdirectory(server.mappath("~/img/"));
  这样就ok了,现在去你的网站根目录下看看,有没有一个img.cfc的文件呢?再双击一下,是不是将img目录下的文件全部导入到qq表情里了呢? enjoy coding!

TOP

返回顶部
AYBlue

Processed in 0.057391 second(s), 7 queries.

当前时区 GMT+8, 现在时间是 2009-1-10 11:51 京ICP备06054220号

清除 Cookies - 联系我们 - 163K.com - Archiver - WAP