ASP教程 PHP教程 JSP教程 CGI教程 XML教程
  • ASP.NET教程
  • JAVA教程
    当前位置:教程 > 程序设计 > ASP.NET教程 > 正文

    编程实现QQ表情文件CFC格式(2)

    编辑:教程网 来源:网络 时间:2011年10月26日 15:42
    我们再建一个类,命名为: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)
  • 原文来自 教程网 转载请注明:http://www.websmill.com/chengxu/aspnet/29587.html

  • 上一篇:ASP.Net Web Page 深入探讨(1)
  • 下一篇:正则表达式regular expression详述