Added BITMAPINFO.FromHBITMAP method and default static values for BITMAPINFO and DIBSECTION

pull/187/head
dahall 2020-12-07 18:50:45 -07:00
parent 87273d6edb
commit fd1d4de1c7
1 changed files with 177 additions and 76 deletions

View File

@ -14,19 +14,20 @@ namespace Vanara.PInvoke
BI_RGB = 0, BI_RGB = 0,
/// <summary> /// <summary>
/// A run-length encoded (RLE) format for bitmaps with 8 bpp. The compression format is a 2-byte format consisting of a count byte followed by a byte /// A run-length encoded (RLE) format for bitmaps with 8 bpp. The compression format is a 2-byte format consisting of a count
/// containing a color index. /// byte followed by a byte containing a color index.
/// </summary> /// </summary>
BI_RLE8 = 1, BI_RLE8 = 1,
/// <summary> /// <summary>
/// An RLE format for bitmaps with 4 bpp. The compression format is a 2-byte format consisting of a count byte followed by two word-length color indexes. /// An RLE format for bitmaps with 4 bpp. The compression format is a 2-byte format consisting of a count byte followed by two
/// word-length color indexes.
/// </summary> /// </summary>
BI_RLE4 = 2, BI_RLE4 = 2,
/// <summary> /// <summary>
/// Specifies that the bitmap is not compressed and that the color table consists of three DWORD color masks that specify the red, green, and blue /// Specifies that the bitmap is not compressed and that the color table consists of three DWORD color masks that specify the
/// components, respectively, of each pixel. This is valid when used with 16- and 32-bpp bitmaps. /// red, green, and blue components, respectively, of each pixel. This is valid when used with 16- and 32-bpp bitmaps.
/// </summary> /// </summary>
BI_BITFIELDS = 3, BI_BITFIELDS = 3,
@ -44,38 +45,50 @@ namespace Vanara.PInvoke
{ {
/// <summary>The bitmap type. This member must be zero.</summary> /// <summary>The bitmap type. This member must be zero.</summary>
public int bmType; public int bmType;
/// <summary>The width, in pixels, of the bitmap. The width must be greater than zero.</summary> /// <summary>The width, in pixels, of the bitmap. The width must be greater than zero.</summary>
public int bmWidth; public int bmWidth;
/// <summary>The height, in pixels, of the bitmap. The height must be greater than zero.</summary> /// <summary>The height, in pixels, of the bitmap. The height must be greater than zero.</summary>
public int bmHeight; public int bmHeight;
/// <summary> /// <summary>
/// The number of bytes in each scan line. This value must be divisible by 2, because the system assumes that the bit values of a bitmap form an /// The number of bytes in each scan line. This value must be divisible by 2, because the system assumes that the bit values of
/// array that is word aligned. /// a bitmap form an array that is word aligned.
/// </summary> /// </summary>
public int bmWidthBytes; public int bmWidthBytes;
/// <summary>The count of color planes.</summary> /// <summary>The count of color planes.</summary>
public ushort bmPlanes; public ushort bmPlanes;
/// <summary>The number of bits required to indicate the color of a pixel.</summary> /// <summary>The number of bits required to indicate the color of a pixel.</summary>
public ushort bmBitsPixel; public ushort bmBitsPixel;
/// <summary> /// <summary>
/// A pointer to the location of the bit values for the bitmap. The bmBits member must be a pointer to an array of character (1-byte) values. /// A pointer to the location of the bit values for the bitmap. The bmBits member must be a pointer to an array of character
/// (1-byte) values.
/// </summary> /// </summary>
public IntPtr bmBits; public IntPtr bmBits;
} }
/// <summary>The BITMAPINFO structure defines the dimensions and color information for a DIB.</summary> /// <summary>The BITMAPINFO structure defines the dimensions and color information for a DIB.</summary>
/// <remarks> /// <remarks>
/// A DIB consists of two distinct parts: a BITMAPINFO structure describing the dimensions and colors of the bitmap, and an array of bytes defining the /// A DIB consists of two distinct parts: a BITMAPINFO structure describing the dimensions and colors of the bitmap, and an array of
/// pixels of the bitmap. The bits in the array are packed together, but each scan line must be padded with zeros to end on a LONG data-type boundary. If /// bytes defining the pixels of the bitmap. The bits in the array are packed together, but each scan line must be padded with zeros
/// the height of the bitmap is positive, the bitmap is a bottom-up DIB and its origin is the lower-left corner. If the height is negative, the bitmap is /// to end on a LONG data-type boundary. If the height of the bitmap is positive, the bitmap is a bottom-up DIB and its origin is
/// a top-down DIB and its origin is the upper left corner. /// the lower-left corner. If the height is negative, the bitmap is a top-down DIB and its origin is the upper left corner.
/// <para> /// <para>
/// A bitmap is packed when the bitmap array immediately follows the BITMAPINFO header. Packed bitmaps are referenced by a single pointer. For packed /// A bitmap is packed when the bitmap array immediately follows the BITMAPINFO header. Packed bitmaps are referenced by a single
/// bitmaps, the biClrUsed member must be set to an even number when using the DIB_PAL_COLORS mode so that the DIB bitmap array starts on a DWORD boundary. /// pointer. For packed bitmaps, the biClrUsed member must be set to an even number when using the DIB_PAL_COLORS mode so that the
/// DIB bitmap array starts on a DWORD boundary.
/// </para> /// </para>
/// <para><c>Note</c></para> /// <para><c>Note</c></para>
/// <para>The bmiColors member should not contain palette indexes if the bitmap is to be stored in a file or transferred to another application.</para> /// <para>
/// <para>Unless the application has exclusive use and control of the bitmap, the bitmap color table should contain explicit RGB values.</para> /// The bmiColors member should not contain palette indexes if the bitmap is to be stored in a file or transferred to another application.
/// </para>
/// <para>
/// Unless the application has exclusive use and control of the bitmap, the bitmap color table should contain explicit RGB values.
/// </para>
/// </remarks> /// </remarks>
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
[PInvokeData("Wingdi.h", MSDNShortId = "dd183375")] [PInvokeData("Wingdi.h", MSDNShortId = "dd183375")]
@ -92,13 +105,16 @@ namespace Vanara.PInvoke
/// </item> /// </item>
/// <item> /// <item>
/// <description> /// <description>
/// An array of 16-bit unsigned integers that specifies indexes into the currently realized logical palette. This use of bmiColors is allowed for /// An array of 16-bit unsigned integers that specifies indexes into the currently realized logical palette. This use of
/// functions that use DIBs. When bmiColors elements contain indexes to a realized logical palette, they must also call the following bitmap /// bmiColors is allowed for functions that use DIBs. When bmiColors elements contain indexes to a realized logical palette,
/// they must also call the following bitmap
/// functions: CreateDIBitmap, CreateDIBPatternBrush, CreateDIBSection (The iUsage parameter of CreateDIBSection must be set to DIB_PAL_COLORS.) /// functions: CreateDIBitmap, CreateDIBPatternBrush, CreateDIBSection (The iUsage parameter of CreateDIBSection must be set to DIB_PAL_COLORS.)
/// </description> /// </description>
/// </item> /// </item>
/// </list> /// </list>
/// <para>The number of entries in the array depends on the values of the biBitCount and biClrUsed members of the BITMAPINFOHEADER structure.</para> /// <para>
/// The number of entries in the array depends on the values of the biBitCount and biClrUsed members of the BITMAPINFOHEADER structure.
/// </para>
/// <para>The colors in the bmiColors table appear in order of importance. For more information, see the Remarks section.</para> /// <para>The colors in the bmiColors table appear in order of importance. For more information, see the Remarks section.</para>
/// </summary> /// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
@ -113,6 +129,15 @@ namespace Vanara.PInvoke
{ {
bmiHeader = new BITMAPINFOHEADER(width, height, bitCount); bmiHeader = new BITMAPINFOHEADER(width, height, bitCount);
} }
/// <summary>Creates a <see cref="BITMAPINFO"/> structure from the information in a bitmap handle.</summary>
/// <param name="hBmp">The handle to a bitmap.</param>
/// <returns>
/// A structure with all the information the bitmap handle. If the specified bitmap is less than 24bpp, this resulting structure
/// must not be used without creating unmanaged memory to hold the structure and <c>BITMAPINFO.bmiHeader.biClrUsed *
/// sizeof(RGBQUAD)</c> extra bytes.
/// </returns>
public static BITMAPINFO FromHBITMAP(in HBITMAP hBmp) => new BITMAPINFO { bmiHeader = BITMAPINFOHEADER.FromHBITMAP(hBmp) };
} }
/// <summary>The BITMAPINFOHEADER structure contains information about the dimensions and color format of a DIB.</summary> /// <summary>The BITMAPINFOHEADER structure contains information about the dimensions and color format of a DIB.</summary>
@ -124,16 +149,21 @@ namespace Vanara.PInvoke
public int biSize; public int biSize;
/// <summary> /// <summary>
/// The width of the bitmap, in pixels. If biCompression is BI_JPEG or BI_PNG, the biWidth member specifies the width of the decompressed JPEG or PNG /// The width of the bitmap, in pixels. If biCompression is BI_JPEG or BI_PNG, the biWidth member specifies the width of the
/// image file, respectively. /// decompressed JPEG or PNG image file, respectively.
/// </summary> /// </summary>
public int biWidth; public int biWidth;
/// <summary> /// <summary>
/// The height of the bitmap, in pixels. If biHeight is positive, the bitmap is a bottom-up DIB and its origin is the lower-left corner. If biHeight /// The height of the bitmap, in pixels. If biHeight is positive, the bitmap is a bottom-up DIB and its origin is the lower-left
/// is negative, the bitmap is a top-down DIB and its origin is the upper-left corner. /// corner. If biHeight is negative, the bitmap is a top-down DIB and its origin is the upper-left corner.
/// <para>If biHeight is negative, indicating a top-down DIB, biCompression must be either BI_RGB or BI_BITFIELDS. Top-down DIBs cannot be compressed.</para> /// <para>
/// <para>If biCompression is BI_JPEG or BI_PNG, the biHeight member specifies the height of the decompressed JPEG or PNG image file, respectively.</para> /// If biHeight is negative, indicating a top-down DIB, biCompression must be either BI_RGB or BI_BITFIELDS. Top-down DIBs
/// cannot be compressed.
/// </para>
/// <para>
/// If biCompression is BI_JPEG or BI_PNG, the biHeight member specifies the height of the decompressed JPEG or PNG image file, respectively.
/// </para>
/// </summary> /// </summary>
public int biHeight; public int biHeight;
@ -141,8 +171,8 @@ namespace Vanara.PInvoke
public ushort biPlanes; public ushort biPlanes;
/// <summary> /// <summary>
/// The number of bits-per-pixel. The biBitCount member of the BITMAPINFOHEADER structure determines the number of bits that define each pixel and /// The number of bits-per-pixel. The biBitCount member of the BITMAPINFOHEADER structure determines the number of bits that
/// the maximum number of colors in the bitmap. This member must be one of the following values. /// define each pixel and the maximum number of colors in the bitmap. This member must be one of the following values.
/// <list type="table"> /// <list type="table">
/// <listheader> /// <listheader>
/// <term>Value</term> /// <term>Value</term>
@ -155,66 +185,72 @@ namespace Vanara.PInvoke
/// <item> /// <item>
/// <term>1</term> /// <term>1</term>
/// <description> /// <description>
/// The bitmap is monochrome, and the bmiColors member of BITMAPINFO contains two entries. Each bit in the bitmap array represents a pixel. If the /// The bitmap is monochrome, and the bmiColors member of BITMAPINFO contains two entries. Each bit in the bitmap array
/// bit is clear, the pixel is displayed with the color of the first entry in the bmiColors table; if the bit is set, the pixel has the color of the /// represents a pixel. If the bit is clear, the pixel is displayed with the color of the first entry in the bmiColors table; if
/// second entry in the table. /// the bit is set, the pixel has the color of the second entry in the table.
/// </description> /// </description>
/// </item> /// </item>
/// <item> /// <item>
/// <term>4</term> /// <term>4</term>
/// <description> /// <description>
/// The bitmap has a maximum of 16 colors, and the bmiColors member of BITMAPINFO contains up to 16 entries. Each pixel in the bitmap is represented /// The bitmap has a maximum of 16 colors, and the bmiColors member of BITMAPINFO contains up to 16 entries. Each pixel in the
/// by a 4-bit index into the color table. For example, if the first byte in the bitmap is 0x1F, the byte represents two pixels. The first pixel /// bitmap is represented by a 4-bit index into the color table. For example, if the first byte in the bitmap is 0x1F, the byte
/// contains the color in the second table entry, and the second pixel contains the color in the sixteenth table entry. /// represents two pixels. The first pixel contains the color in the second table entry, and the second pixel contains the color
/// in the sixteenth table entry.
/// </description> /// </description>
/// </item> /// </item>
/// <item> /// <item>
/// <term>8</term> /// <term>8</term>
/// <description> /// <description>
/// The bitmap has a maximum of 256 colors, and the bmiColors member of BITMAPINFO contains up to 256 entries. In this case, each byte in the array /// The bitmap has a maximum of 256 colors, and the bmiColors member of BITMAPINFO contains up to 256 entries. In this case,
/// represents a single pixel. /// each byte in the array represents a single pixel.
/// </description> /// </description>
/// </item> /// </item>
/// <item> /// <item>
/// <term>16</term> /// <term>16</term>
/// <description> /// <description>
/// The bitmap has a maximum of 2^16 colors. If the biCompression member of the BITMAPINFOHEADER is BI_RGB, the bmiColors member of BITMAPINFO is /// The bitmap has a maximum of 2^16 colors. If the biCompression member of the BITMAPINFOHEADER is BI_RGB, the bmiColors member
/// NULL. Each WORD in the bitmap array represents a single pixel. The relative intensities of red, green, and blue are represented with five bits /// of BITMAPINFO is NULL. Each WORD in the bitmap array represents a single pixel. The relative intensities of red, green, and
/// for each color component. The value for blue is in the least significant five bits, followed by five bits each for green and red. The most /// blue are represented with five bits for each color component. The value for blue is in the least significant five bits,
/// significant bit is not used. The bmiColors color table is used for optimizing colors used on palette-based devices, and must contain the number /// followed by five bits each for green and red. The most significant bit is not used. The bmiColors color table is used for
/// of entries specified by the biClrUsed member of the BITMAPINFOHEADER. /// optimizing colors used on palette-based devices, and must contain the number of entries specified by the biClrUsed member of
/// the BITMAPINFOHEADER.
/// <para> /// <para>
/// If the biCompression member of the BITMAPINFOHEADER is BI_BITFIELDS, the bmiColors member contains three DWORD color masks that specify the red, /// If the biCompression member of the BITMAPINFOHEADER is BI_BITFIELDS, the bmiColors member contains three DWORD color masks
/// green, and blue components, respectively, of each pixel. Each WORD in the bitmap array represents a single pixel. /// that specify the red, green, and blue components, respectively, of each pixel. Each WORD in the bitmap array represents a
/// single pixel.
/// </para> /// </para>
/// <para> /// <para>
/// When the biCompression member is BI_BITFIELDS, bits set in each DWORD mask must be contiguous and should not overlap the bits of another mask. /// When the biCompression member is BI_BITFIELDS, bits set in each DWORD mask must be contiguous and should not overlap the
/// All the bits in the pixel do not have to be used. /// bits of another mask. All the bits in the pixel do not have to be used.
/// </para> /// </para>
/// </description> /// </description>
/// </item> /// </item>
/// <item> /// <item>
/// <term>24</term> /// <term>24</term>
/// <description> /// <description>
/// The bitmap has a maximum of 2^24 colors, and the bmiColors member of BITMAPINFO is NULL. Each 3-byte triplet in the bitmap array represents the /// The bitmap has a maximum of 2^24 colors, and the bmiColors member of BITMAPINFO is NULL. Each 3-byte triplet in the bitmap
/// relative intensities of blue, green, and red, respectively, for a pixel. The bmiColors color table is used for optimizing colors used on /// array represents the relative intensities of blue, green, and red, respectively, for a pixel. The bmiColors color table is
/// palette-based devices, and must contain the number of entries specified by the biClrUsed member of the BITMAPINFOHEADER. /// used for optimizing colors used on palette-based devices, and must contain the number of entries specified by the biClrUsed
/// member of the BITMAPINFOHEADER.
/// </description> /// </description>
/// </item> /// </item>
/// <item> /// <item>
/// <term>32</term> /// <term>32</term>
/// <description> /// <description>
/// The bitmap has a maximum of 2^32 colors. If the biCompression member of the BITMAPINFOHEADER is BI_RGB, the bmiColors member of BITMAPINFO is /// The bitmap has a maximum of 2^32 colors. If the biCompression member of the BITMAPINFOHEADER is BI_RGB, the bmiColors member
/// NULL. Each DWORD in the bitmap array represents the relative intensities of blue, green, and red for a pixel. The value for blue is in the least /// of BITMAPINFO is NULL. Each DWORD in the bitmap array represents the relative intensities of blue, green, and red for a
/// significant 8 bits, followed by 8 bits each for green and red. The high byte in each DWORD is not used. The bmiColors color table is used for /// pixel. The value for blue is in the least significant 8 bits, followed by 8 bits each for green and red. The high byte in
/// optimizing colors used on palette-based devices, and must contain the number of entries specified by the biClrUsed member of the BITMAPINFOHEADER. /// each DWORD is not used. The bmiColors color table is used for optimizing colors used on palette-based devices, and must
/// contain the number of entries specified by the biClrUsed member of the BITMAPINFOHEADER.
/// <para> /// <para>
/// If the biCompression member of the BITMAPINFOHEADER is BI_BITFIELDS, the bmiColors member contains three DWORD color masks that specify the red, /// If the biCompression member of the BITMAPINFOHEADER is BI_BITFIELDS, the bmiColors member contains three DWORD color masks
/// green, and blue components, respectively, of each pixel. Each DWORD in the bitmap array represents a single pixel. /// that specify the red, green, and blue components, respectively, of each pixel. Each DWORD in the bitmap array represents a
/// single pixel.
/// </para> /// </para>
/// <para> /// <para>
/// When the biCompression member is BI_BITFIELDS, bits set in each DWORD mask must be contiguous and should not overlap the bits of another mask. /// When the biCompression member is BI_BITFIELDS, bits set in each DWORD mask must be contiguous and should not overlap the
/// All the bits in the pixel do not need to be used. /// bits of another mask. All the bits in the pixel do not need to be used.
/// </para> /// </para>
/// </description> /// </description>
/// </item> /// </item>
@ -226,14 +262,14 @@ namespace Vanara.PInvoke
public BitmapCompressionMode biCompression; public BitmapCompressionMode biCompression;
/// <summary> /// <summary>
/// The size, in bytes, of the image. This may be set to zero for BI_RGB bitmaps. If biCompression is BI_JPEG or BI_PNG, biSizeImage indicates the /// The size, in bytes, of the image. This may be set to zero for BI_RGB bitmaps. If biCompression is BI_JPEG or BI_PNG,
/// size of the JPEG or PNG image buffer, respectively. /// biSizeImage indicates the size of the JPEG or PNG image buffer, respectively.
/// </summary> /// </summary>
public uint biSizeImage; public uint biSizeImage;
/// <summary> /// <summary>
/// The horizontal resolution, in pixels-per-meter, of the target device for the bitmap. An application can use this value to select a bitmap from a /// The horizontal resolution, in pixels-per-meter, of the target device for the bitmap. An application can use this value to
/// resource group that best matches the characteristics of the current device. /// select a bitmap from a resource group that best matches the characteristics of the current device.
/// </summary> /// </summary>
public int biXPelsPerMeter; public int biXPelsPerMeter;
@ -241,21 +277,24 @@ namespace Vanara.PInvoke
public int biYPelsPerMeter; public int biYPelsPerMeter;
/// <summary> /// <summary>
/// The number of color indexes in the color table that are actually used by the bitmap. If this value is zero, the bitmap uses the maximum number of /// The number of color indexes in the color table that are actually used by the bitmap. If this value is zero, the bitmap uses
/// colors corresponding to the value of the biBitCount member for the compression mode specified by biCompression. /// the maximum number of colors corresponding to the value of the biBitCount member for the compression mode specified by biCompression.
/// <para> /// <para>
/// If biClrUsed is nonzero and the biBitCount member is less than 16, the biClrUsed member specifies the actual number of colors the graphics engine /// If biClrUsed is nonzero and the biBitCount member is less than 16, the biClrUsed member specifies the actual number of
/// or device driver accesses. If biBitCount is 16 or greater, the biClrUsed member specifies the size of the color table used to optimize /// colors the graphics engine or device driver accesses. If biBitCount is 16 or greater, the biClrUsed member specifies the
/// performance of the system color palettes. If biBitCount equals 16 or 32, the optimal color palette starts immediately following the three DWORD masks. /// size of the color table used to optimize performance of the system color palettes. If biBitCount equals 16 or 32, the
/// optimal color palette starts immediately following the three DWORD masks.
/// </para> /// </para>
/// <para> /// <para>
/// When the bitmap array immediately follows the BITMAPINFO structure, it is a packed bitmap. Packed bitmaps are referenced by a single pointer. /// When the bitmap array immediately follows the BITMAPINFO structure, it is a packed bitmap. Packed bitmaps are referenced by
/// Packed bitmaps require that the biClrUsed member must be either zero or the actual size of the color table. /// a single pointer. Packed bitmaps require that the biClrUsed member must be either zero or the actual size of the color table.
/// </para> /// </para>
/// </summary> /// </summary>
public uint biClrUsed; public uint biClrUsed;
/// <summary>The number of color indexes that are required for displaying the bitmap. If this value is zero, all colors are required.</summary> /// <summary>
/// The number of color indexes that are required for displaying the bitmap. If this value is zero, all colors are required.
/// </summary>
public uint biClrImportant; public uint biClrImportant;
/// <summary>Initializes a new instance of the <see cref="BITMAPINFOHEADER"/> structure.</summary> /// <summary>Initializes a new instance of the <see cref="BITMAPINFOHEADER"/> structure.</summary>
@ -273,47 +312,105 @@ namespace Vanara.PInvoke
biCompression = BitmapCompressionMode.BI_RGB; biCompression = BitmapCompressionMode.BI_RGB;
biSizeImage = 0; // (uint)width * (uint)height * bitCount / 8; biSizeImage = 0; // (uint)width * (uint)height * bitCount / 8;
} }
/// <summary>Creates a <see cref="BITMAPINFOHEADER"/> structure from the information in a bitmap handle.</summary>
/// <param name="hBmp">The handle to a bitmap.</param>
/// <returns>
/// A structure with all the information the bitmap handle. If the specified bitmap is less than 24bpp, the <see cref="biClrUsed"/> value is set to the
/// number of <see cref="RGBQUAD"/> structures that must be allocated.
/// </returns>
public static BITMAPINFOHEADER FromHBITMAP(in HBITMAP hBmp)
{
// Retrieve the bitmap color format, width, and height.
var bmp = GetObject<BITMAP>(hBmp);
// Convert the color format to a count of bits.
var cClrBits = (ushort)(bmp.bmPlanes * bmp.bmBitsPixel) switch
{
1 => 1,
<= 4 => 4,
<= 8 => 8,
<= 16 => 16,
<= 24 => 24,
_ => 32,
};
// Initialize the fields in the BITMAPINFO structure.
return new BITMAPINFOHEADER
{
biSize = Marshal.SizeOf(typeof(BITMAPINFOHEADER)),
biWidth = bmp.bmWidth,
biHeight = bmp.bmHeight,
biPlanes = bmp.bmPlanes,
biBitCount = bmp.bmBitsPixel,
// This value is correct, but there is no memory alllocated to hold these bits
biClrUsed = cClrBits < 24 ? 1U << cClrBits : 0,
// If the bitmap is not compressed, set the BI_RGB flag.
biCompression = BitmapCompressionMode.BI_RGB,
// Compute the number of bytes in the array of color indices and store the result in biSizeImage. The width must be uint
// aligned unless the bitmap is RLE compressed.
biSizeImage = (uint)(((bmp.bmWidth * cClrBits + 31) & ~31) / 8 * bmp.bmHeight),
// Set biClrImportant to 0, indicating that all of the device colors are important.
biClrImportant = 0
};
}
/// <summary>Gets the default value for this structure with size fields set appropriately.</summary>
public static readonly BITMAPINFOHEADER Default = new BITMAPINFOHEADER { biSize = Marshal.SizeOf(typeof(BITMAPINFOHEADER)) };
} }
/// <summary> /// <summary>
/// The DIBSECTION structure contains information about a DIB created by calling the CreateDIBSection function. A DIBSECTION structure includes /// The DIBSECTION structure contains information about a DIB created by calling the CreateDIBSection function. A DIBSECTION
/// information about the bitmap's dimensions, color format, color masks, optional file mapping object, and optional bit values storage offset. An /// structure includes information about the bitmap's dimensions, color format, color masks, optional file mapping object, and
/// application can obtain a filled-in DIBSECTION structure for a given DIB by calling the GetObject function. /// optional bit values storage offset. An application can obtain a filled-in DIBSECTION structure for a given DIB by calling the
/// GetObject function.
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
[PInvokeData("Wingdi.h", MSDNShortId = "dd183567")] [PInvokeData("Wingdi.h", MSDNShortId = "dd183567")]
public struct DIBSECTION public struct DIBSECTION
{ {
/// <summary> /// <summary>
/// A BITMAP data structure that contains information about the DIB: its type, its dimensions, its color capacities, and a pointer to its bit values. /// A BITMAP data structure that contains information about the DIB: its type, its dimensions, its color capacities, and a
/// pointer to its bit values.
/// </summary> /// </summary>
public BITMAP dsBm; public BITMAP dsBm;
/// <summary>A BITMAPINFOHEADER structure that contains information about the color format of the DIB.</summary> /// <summary>A BITMAPINFOHEADER structure that contains information about the color format of the DIB.</summary>
public BITMAPINFOHEADER dsBmih; public BITMAPINFOHEADER dsBmih;
private uint dsBitField1; private uint dsBitField1;
private uint dsBitField2; private uint dsBitField2;
private uint dsBitField3; private uint dsBitField3;
/// <summary> /// <summary>
/// The DSH sectionContains a handle to the file mapping object that the CreateDIBSection function used to create the DIB. If CreateDIBSection was /// The DSH sectionContains a handle to the file mapping object that the CreateDIBSection function used to create the DIB. If
/// called with a NULL value for its hSection parameter, causing the system to allocate memory for the bitmap, the dshSection member will be NULL. /// CreateDIBSection was called with a NULL value for its hSection parameter, causing the system to allocate memory for the
/// bitmap, the dshSection member will be NULL.
/// </summary> /// </summary>
public IntPtr dshSection; public IntPtr dshSection;
/// <summary> /// <summary>
/// The offset to the bitmap's bit values within the file mapping object referenced by dshSection. If dshSection is NULL, the dsOffset value has no meaning. /// The offset to the bitmap's bit values within the file mapping object referenced by dshSection. If dshSection is NULL, the
/// dsOffset value has no meaning.
/// </summary> /// </summary>
public uint dsOffset; public uint dsOffset;
/// <summary> /// <summary>
/// Specifies three color masks for the DIB. This field is only valid when the BitCount member of the BITMAPINFOHEADER structure has a value greater /// Specifies three color masks for the DIB. This field is only valid when the BitCount member of the BITMAPINFOHEADER structure
/// than 8. Each color mask indicates the bits that are used to encode one of the three color channels (red, green, and blue). /// has a value greater than 8. Each color mask indicates the bits that are used to encode one of the three color channels (red,
/// green, and blue).
/// </summary> /// </summary>
#pragma warning disable IDE1006 // Naming Styles #pragma warning disable IDE1006 // Naming Styles
public uint[] dsBitFields public uint[] dsBitFields
#pragma warning restore IDE1006 // Naming Styles #pragma warning restore IDE1006 // Naming Styles
{ {
get => new[] { dsBitField1, dsBitField2, dsBitField3 }; get => new[] { dsBitField1, dsBitField2, dsBitField3 };
set { dsBitField1 = value[0]; dsBitField2 = value[1]; dsBitField3 = value[2]; } set { dsBitField1 = value[0]; dsBitField2 = value[1]; dsBitField3 = value[2]; }
} }
/// <summary>Gets the default value for this structure with size fields set appropriately.</summary>
public static readonly DIBSECTION Default = new DIBSECTION { dsBmih = BITMAPINFOHEADER.Default };
} }
/// <summary>The RGBQUAD structure describes a color consisting of relative intensities of red, green, and blue.</summary> /// <summary>The RGBQUAD structure describes a color consisting of relative intensities of red, green, and blue.</summary>
@ -341,6 +438,10 @@ namespace Vanara.PInvoke
set { rgbReserved = value.A; rgbBlue = value.B; rgbGreen = value.G; rgbRed = value.R; } set { rgbReserved = value.A; rgbBlue = value.B; rgbGreen = value.G; rgbRed = value.R; }
} }
/// <summary>Gets a value indicating whether any transparency is defined.</summary>
/// <value><see langword="true"/> if this value is transparent; otherwise, <see langword="false"/>.</value>
public bool IsTransparent => rgbReserved == 0;
/// <summary>Performs an implicit conversion from <see cref="System.Drawing.Color"/> to <see cref="RGBQUAD"/>.</summary> /// <summary>Performs an implicit conversion from <see cref="System.Drawing.Color"/> to <see cref="RGBQUAD"/>.</summary>
/// <param name="c">The c.</param> /// <param name="c">The c.</param>
/// <returns>The result of the conversion.</returns> /// <returns>The result of the conversion.</returns>