PixelFormat.Format32bppArgb seems to have wrong byte order

前端 未结 3 1030
天命终不由人
天命终不由人 2020-12-14 07:17

I try to get all byte values from a Bitmap(System.Drawing.Bitmap). Therefore I lock the bytes and copy them:

public static byte[] GetPixels(Bitmap bitmap){
          


        
相关标签:
3条回答
  • 2020-12-14 07:42

    Pixel data is ARGB, 1 byte for alpha, 1 for red, 1 for green, 1 for blue. Alpha is the most significant byte, blue is the least significant. On a little-endian machine, like yours and many others, the little end is stored first so the byte order is bb gg rr aa. So 0 0 255 255 equals blue = 0, green = 0, red = 255, alpha = 255. That's red.

    This endian-ness order detail disappears when you cast bd.Scan0 to an int* (pointer-to-integer) since integers are stored little-endian as well.

    0 讨论(0)
  • 2020-12-14 07:49

    AFAIK it is technically based on COLORREF (which is used in Windows GDI/GDI+ everywhere) and that is stored RGBA in memory... see http://msdn.microsoft.com/en-us/library/dd183449%28VS.85%29.aspx

    0 讨论(0)
  • 2020-12-14 07:59

    In Bpp32Argb pixel format. You dont' need to byte-by-byte access.

    Trun Scan0 to an Int32 pointer in unsafe context.

    unsafe
    {
        var ptr=(int*)bmData.Scan0;
    }
    

    You can do some bit operation like below To access color channels of first pixel.

    And dont need to care byte-order.

    var a=(ptr[0] & 0xFF000000)>>24;
    var r=(ptr[0] & 0x00FF0000)>>16;
    var g=(ptr[0] & 0x0000FF00)>>8;
    var b=(ptr[0] & 0x000000FF);
    

    BTW you can work with Color.ToArgb() returned int easily.

    0 讨论(0)
提交回复
热议问题