CreateDIBSection and BITMAPINFO with variable-length inline array #1082
                  
                    
                      harborsiem
                    
                  
                
                  started this conversation in
                Show and tell
              
            Replies: 3 comments 1 reply
-
| Here is an other unsafe implementation without using an additional struct BITMAPINFO256 HDC hDCScreen = PInvoke.GetDC(HWND.Null);
// Image
int quadsSize = mEncoder.Colors.Length * sizeof(RGBQUAD);
int biSize = quadsSize > 0 ? sizeof(BITMAPINFO) + sizeof(RGBQUAD) * (quadsSize - 1) : sizeof(BITMAPINFO);
byte* b = stackalloc byte[biSize];
BITMAPINFO* pbi = (BITMAPINFO*)b;
pbi->bmiHeader = mEncoder.Header;
pbi->bmiHeader.biHeight /= 2;
if (quadsSize > 0)
{
        RGBQUAD* bmiColorsPtr = &pbi->bmiColors._0;
        fixed (RGBQUAD* rgbQuadsLocal = mEncoder.Colors)
             //PInvoke.RtlMoveMemory(bmiColorsPtr, rgbQuadsLocal, (nint)quadsSize);
             Buffer.MemoryCopy(rgbQuadsLocal, bmiColorsPtr, (long)quadsSize, (long)quadsSize);
}
HDC hDCScreenOUTBmp = PInvoke.CreateCompatibleDC(hDCScreen);
void* bits;
HBITMAP hBitmapOUTBmp = PInvoke.CreateDIBSection(hDCScreenOUTBmp, pbi, DIB_USAGE.DIB_RGB_COLORS, &bits, HANDLE.Null, 0); | 
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            -
| Thank you for sharing. FYI #387 is related to this. | 
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            -
| Here is the implementation without using an additional struct BITMAPINFO256 that uses CsWin32 version >= 0.3.85-beta HDC hDCScreen = PInvoke.GetDC(HWND.Null);
// Image
//CsWin32 version >= 0.3.85-beta
int rgbQuadLength = mEncoder.Colors.Length;
byte* b = stackalloc byte[BITMAPINFO.SizeOf(rgbQuadLength)];
BITMAPINFO* pbi = (BITMAPINFO*)b;
pbi->bmiHeader = mEncoder.Header;
pbi->bmiHeader.biHeight /= 2;
if (rgbQuadLength > 0)
{
    mEncoder.Colors.CopyTo(pbi->bmiColors.AsSpan(rgbQuadLength));
}
HDC hDCScreenOUTBmp = PInvoke.CreateCompatibleDC(hDCScreen);
void* bits;
HBITMAP hBitmapOUTBmp = PInvoke.CreateDIBSection(hDCScreenOUTBmp, pbi, DIB_USAGE.DIB_RGB_COLORS, &bits, HANDLE.Null, 0); | 
Beta Was this translation helpful? Give feedback.
                  
                    1 reply
                  
                
            
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
MultiIconLib.zip
In some closed issues about BITMAPINFO I did not found any usable information how to use the actual generated BITMAPINFO for the bmiColors field. The bmiColors has just a size of 1 RGBQUAD. But for my implementation I need up to 256 RGBQUADs. So I developed a solution for this problem. First I build a new struct called BITMAPINFO256 (see attached file). And her is an example how to use with a BITMAPINFO256:
Beta Was this translation helpful? Give feedback.
All reactions