예제 #1
0
 /// <summary>
 /// Writes the bitmap header data to the binary stream.
 /// </summary>
 /// <param name="writer">
 /// The <see cref="EndianBinaryWriter"/> containing the stream to write to.
 /// </param>
 /// <param name="fileHeader">
 /// The <see cref="BmpFileHeader"/> containing the header data.
 /// </param>
 private static void WriteHeader(EndianBinaryWriter writer, BmpFileHeader fileHeader)
 {
     writer.Write(fileHeader.Type);
     writer.Write(fileHeader.FileSize);
     writer.Write(fileHeader.Reserved);
     writer.Write(fileHeader.Offset);
 }
예제 #2
0
        /// <summary>
        /// Reads the <see cref="BmpFileHeader"/> from the stream.
        /// </summary>
        private void ReadFileHeader()
        {
            byte[] data = new byte[BmpFileHeader.Size];

            this.currentStream.Read(data, 0, BmpFileHeader.Size);

            this.fileHeader = new BmpFileHeader
            {
                Type     = BitConverter.ToInt16(data, 0),
                FileSize = BitConverter.ToInt32(data, 2),
                Reserved = BitConverter.ToInt32(data, 6),
                Offset   = BitConverter.ToInt32(data, 10)
            };
        }
예제 #3
0
        /// <summary>
        /// Encodes the image to the specified stream from the <see cref="ImageBase{TColor, TPacked}"/>.
        /// </summary>
        /// <typeparam name="TColor">The pixel format.</typeparam>
        /// <typeparam name="TPacked">The packed format. <example>long, float.</example></typeparam>
        /// <param name="image">The <see cref="ImageBase{TColor, TPacked}"/> to encode from.</param>
        /// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param>
        /// <param name="bitsPerPixel">The <see cref="BmpBitsPerPixel"/></param>
        public void Encode <TColor, TPacked>(ImageBase <TColor, TPacked> image, Stream stream, BmpBitsPerPixel bitsPerPixel)
            where TColor : struct, IPackedPixel <TPacked>
            where TPacked : struct
        {
            Guard.NotNull(image, nameof(image));
            Guard.NotNull(stream, nameof(stream));

            this.bmpBitsPerPixel = bitsPerPixel;

            // Cast to int will get the bytes per pixel
            short bpp          = (short)(8 * (int)bitsPerPixel);
            int   bytesPerLine = 4 * (((image.Width * bpp) + 31) / 32);

            this.padding = bytesPerLine - (image.Width * (int)bitsPerPixel);

            // Do not use IDisposable pattern here as we want to preserve the stream.
            EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Little, stream);

            BmpInfoHeader infoHeader = new BmpInfoHeader
            {
                HeaderSize   = BmpInfoHeader.Size,
                Height       = image.Height,
                Width        = image.Width,
                BitsPerPixel = bpp,
                Planes       = 1,
                ImageSize    = image.Height * bytesPerLine,
                ClrUsed      = 0,
                ClrImportant = 0
            };

            BmpFileHeader fileHeader = new BmpFileHeader
            {
                Type     = 19778, // BM
                Offset   = 54,
                FileSize = 54 + infoHeader.ImageSize
            };

            WriteHeader(writer, fileHeader);
            this.WriteInfo(writer, infoHeader);
            this.WriteImage(writer, image);

            writer.Flush();
        }