问题描述
我通过以下方式获取屏幕快照的字节数组,这是在ctypes中完成的,ctypes没有问题,如果我给它一个ctypes标签,那么ctypes会被人们弄糊涂,所以我简化了所有错误检查等,以向您显示该过程.
I am obtaining a byte array of a screenshot by the following, this is done in ctypes, there is no issues with the ctypes, if I gave this a ctypes tag the ctypes folks would be confused, so I simplified out all the error checking etc to show you the procedure.
CreateDC('DISPLAY', null, null, null);
nWidth = GetDeviceCaps(hdcScreen, HORZRES); // 1280 // numbers are always divisilbe by 4
nHeight = GetDeviceCaps(hdcScreen, ostypes.CONST.VERTRES); // 1024
nBPP = GetDeviceCaps(hdcScreen, BITSPIXEL); // 32
hdcMemoryDC = ostypes.API('CreateCompatibleDC')(hdcScreen);
bmi = BITMAPV5HEADER();
bmi.bV5Size = BITMAPV5HEADER.size;
bmi.bV5Width = nWidth;
bmi.bV5Height = -1 * nHeight; // top-down
bmi.bV5Planes = 1;
bmi.bV5BitCount = nBPP;
bmi.bV5Compression = BI_BITFIELDS;
bmi.bV5RedMask = 0xff;
bmi.bV5GreenMask = 0xff00;
bmi.bV5BlueMask = 0xff0000;
bmi.bV5AlphaMask = 0xff000000;
cBmi = ctypes.cast(bmi.address(), BITMAPINFO.ptr);
pixelBuffer = BYTE.ptr();
hbmp = CreateDIBSection(hdcScreen, cBmi, DIB_RGB_COLORS, pixelBuffer.address(), null, 0);
SelectObject(hdcMemoryDC, hbmp);
BitBlt(hdcMemoryDC, 0,0, nWidth, nHeight, hdcScreen, 0, 0, SRCCOPY);
byteArr = ctypes.cast(pixelBuffer, BYTE.array(arrLen).ptr).contents;
因此,使用上述标志,我得到了一个BGRA byteArr,它看起来像这样:
So with the above flags I get a BGRA byteArr which looks like this:
array(5242880)([240, 200, 105, 255, ..... ])
所以B是240,G是200,R是105,A是255.
So the B is 240, G is 200, R is 105, and A is 255.
我想将其转换为RGBA形式(出于ctypes性能的原因,在C端完成此操作很重要).
I want to get this into a RGBA form (it's important it's done on the C side for ctypes performance reasons).
因此,现在我们可以通过使用以下掩码将其成功变为RGBA形式,但是A变为0:
So now we can successfully get it into RGBA form HOWEVER the A becomes 0, by using these masks:
bmi.bV5RedMask = 0xff0000;
bmi.bV5GreenMask = 0xff00;
bmi.bV5BlueMask = 0xff;
bmi.bV5AlphaMask = 0xff000000; // we also tried 0x00000000
现在,这给了我们
array(5242880)([105, 200, 240, 0, ..... ])
所以问题是A值将变为0,如何保持RGBA格式,而将A值保持在255.
So the issue is the A value is going to 0, how can I keep this RGBA format, but keep the A value at 255.
推荐答案
与几乎所有GDI函数一样,BitBlt不会保留alpha值.
BitBlt, like nearly all GDI functions, doesn't preserve the alpha values.
如果您正在从屏幕上发条,则所有的Alpha值均为255-屏幕上没有透明孔.因此,您可以在blit之后自行设置Alpha.
If you're blitting from the screen, then all the alphas are 255--the screen doesn't have holes of transparency. So you can just set the alphas yourself after doing the blit.
这篇关于获得RGBA的BITMAPV5HEADER使A保持为255的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!