代码
1
/*
*
2 *
3 * 2009-4-17
4 *
5 *
6 * 根据flash读取其宽度和高度
7 * */
8 using System;
9 using System.Collections.Generic;
10 using System.Linq;
11 using System.Text;
12 using System.IO;
13 using System.Collections;
14
15 namespace CommonData.Flash
16 {
17 [Serializable]
18 public partial class FlashInfo
19 {
20 private int width, height, version, frameCount, fileLength;
21 private float frameRate;
22 private bool isCompressed;
23
24 public FlashInfo( string filename)
25 {
26 if ( ! File.Exists(filename))
27 {
28 throw new FileNotFoundException(filename);
29 }
30 FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
31 BinaryReader reader = new BinaryReader(stream);
32 try
33 {
34 if (stream.Length < 8 )
35 {
36 throw new InvalidDataException( " 文件不是 Flash 文件格式 " );
37 }
38 string flashMark = new string (reader.ReadChars( 3 ));
39 if (flashMark != " FWS " && flashMark != " CWS " )
40 {
41 throw new InvalidDataException( " 文件不是 Flash 文件格式 " );
42 }
43 isCompressed = flashMark == " CWS " ;
44 version = Convert.ToInt32(reader.ReadByte());
45 fileLength = reader.ReadInt32();
46 byte [] dataPart = new byte [stream.Length - 8 ];
47 reader.Read(dataPart, 0 , dataPart.Length);
48 MemoryStream dataStream = new MemoryStream(dataPart);
49 try
50 {
51 if (isCompressed)
52 {
53 MemoryStream outStream = new MemoryStream();
54 zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outStream);
55 CopyStream(dataStream, outZStream);
56 outStream.Position = 0 ;
57 ProcessCompressedPart(outStream);
58 outZStream.Close();
59 outStream.Close();
60 }
61 else
62 ProcessCompressedPart(dataStream);
63 }
64 finally
65 {
66 dataStream.Close();
67 }
68 }
69 finally
70 {
71 reader.Close();
72 stream.Close();
73 }
74 }
75
76 private void ProcessCompressedPart(MemoryStream stream)
77 {
78 BinaryReader reader = new BinaryReader(stream);
79 try
80 {
81 byte [] rect;
82 int nbits, totalBits, totalBytes;
83 nbits = reader.ReadByte() >> 3 ;
84 totalBits = nbits * 4 + 5 ;
85 totalBytes = totalBits / 8 ;
86 if (totalBits % 8 != 0 )
87 {
88 totalBytes ++ ;
89 }
90 reader.BaseStream.Seek( - 1 , SeekOrigin.Current);
91 rect = reader.ReadBytes(totalBytes);
92 frameRate = float .Parse( string .Format( " {1}.{0} " , reader.ReadByte(), reader.ReadByte()));
93 frameCount = Convert.ToInt32(reader.ReadInt16());
94 BitArray bits = new BitArray(rect);
95 bool [] reversedBits = new bool [bits.Length];
96 for ( int i = 0 ; i < totalBytes; i ++ )
97 {
98 int count = 7 ;
99 for ( int j = 8 * i; j < 8 * (i + 1 ); j ++ )
100 {
101 reversedBits[j + count] = bits[j];
102 count -= 2 ;
103 }
104 }
105 bits = new BitArray(reversedBits);
106 StringBuilder sbField = new StringBuilder(bits.Length);
107 for ( int i = 0 ; i < bits.Length; i ++ )
108 sbField.Append(bits[i] ? " 1 " : " 0 " );
109 string result = sbField.ToString();
110 string widthBinary = result.Substring(nbits + 5 , nbits);
111 string heightBinary = result.Substring( 3 * nbits + 5 , nbits);
112 width = Convert.ToInt32(FlashInfo.BinaryToInt64(widthBinary) / 20 );
113 height = Convert.ToInt32(FlashInfo.BinaryToInt64(heightBinary) / 20 );
114 }
115 finally
116 {
117 reader.Close();
118 }
119 }
120
121 private static long BinaryToInt64( string binaryString)
122 {
123 if ( string .IsNullOrEmpty(binaryString))
124 throw new ArgumentNullException();
125 long result = 0 ;
126 for ( int i = 0 ; i < binaryString.Length; i ++ )
127 {
128 result = result * 2 ;
129 if (binaryString[i] == ' 1 ' )
130 result ++ ;
131 }
132 return result;
133 }
134
135 public int Width
136 {
137 get
138 {
139 return this .width;
140 }
141 }
142
143 public int Height
144 {
145 get
146 {
147 return this .height;
148 }
149 }
150
151 public int FileLength
152 {
153 get
154 {
155 return this .fileLength;
156 }
157 }
158
159 public int Version
160 {
161 get
162 {
163 return this .version;
164 }
165 }
166
167 public float FrameRate
168 {
169 get
170 {
171 return this .frameRate;
172 }
173 }
174
175 public int FrameCount
176 {
177 get
178 {
179 return this .frameCount;
180 }
181 }
182
183 public bool IsCompressed
184 {
185 get
186 {
187 return this .isCompressed;
188 }
189 }
190
191 public static void CopyStream(System.IO.Stream input, System.IO.Stream output)
192 {
193 byte [] buffer = new byte [ 2000 ];
194 int len;
195 while ((len = input.Read(buffer, 0 , 2000 )) > 0 )
196 {
197 output.Write(buffer, 0 , len);
198 }
199 output.Flush();
200 }
201 }
202 }
203
2 *
3 * 2009-4-17
4 *
5 *
6 * 根据flash读取其宽度和高度
7 * */
8 using System;
9 using System.Collections.Generic;
10 using System.Linq;
11 using System.Text;
12 using System.IO;
13 using System.Collections;
14
15 namespace CommonData.Flash
16 {
17 [Serializable]
18 public partial class FlashInfo
19 {
20 private int width, height, version, frameCount, fileLength;
21 private float frameRate;
22 private bool isCompressed;
23
24 public FlashInfo( string filename)
25 {
26 if ( ! File.Exists(filename))
27 {
28 throw new FileNotFoundException(filename);
29 }
30 FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
31 BinaryReader reader = new BinaryReader(stream);
32 try
33 {
34 if (stream.Length < 8 )
35 {
36 throw new InvalidDataException( " 文件不是 Flash 文件格式 " );
37 }
38 string flashMark = new string (reader.ReadChars( 3 ));
39 if (flashMark != " FWS " && flashMark != " CWS " )
40 {
41 throw new InvalidDataException( " 文件不是 Flash 文件格式 " );
42 }
43 isCompressed = flashMark == " CWS " ;
44 version = Convert.ToInt32(reader.ReadByte());
45 fileLength = reader.ReadInt32();
46 byte [] dataPart = new byte [stream.Length - 8 ];
47 reader.Read(dataPart, 0 , dataPart.Length);
48 MemoryStream dataStream = new MemoryStream(dataPart);
49 try
50 {
51 if (isCompressed)
52 {
53 MemoryStream outStream = new MemoryStream();
54 zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outStream);
55 CopyStream(dataStream, outZStream);
56 outStream.Position = 0 ;
57 ProcessCompressedPart(outStream);
58 outZStream.Close();
59 outStream.Close();
60 }
61 else
62 ProcessCompressedPart(dataStream);
63 }
64 finally
65 {
66 dataStream.Close();
67 }
68 }
69 finally
70 {
71 reader.Close();
72 stream.Close();
73 }
74 }
75
76 private void ProcessCompressedPart(MemoryStream stream)
77 {
78 BinaryReader reader = new BinaryReader(stream);
79 try
80 {
81 byte [] rect;
82 int nbits, totalBits, totalBytes;
83 nbits = reader.ReadByte() >> 3 ;
84 totalBits = nbits * 4 + 5 ;
85 totalBytes = totalBits / 8 ;
86 if (totalBits % 8 != 0 )
87 {
88 totalBytes ++ ;
89 }
90 reader.BaseStream.Seek( - 1 , SeekOrigin.Current);
91 rect = reader.ReadBytes(totalBytes);
92 frameRate = float .Parse( string .Format( " {1}.{0} " , reader.ReadByte(), reader.ReadByte()));
93 frameCount = Convert.ToInt32(reader.ReadInt16());
94 BitArray bits = new BitArray(rect);
95 bool [] reversedBits = new bool [bits.Length];
96 for ( int i = 0 ; i < totalBytes; i ++ )
97 {
98 int count = 7 ;
99 for ( int j = 8 * i; j < 8 * (i + 1 ); j ++ )
100 {
101 reversedBits[j + count] = bits[j];
102 count -= 2 ;
103 }
104 }
105 bits = new BitArray(reversedBits);
106 StringBuilder sbField = new StringBuilder(bits.Length);
107 for ( int i = 0 ; i < bits.Length; i ++ )
108 sbField.Append(bits[i] ? " 1 " : " 0 " );
109 string result = sbField.ToString();
110 string widthBinary = result.Substring(nbits + 5 , nbits);
111 string heightBinary = result.Substring( 3 * nbits + 5 , nbits);
112 width = Convert.ToInt32(FlashInfo.BinaryToInt64(widthBinary) / 20 );
113 height = Convert.ToInt32(FlashInfo.BinaryToInt64(heightBinary) / 20 );
114 }
115 finally
116 {
117 reader.Close();
118 }
119 }
120
121 private static long BinaryToInt64( string binaryString)
122 {
123 if ( string .IsNullOrEmpty(binaryString))
124 throw new ArgumentNullException();
125 long result = 0 ;
126 for ( int i = 0 ; i < binaryString.Length; i ++ )
127 {
128 result = result * 2 ;
129 if (binaryString[i] == ' 1 ' )
130 result ++ ;
131 }
132 return result;
133 }
134
135 public int Width
136 {
137 get
138 {
139 return this .width;
140 }
141 }
142
143 public int Height
144 {
145 get
146 {
147 return this .height;
148 }
149 }
150
151 public int FileLength
152 {
153 get
154 {
155 return this .fileLength;
156 }
157 }
158
159 public int Version
160 {
161 get
162 {
163 return this .version;
164 }
165 }
166
167 public float FrameRate
168 {
169 get
170 {
171 return this .frameRate;
172 }
173 }
174
175 public int FrameCount
176 {
177 get
178 {
179 return this .frameCount;
180 }
181 }
182
183 public bool IsCompressed
184 {
185 get
186 {
187 return this .isCompressed;
188 }
189 }
190
191 public static void CopyStream(System.IO.Stream input, System.IO.Stream output)
192 {
193 byte [] buffer = new byte [ 2000 ];
194 int len;
195 while ((len = input.Read(buffer, 0 , 2000 )) > 0 )
196 {
197 output.Write(buffer, 0 , len);
198 }
199 output.Flush();
200 }
201 }
202 }
203
这个类用于处理Flash文件的,可以获得falsh 的一些相关信息。这里需要一个插件的处理 zlib.net.dll。 可以到网上去下载