示例#1
0
 private static void Write(BinaryWriter writer, BmpInfoHeader infoHeader)
 {
     writer.Write(infoHeader.HeaderSize);
     writer.Write(infoHeader.Width);
     writer.Write(infoHeader.Height);
     writer.Write(infoHeader.Planes);
     writer.Write(infoHeader.BitsPerPixel);
     writer.Write((int)infoHeader.Compression);
     writer.Write(infoHeader.ImageSize);
     writer.Write(infoHeader.XPelsPerMeter);
     writer.Write(infoHeader.YPelsPerMeter);
     writer.Write(infoHeader.ClrUsed);
     writer.Write(infoHeader.ClrImportant);
 }
示例#2
0
            private void ReadInfoHeader()
            {
                byte[] data = new byte[BmpInfoHeader.Size];

                _stream.Read(data, 0, BmpInfoHeader.Size);

                _infoHeader               = new BmpInfoHeader();
                _infoHeader.HeaderSize    = BitConverter.ToInt32(data, 0);
                _infoHeader.Width         = BitConverter.ToInt32(data, 4);
                _infoHeader.Height        = BitConverter.ToInt32(data, 8);
                _infoHeader.Planes        = BitConverter.ToInt16(data, 12);
                _infoHeader.BitsPerPixel  = BitConverter.ToInt16(data, 14);
                _infoHeader.ImageSize     = BitConverter.ToInt32(data, 20);
                _infoHeader.XPelsPerMeter = BitConverter.ToInt32(data, 24);
                _infoHeader.YPelsPerMeter = BitConverter.ToInt32(data, 28);
                _infoHeader.ClrUsed       = BitConverter.ToInt32(data, 32);
                _infoHeader.ClrImportant  = BitConverter.ToInt32(data, 36);
                _infoHeader.Compression   = (BmpCompression)BitConverter.ToInt32(data, 16);
            }
示例#3
0
        /// <summary>
        /// Encodes the data of the specified image and writes the result to
        /// the specified stream.
        /// </summary>
        /// <param name="image">The image, where the data should be get from.
        /// Cannot be null (Nothing in Visual Basic).</param>
        /// <param name="stream">The stream, where the image data should be written to.
        /// Cannot be null (Nothing in Visual Basic).</param>
        /// <exception cref="ArgumentNullException">
        ///     <para><paramref name="image"/> is null (Nothing in Visual Basic).</para>
        ///     <para>- or -</para>
        ///     <para><paramref name="stream"/> is null (Nothing in Visual Basic).</para>
        /// </exception>
        public void Encode(ImageBase image, Stream stream)
        {
            Guard.NotNull(image, "image");
            Guard.NotNull(stream, "stream");

            int rowWidth = image.PixelWidth;

            int amount = (image.PixelWidth * 3) % 4;

            if (amount != 0)
            {
                rowWidth += 4 - amount;
            }

            BinaryWriter writer = new BinaryWriter(stream);

            BmpFileHeader fileHeader = new BmpFileHeader();

            fileHeader.Type     = 19778;
            fileHeader.Offset   = 54;
            fileHeader.FileSize = 54 + image.PixelHeight * rowWidth * 3;
            Write(writer, fileHeader);

            BmpInfoHeader infoHeader = new BmpInfoHeader();

            infoHeader.HeaderSize   = 40;
            infoHeader.Height       = image.PixelHeight;
            infoHeader.Width        = image.PixelWidth;
            infoHeader.BitsPerPixel = 24;
            infoHeader.Planes       = 1;
            infoHeader.Compression  = BmpCompression.RGB;
            infoHeader.ImageSize    = image.PixelHeight * rowWidth * 3;
            infoHeader.ClrUsed      = 0;
            infoHeader.ClrImportant = 0;
            Write(writer, infoHeader);

            WriteImage(writer, image);

            writer.Flush();
        }
示例#4
0
        private static BmpPixel[,] ParseImageData(byte[] bmpBytes, BmpHeader header, BmpInfoHeader infoHeader)
        {
            if (infoHeader.Compression != 0)
            {
                var debug = "compression";
            }

            var offSet     = header.DataOffSet;
            var pixelCount = infoHeader.ImageSize > 0 ? infoHeader.ImageSize : infoHeader.Width * infoHeader.Height;

            var pixels    = new BmpPixel[infoHeader.Height, infoHeader.Width];
            var byteSteps = infoHeader.BitPerPixel / 8;

            for (var i = 0; i < pixelCount; i++)
            {
                var byteIndex = (i * byteSteps) + offSet;
                var y         = infoHeader.Height - 1 - i / infoHeader.Width;
                var x         = i % infoHeader.Width;
                var pixel     = new BmpPixel
                {
                    Blue  = bmpBytes[byteIndex + 0],
                    Green = bmpBytes[byteIndex + 1],
                    Red   = bmpBytes[byteIndex + 2]
                };
                if (infoHeader.BitPerPixel >= 32)
                {
                    pixel.Alpha = bmpBytes[byteIndex + 2];
                }
                else
                {
                    pixel.Alpha = 255;
                }

                pixels[y, x] = pixel;
            }

            return(pixels);
        }
