2

作为主体。最好使用 C 代码。

4

5 回答 5

3

=========MFC++版本

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#版本

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;
    }
于 2010-08-22T17:15:54.473 回答
1

使用 ::GetDIBits,如#333559 所述

于 2009-03-26T13:14:46.787 回答
1

在那个答案上,不清楚如何处理 GetDiBits():

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

bits[3] == 左上角像素的 alpha;

bits[3] 是否应该针对零进行测试?如何处理该值?谢谢,

于 2009-03-26T13:22:42.993 回答
1

我已经实现了一个原型,但它不能正常工作,所以代码中可能有问题。我在这里分享它,希望我们可以一起修复它:

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;
}

谢谢!

于 2009-03-28T10:21:52.930 回答
0

使用GetDIBits(hdc, hbmp, 0, 1, NULL, &bmi, DIB_RGB_COLORS)

然后看看bmi.bmiHeader.biBitCount的值是否为32那么它有 alpha 通道,否则它没有 alpha。

于 2016-01-03T03:44:24.343 回答