private static void Write(BinaryWriter writer, BmpFileHeader fileHeader) { writer.Write(fileHeader.Type); writer.Write(fileHeader.FileSize); writer.Write(fileHeader.Reserved); writer.Write(fileHeader.Offset); }
public void TestWrite() { var header = new BmpFileHeader(1, 2, 3, 4); var buffer = new byte[14]; header.WriteTo(buffer); Assert.Equal("AQACAAAAAwAAAAQAAAA=", Convert.ToBase64String(buffer)); }
private void ReadFileHeader() { byte[] data = new byte[BmpFileHeader.Size]; _stream.Read(data, 0, BmpFileHeader.Size); _fileHeader = new BmpFileHeader(); _fileHeader.Type = BitConverter.ToInt16(data, 0); _fileHeader.FileSize = BitConverter.ToInt32(data, 2); _fileHeader.Reserved = BitConverter.ToInt32(data, 6); _fileHeader.Offset = BitConverter.ToInt32(data, 10); }
public static bool WriteBmp(this RayImage img, Stream output) { try { var sizeFileHeader = Marshal.SizeOf <BmpFileHeader>(); //const int sizeFileHeader = 14; var sizeInfoHeader = Marshal.SizeOf <BmpInfoHeader>(); //const int sizeInfoHeader = 40; var sizePixels = img.Width * img.Height * 3; var buffer = new byte[sizeFileHeader + sizeInfoHeader + sizePixels]; var span = new Span <byte>(buffer); // write file header. ref BmpFileHeader fileHeader = ref MemoryMarshal.Cast <byte, BmpFileHeader>(span)[0]; fileHeader.Signature = (int)('B') + ((int)('M') << 8); fileHeader.FileSize = buffer.Length; fileHeader.DataOffset = sizeFileHeader + sizeInfoHeader; // write info header. ref BmpInfoHeader infoHeader = ref MemoryMarshal.Cast <byte, BmpInfoHeader>(span.Slice(sizeFileHeader, sizeInfoHeader))[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(); }
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(); }