• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dapplo / Dapplo.Windows / 22078775535

16 Feb 2026 10:03PM UTC coverage: 34.168% (-1.5%) from 35.711%
22078775535

push

github

Lakritzator
Fixed test-case (I modified the code via another solution, didn't see the test)

581 of 1830 branches covered (31.75%)

Branch coverage included in aggregate %.

1616 of 4600 relevant lines covered (35.13%)

23.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Dapplo.Windows.Gdi32/Structs/BitmapInfoHeader.cs
1
// Copyright (c) Dapplo and contributors. All rights reserved.
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3

4
using System;
5
using System.Runtime.InteropServices;
6
using Dapplo.Windows.Gdi32.Enums;
7

8
namespace Dapplo.Windows.Gdi32.Structs;
9

10
/// <summary>
11
///     See
12
///     <a href="https://docs.microsoft.com/en-us/previous-versions/dd183376(v=vs.85)">BITMAPINFOHEADER structure</a>
13
/// </summary>
14
[StructLayout(LayoutKind.Explicit)]
15
public struct BitmapInfoHeader
16
{
17
    [FieldOffset(0)]
18
    private uint _biSize;
19
    [FieldOffset(4)]
20
    private int _biWidth;
21
    [FieldOffset(8)]
22
    private int _biHeight;
23
    [FieldOffset(12)]
24
    private ushort _biPlanes;
25
    [FieldOffset(14)]
26
    private ushort _biBitCount;
27
    [FieldOffset(16)]
28
    private BitmapCompressionMethods _biCompression;
29
    [FieldOffset(20)]
30
    private uint _biSizeImage;
31
    [FieldOffset(24)]
32
    private int _biXPelsPerMeter;
33
    [FieldOffset(28)]
34
    private int _biYPelsPerMeter;
35
    [FieldOffset(32)]
36
    private uint _biClrUsed;
37
    [FieldOffset(36)]
38
    private uint _biClrImportant;
39

40
    /// <summary>
41
    ///     The number of bytes required by the structure.
42
    ///     Applications should use this member to determine which bitmap information header structure is being used.
43
    /// </summary>
44
    public uint Size {
45
        get => _biSize;
×
46
        set => _biSize = value;
×
47
    }
48

49
    /// <summary>
50
    ///     The width of the bitmap, in pixels.
51
    ///     If bV5Compression is BI_JPEG or BI_PNG, the bV5Width member specifies the width of the decompressed JPEG or PNG
52
    ///     image in pixels.
53
    /// </summary>
54
    public int Width
55
    {
56
        get => _biWidth;
×
57
        set => _biWidth = value;
×
58
    }
59

60

61
    /// <summary>
62
    ///     The height of the bitmap, in pixels. If the value of bV5Height is positive, the bitmap is a bottom-up DIB and its
63
    ///     origin is the lower-left corner. If bV5Height value is negative, the bitmap is a top-down DIB and its origin is the
64
    ///     upper-left corner.
65
    ///     If bV5Height is negative, indicating a top-down DIB, bV5Compression must be either BI_RGB or BI_BITFIELDS. Top-down
66
    ///     DIBs cannot be compressed.
67
    ///     If bV5Compression is BI_JPEG or BI_PNG, the bV5Height member specifies the height of the decompressed JPEG or PNG
68
    ///     image in pixels.
69
    /// </summary>
70
    public int Height
71
    {
72
        get => _biHeight;
×
73
        set => _biHeight = value;
×
74
    }
75

76
    /// <summary>
77
    ///     The number of planes for the target device. This value must be set to 1.
78
    /// </summary>
79
    public ushort Planes
80
    {
81
        get => _biPlanes;
×
82
        set => _biPlanes = value;
×
83
    }
84

85
    /// <summary>
86
    ///     The number of bits that define each pixel and the maximum number of colors in the bitmap.
87
    ///     This member can be one of the following values:
88
    ///     0        The number of bits per pixel is specified or is implied by the JPEG or PNG file format.
89
    ///     1        The bitmap is monochrome, and the bmiColors member of BITMAPINFO contains two entries. Each bit in the bitmap
90
    ///     array represents a pixel. If the bit is clear, the pixel is displayed with the color of the first entry in the
91
    ///     bmiColors color table. If the bit is set, the pixel has the color of the second entry in the table.
92
    ///     4        The bitmap has a maximum of 16 colors, and the bmiColors member of BITMAPINFO contains up to 16 entries. Each
93
    ///     pixel in the bitmap is represented by a 4-bit index into the color table. For example, if the first byte in the
94
    ///     bitmap is 0x1F, the byte represents two pixels. The first pixel contains the color in the second table entry, and
95
    ///     the second pixel contains the color in the sixteenth table entry.
96
    ///     8        The bitmap has a maximum of 256 colors, and the bmiColors member of BITMAPINFO contains up to 256 entries. In
97
    ///     this case, each byte in the array represents a single pixel.
98
    ///     16        The bitmap has a maximum of 2^16 colors. If the bV5Compression member of the BITMAPV5HEADER structure is BI_RGB,
99
    ///     the bmiColors member of BITMAPINFO is NULL. Each WORD in the bitmap array represents a single pixel. The relative
100
    ///     intensities of red, green, and blue are represented with five bits for each color component. The value for blue is
101
    ///     in the least significant five bits, followed by five bits each for green and red. The most significant bit is not
102
    ///     used. The bmiColors color table is used for optimizing colors used on palette-based devices, and must contain the
103
    ///     number of entries specified by the bV5ClrUsed member of the BITMAPV5HEADER.
104
    ///     If the bV5Compression member of the BITMAPV5HEADER is BI_BITFIELDS, the bmiColors member contains three DWORD color
105
    ///     masks that specify the red, green, and blue components, respectively, of each pixel. Each WORD in the bitmap array
106
    ///     represents a single pixel.
107
    ///     When the bV5Compression member is BI_BITFIELDS, bits set in each DWORD mask must be contiguous and should not
108
    ///     overlap the bits of another mask. All the bits in the pixel do not need to be used.
109
    ///     24        The bitmap has a maximum of 2^24 colors, and the bmiColors member of BITMAPINFO is NULL. Each 3-byte triplet in
110
    ///     the bitmap array represents the relative intensities of blue, green, and red, respectively, for a pixel. The
111
    ///     bmiColors color table is used for optimizing colors used on palette-based devices, and must contain the number of
112
    ///     entries specified by the bV5ClrUsed member of the BITMAPV5HEADER structure.
113
    ///     32        The bitmap has a maximum of 2^32 colors. If the bV5Compression member of the BITMAPV5HEADER is BI_RGB, the
114
    ///     bmiColors member of BITMAPINFO is NULL. Each DWORD in the bitmap array represents the relative intensities of blue,
115
    ///     green, and red for a pixel. The value for blue is in the least significant 8 bits, followed by 8 bits each for
116
    ///     green and red. The high byte in each DWORD is not used. The bmiColors color table is used for optimizing colors
117
    ///     used on palette-based devices, and must contain the number of entries specified by the bV5ClrUsed member of the
118
    ///     BITMAPV5HEADER.
119
    ///     If the bV5Compression member of the BITMAPV5HEADER is BI_BITFIELDS, the bmiColors member contains three DWORD color
120
    ///     masks that specify the red, green, and blue components of each pixel. Each DWORD in the bitmap array represents a
121
    ///     single pixel.
122
    /// </summary>
123
    public ushort BitCount
124
    {
125
        get => _biBitCount;
×
126
        set => _biBitCount = value;
×
127
    }
128

129
    /// <summary>
130
    ///     Specifies that the bitmap is not compressed.
131
    ///     The bV5RedMask, bV5GreenMask, and bV5BlueMask members specify the red, green, and blue components of each pixel.
132
    ///     This is valid when used with 16- and 32-bpp bitmaps.
133
    /// </summary>
134
    public BitmapCompressionMethods Compression
135
    {
136
        get => _biCompression;
×
137
        set => _biCompression = value;
×
138
    }
139

140
    /// <summary>
141
    ///     The size, in bytes, of the image. This may be set to zero for BI_RGB bitmaps.
142
    ///     If bV5Compression is BI_JPEG or BI_PNG, bV5SizeImage is the size of the JPEG or PNG image buffer.
143
    /// </summary>
144
    public uint SizeImage
145
    {
146
        get => _biSizeImage;
×
147
        set => _biSizeImage = value;
×
148
    }
149

150
    /// <summary>
151
    ///     The horizontal resolution, in pixels-per-meter, of the target device for the bitmap. An application can use this
152
    ///     value to select a bitmap from a resource group that best matches the characteristics of the current device.
153
    /// </summary>
154
    public int XPelsPerMeter
155
    {
156
        get => _biXPelsPerMeter;
×
157
        set => _biXPelsPerMeter = value;
×
158
    }
159

160
    /// <summary>
161
    ///     The vertical resolution, in pixels-per-meter, of the target device for the bitmap.
162
    /// </summary>
163
    public int YPelsPerMeter
164
    {
165
        get => _biYPelsPerMeter;
×
166
        set => _biYPelsPerMeter = value;
×
167
    }
168

169
    /// <summary>
170
    ///     The number of color indexes in the color table that are actually used by the bitmap. If this value is zero, the
171
    ///     bitmap uses the maximum number of colors corresponding to the value of the bV5BitCount member for the compression
172
    ///     mode specified by bV5Compression.
173
    ///     If bV5ClrUsed is nonzero and bV5BitCount is less than 16, the bV5ClrUsed member specifies the actual number of
174
    ///     colors the graphics engine or device driver accesses. If bV5BitCount is 16 or greater, the bV5ClrUsed member
175
    ///     specifies the size of the color table used to optimize performance of the system color palettes. If bV5BitCount
176
    ///     equals 16 or 32, the optimal color palette starts immediately following the BITMAPV5HEADER. If bV5ClrUsed is
177
    ///     nonzero, the color table is used on palettized devices, and bV5ClrUsed specifies the number of entries.
178
    /// </summary>
179
    public uint ColorsUsed
180
    {
181
        get => _biClrUsed;
×
182
        set => _biClrUsed = value;
×
183
    }
184

185
    /// <summary>
186
    ///     The number of color indexes that are required for displaying the bitmap. If this value is zero, all colors are
187
    ///     required.
188
    /// </summary>
189
    public uint ColorsImportant
190
    {
191
        get => _biClrImportant;
×
192
        set => _biClrImportant = value;
×
193
    }
194
        
195
    /// <summary>
196
    ///     Constructor with values
197
    /// </summary>
198
    /// <param name="width">int with the width of the bitmap</param>
199
    /// <param name="height">int with the height of the bitmap</param>
200
    /// <param name="bpp">int with the bits per pixel of the bitmap</param>
201
    public static BitmapInfoHeader Create(int width, int height, ushort bpp)
202
    {
203
        return new BitmapInfoHeader
×
204
        {
×
205
            // BITMAPINFOHEADER < DIBV5 is 40 bytes
×
206
            _biSize = (uint) Marshal.SizeOf(typeof(BitmapInfoHeader)),
×
207
            // Should always be 1
×
208
            _biPlanes = 1,
×
209
            _biCompression = BitmapCompressionMethods.BI_RGB,
×
210
            _biWidth = width,
×
211
            _biHeight = height,
×
212
            _biBitCount = bpp,
×
213
            _biSizeImage = (uint) (width * Math.Abs(height) * (bpp >> 3)),
×
214
            _biXPelsPerMeter = 0,
×
215
            _biYPelsPerMeter = 0,
×
216
            _biClrUsed = 0,
×
217
            _biClrImportant = 0,
×
218
        };
×
219
    }
220

221
    /// <summary>
222
    ///     Check if this is a DIB V5
223
    /// </summary>
224
    public bool IsDibV4
225
    {
226
        get
227
        {
228
            var sizeOfBmi = (uint)Marshal.SizeOf(typeof(BitmapV4Header));
×
229
            return _biSize == sizeOfBmi;
×
230
        }
231
    }
232

233
    /// <summary>
234
    ///     Check if this is a DIB V5
235
    /// </summary>
236
    public bool IsDibV5
237
    {
238
        get
239
        {
240
            var sizeOfBmi = (uint) Marshal.SizeOf(typeof(BitmapV5Header));
×
241
            return _biSize >= sizeOfBmi;
×
242
        }
243
    }
244

245
    /// <summary>
246
    ///     Calculate the offset to the pixels
247
    /// </summary>
248
    public uint OffsetToPixels
249
    {
250
        get
251
        {
252
            if (_biCompression == BitmapCompressionMethods.BI_BITFIELDS)
×
253
            {
254
                // Add 3x4 bytes for the bitfield color mask
255
                return _biSize + 3 * 4;
×
256
            }
257
            return _biSize;
×
258
        }
259
    }
260
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc