private static void WriteWmfHeader(Metafile metafile, Stream stream)
        {
            WmfHeader header = new WmfHeader((short)metafile.Width, (short)metafile.Height, (ushort)metafile.HorizontalResolution);

            byte[] rawHeader = BinarySerializer.SerializeValueType(header);
            stream.Write(rawHeader, 0, rawHeader.Length);
        }
예제 #2
0
        /// <summary>
        /// Fixes file size field, max records field and number of objects field.
        /// </summary>
        public void FixHeader()
        {
            if (!stream.CanSeek)
            {
                throw new WmfException("Cannot fix WMF header since the stream does not support seeking!");
            }

            //Skip WmfFormat record
            stream.Seek(22, SeekOrigin.Begin);

            //Read whole WmfHeader record
            byte[] buffer = new byte[18];
            stream.Read(buffer, 0, 18);

            //Create new header record
            var header = new WmfHeader();

            using (var ms = new MemoryStream(buffer))
            {
                using (var reader = new BinaryReader(ms))
                {
                    uint max, count;
                    Find(out max, out count);

                    long l = stream.Length - 22;                     //Length without WmfFormat in bytes
                    if (l % 2 == 1)
                    {
                        l += 1;                            //Round to WORD padding
                    }
                    long size = l / 2;                     //Length in words

                    header.Read(reader);
                    header.FileSize        = (uint)size;
                    header.NumberOfObjects = (ushort)count;
                    header.MaxRecord       = max;
                }
            }

            byte[] fix = new byte[18];
            using (var ms = new MemoryStream())
            {
                using (var writer = new BinaryWriter(ms))
                {
                    header.Write(writer);
                    fix = ms.ToArray();
                }
            }

            //Rewind back to position of overwrite
            this.stream.Seek(22, SeekOrigin.Begin);
            this.writer.Write(fix);

            //Forward to when fixing started
            stream.Seek(0, SeekOrigin.End);
        }
예제 #3
0
        public IBinaryRecord Read()
        {
            if (!this.IsFormatRead)
            {
                this.IsFormatRead = true;
                var format = new WmfFormat();
                format.Read(reader);
                return(format);
            }

            if (!this.IsHeaderRead)
            {
                this.IsHeaderRead = true;
                var header = new WmfHeader();
                header.Read(reader);
                return(header);
            }

            long begin = reader.BaseStream.Position;

            uint   length = reader.ReadUInt32();           //Length in WORDs
            ushort type   = reader.ReadUInt16();

            var rt     = (RecordType)type;
            var record = WmfHelper.GetRecordByType(rt) as WmfBinaryRecord;

            if (record == null)
            {
                record            = new WmfUnknownRecord();
                record.RecordType = rt;                 //Only set for UnknownRecord otherwise it is already defined by attribute above the class
            }

            record.RecordSize = length;
            record.Read(reader);

            long end    = reader.BaseStream.Position;
            long rlen   = end - begin;           //Read length
            long excess = 2 * length - rlen;

            if (excess > 0)
            {
                //Oops, reader did not read whole record?!
                reader.Skip((int)excess);
            }

            return(record);
        }