// 从协议中获取字符串内容 public String GetString() { if (bytes == null) { return(""); } if (bytes.Length < start + 4) { return(""); } // 获得协议中字符串的长度 int strLen = MyBitConverter.ToInt32(bytes, start); if (bytes.Length < start + 4 + strLen) { return(""); } // 获得协议中具体的字符串的内容 String str = System.Text.Encoding.UTF8.GetString(bytes, start + sizeof(Int32), strLen); // start指针前移 this.start = start + 4 + strLen; return(str); }
/// <summary> /// Reads a PNG header chunk. /// </summary> /// <param name="headerBlock">Block of data containing headers.</param> /// <param name="offset">Offset in data to begin header.</param> public PNGChunk(byte[] headerBlock, int offset = 8) { Length = MyBitConverter.ToInt32(headerBlock, offset, MyBitConverter.Endianness.BigEndian); ChunkType = MyBitConverter.ToInt32(headerBlock, offset + 4, MyBitConverter.Endianness.BigEndian); ChunkData = new byte[Length]; Array.Copy(headerBlock, offset + 8, ChunkData, 0, Length); CRC = MyBitConverter.ToInt32(headerBlock, offset + 8 + Length, MyBitConverter.Endianness.BigEndian); }
public int GetInt() { if (bytes == null) { return(0); } if (bytes.Length < start + 4) { return(0); } int result = MyBitConverter.ToInt32(bytes, start); start = start + 4; return(result); }
/// <summary> /// Read field descriptor from block. /// A "property" of the image. /// </summary> /// <param name="IDBlock">Block containing descriptor, but NOT it's data.</param> /// <param name="endianness">Big or little endianness defined by TIFF header.</param> /// <param name="dataStream">Full image stream to read descriptor data from.</param> public FieldDescriptor(byte[] IDBlock, MyBitConverter.Endianness endianness, Stream dataStream) { FieldTag = (FieldTags)MyBitConverter.ToInt16(IDBlock, 0, endianness); FieldType = (FieldTypes)MyBitConverter.ToInt16(IDBlock, 2, endianness); FieldLength = MyBitConverter.ToInt32(IDBlock, 4, endianness); DataOffset = MyBitConverter.ToInt32(IDBlock, 8, endianness); // Read data indicated by descriptor long oldOffset = dataStream.Position; dataStream.Seek(DataOffset, SeekOrigin.Begin); Data = new byte[FieldLength]; dataStream.Read(Data, 0, FieldLength); // Reset stream position for next descriptor dataStream.Seek(oldOffset, SeekOrigin.Begin); }
/// <summary> /// Loads PNG header from stream. /// </summary> /// <param name="stream">Fully formatted header stream. Position not relevant, but not reset.</param> /// <returns>Header length.</returns> protected override long Load(Stream stream) { base.Load(stream); byte[] temp = stream.ReadBytes(HeaderSize); if (!CheckIdentifier(temp)) { throw new FormatException("Stream is not a PNG Image"); } PNGChunk header = new PNGChunk(temp); Width = MyBitConverter.ToInt32(header.ChunkData, 0, MyBitConverter.Endianness.BigEndian); Height = MyBitConverter.ToInt32(header.ChunkData, 4, MyBitConverter.Endianness.BigEndian); BitDepth = header.ChunkData[8]; colourType = (ColourType)header.ChunkData[9]; CompressionMethod = (CompressionMethods)header.ChunkData[10]; FilterMethod = (FilterMethods)header.ChunkData[11]; InterlaceMethod = (InterlaceMethdods)header.ChunkData[12]; return(-1); // Since we don't know the length of the entire header, no point returning any value. }