读取结构文件

struct FontMessageType
  {
   public int Width; //字模宽
   [MarshalAs(UnmanagedType.ByValArray,SizeConst=51)]
   public LinePoint_AType[] LineType;  //构成字模的矢量
      }

  struct LinePoint_AType
  {
   public int PointNum;
   [MarshalAs(UnmanagedType.ByValArray,SizeConst=200)]
   public Point[] LinePoint_A;
  }
  private void ReadStructFile(String filename)
  {
   byte[] btv;
   String strv = "";
   FontMessageType btest = new FontMessageType();
   int len = 1;

   btest.LineType = new LinePoint_AType[51];

   for (int i = 0; i < btest.LineType.Length; i++)
   {
    btest.LineType[i].LinePoint_A = new Point[200];
   }

   FileStream fs = new FileStream(filename, FileMode.Open);//"aa.txt"
   BinaryReader br = new BinaryReader(fs);

   len = Marshal.SizeOf(btest);
   btv = new byte[len];
   btv = br.ReadBytes((int)fs.Length);
   strv = Encoding.Default.GetString(btv);
   br.Close(); fs.Close(); fs.Dispose();
   
   IntPtr ptr = Marshal.AllocHGlobal(len);
   try
   {
    Marshal.Copy(btv, 0, ptr, Math.Min(len, btv.Length));
    btest = (FontMessageType)Marshal.PtrToStructure(ptr, typeof(FontMessageType));
   }
   finally
   {
    Marshal.FreeHGlobal(ptr);
   }
  }

posted @ 2008-07-29 20:24  《云中漫步》  阅读(154)  评论(0)    收藏  举报