示例#5
0
        public static Bmp FromPixels(BmpPixel[,] pixels, ushort bitsPerPixel = 32)
        {
            var header = new BmpHeader {
                DataOffSet = 54,
                FileSize   = (uint)(pixels.Length * (bitsPerPixel / 8) + 54),
                Reserved   = 0,
                Signature  = 0x4d42
            };

            var height = (uint)pixels.GetLength(0);
            var width  = (uint)pixels.GetLength(1);

            var infoHeader = new BmpInfoHeader
            {
                BitPerPixel     = bitsPerPixel,
                ColorsUsed      = 0,
                Compression     = 0,
                Height          = height,
                Width           = width,
                ImageSize       = 0,
                ImportantColors = 0,
                Planes          = 1,
                Size            = 40,
                XpixelsPerM     = 3780,
                YpixelsPerM     = 3780,
            };


            var pixelData = pixels;

            return(new Bmp
            {
                Header = header,
                InfoHeader = infoHeader,
                Pixels = pixelData
            });
        }
示例#6
0
 private static void Write(BinaryWriter writer, BmpInfoHeader infoHeader)
 {
     writer.Write(infoHeader.HeaderSize);
     writer.Write(infoHeader.Width);
     writer.Write(infoHeader.Height);
     writer.Write(infoHeader.Planes);
     writer.Write(infoHeader.BitsPerPixel);
     writer.Write((int)infoHeader.Compression);
     writer.Write(infoHeader.ImageSize);
     writer.Write(infoHeader.XPelsPerMeter);
     writer.Write(infoHeader.YPelsPerMeter);
     writer.Write(infoHeader.ClrUsed);
     writer.Write(infoHeader.ClrImportant);
 }
示例#7
0
                public void Encode(Image image, Stream stream)
                {
                    int rowWidth = image.Width;

                    int amount = (image.Width * 3) % 4;
                    if (amount != 0)
                    {
                        rowWidth += 4 - amount;
                    }

                    BinaryWriter writer = new BinaryWriter(stream);

                    BmpFileHeader fileHeader = new BmpFileHeader();
                    fileHeader.Type = 19778;
                    fileHeader.Offset = 54;
                    fileHeader.FileSize = 54 + image.Height * rowWidth * 3;
                    Write(writer, fileHeader);

                    BmpInfoHeader infoHeader = new BmpInfoHeader();
                    infoHeader.HeaderSize = 40;
                    infoHeader.Height = image.Height;
                    infoHeader.Width = image.Width;
                    infoHeader.BitsPerPixel = 24;
                    infoHeader.Planes = 1;
                    infoHeader.Compression = BmpCompression.RGB;
                    infoHeader.ImageSize = image.Height * rowWidth * 3;
                    infoHeader.ClrUsed = 0;
                    infoHeader.ClrImportant = 0;
                    Write(writer, infoHeader);

                    WriteImage(writer, image);

                    writer.Flush();
                }
示例#8
0
                private void ReadInfoHeader()
                {
                    byte[] data = new byte[BmpInfoHeader.Size];

                    _stream.Read(data, 0, BmpInfoHeader.Size);

                    _infoHeader = new BmpInfoHeader();
                    _infoHeader.HeaderSize = BitConverter.ToInt32(data, 0);
                    _infoHeader.Width = BitConverter.ToInt32(data, 4);
                    _infoHeader.Height = BitConverter.ToInt32(data, 8);
                    _infoHeader.Planes = BitConverter.ToInt16(data, 12);
                    _infoHeader.BitsPerPixel = BitConverter.ToInt16(data, 14);
                    _infoHeader.ImageSize = BitConverter.ToInt32(data, 20);
                    _infoHeader.XPelsPerMeter = BitConverter.ToInt32(data, 24);
                    _infoHeader.YPelsPerMeter = BitConverter.ToInt32(data, 28);
                    _infoHeader.ClrUsed = BitConverter.ToInt32(data, 32);
                    _infoHeader.ClrImportant = BitConverter.ToInt32(data, 36);
                    _infoHeader.Compression = (BmpCompression)BitConverter.ToInt32(data, 16);
                }