How to programmatically detect if a bitmap has alpha channel?

我怕爱的太早我们不能终老 提交于 2019-12-11 03:32:05

问题


As subject. Preferibly using C code.


回答1:


=========MFC++ version

private: static Boolean __gc* BitmapHasAlpha(BitmapData __gc* bmpData)
{
    if ((bmpData->PixelFormat != PixelFormat::Format32bppArgb) && (bmpData->PixelFormat != PixelFormat::Format32bppRgb))
    {
        return false;
    }
    for (Int32 __gc* i = 0; (i < bmpData->Height); i++)
    {
        Int32 __gc* num2 = (i * bmpData->Stride);
        for (Int32 __gc* j = 3; (j < (bmpData->Width * 4)); j += 4)
        {
            Byte __gc** numPtr = *static_cast<__box Byte __gc***>(((bmpData->Scan0->ToPointer() + num2) + j));
            if (numPtr[0] != 0)
            {
                return true;
            }
        }
    }
    return false;
}

=========C# version

private static unsafe bool BitmapHasAlpha(BitmapData bmpData)
    {
        if ((bmpData.PixelFormat != PixelFormat.Format32bppArgb) && (bmpData.PixelFormat != PixelFormat.Format32bppRgb))
        {
            return false;
        }
        for (int i = 0; i < bmpData.Height; i++)
        {
            int num2 = i * bmpData.Stride;
            for (int j = 3; j < (bmpData.Width * 4); j += 4)
            {
                byte* numPtr = ((byte*)bmpData.Scan0.ToPointer()) + num2 + j;
                if (numPtr[0] != 0)
                {
                    return true;
                }
            }
        }
        return false;
    }



回答2:


With ::GetDIBits as described on #333559




回答3:


On that answer it is not clear what to do with GetDiBits():

GetDIBits( hDC, hBmp, 0, 1, (void**) &bits, &bmi, DIB_RGB_COLORS );

bits[3] == alpha of topleft pixel;

Should bits[3] be tested against zero ? what to do with that value ? thanks,




回答4:


I have implemented a prototype but it is not working fine, so there may be something wrong in the code. I share it here with the hope that we can fix it together:

BOOL HasAlphaChannel( HBITMAP hBmp )
{
   HDC hDC = CreateCompatibleDC( NULL );
   BITMAPINFO bmi; 
   void * bits;
   unsigned long ul; 
   BOOL bAlphaChannel = FALSE;

   memset( &bmi, 0, sizeof( BITMAPINFO ) ); 
   bmi.bmiHeader.biSize = sizeof( BITMAPINFOHEADER );  

   GetDIBits( hDC, hBmp, 0, 1, NULL, &bmi, DIB_RGB_COLORS ); 

   bits = malloc( bmi.bmiHeader.biSizeImage );

   // this is returning zero wich means error (why???) 
   GetDIBits( hDC, hBmp, 0, bmi.bmiHeader.biHeight, &bits, &bmi, DIB_RGB_COLORS ); 

   for( ul = 0; ul < bmi.bmiHeader.biSizeImage; ul += 4 )
      if( ( ( char * ) bits )[ ul + 3 ] != 0 )
         bAlphaChannel = TRUE;    

   free( bits );

   DeleteDC( hDC );

   return bAlphaChannel;
}

Thanks!




回答5:


Use GetDIBits(hdc, hbmp, 0, 1, NULL, &bmi, DIB_RGB_COLORS).

And then see if bmi.bmiHeader.biBitCount has value of 32 then it has alpha channel, otherwise it doesn't have alpha.



来源:https://stackoverflow.com/questions/685684/how-to-programmatically-detect-if-a-bitmap-has-alpha-channel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!