为什么bmp被创建为不透明?

时间:2017-04-24 11:36:04

标签: c++ windows clipboard

我有以下代码创建透明的bmp图像并将其保存到剪贴板。在此之后,我尝试将其复制到Photoshop或办公室,Gimp。但图像并不透明:

#include <Windows.h>
#include <iostream>
#include <Wingdi.h>
#include <algorithm>

using namespace std;

pair<int, HGLOBAL> dibv5_img(void)
{
    const DWORD width = 100;
    const DWORD height = 100;
    BITMAPV5HEADER header = {0};
    header.bV5Size = sizeof(BITMAPV5HEADER);
    header.bV5Width = width;
    header.bV5Height = -height;
    header.bV5Planes = 1;
    header.bV5BitCount = 32;
    header.bV5Compression = BI_BITFIELDS;
    header.bV5SizeImage = header.bV5Width * header.bV5Height *     header.bV5BitCount / 8;
    header.bV5CSType = LCS_WINDOWS_COLOR_SPACE;
    header.bV5Intent = LCS_GM_IMAGES;

    header.bV5BlueMask = 0x000000FF;
    header.bV5GreenMask = 0x0000FF00;
    header.bV5RedMask = 0x00FF0000;
    header.bV5AlphaMask = 0xFF000000;

    DWORD masks[3];
    masks[0] = header.bV5BlueMask;
    masks[1] = header.bV5GreenMask;
    masks[2] = header.bV5AlphaMask;

    DWORD pixels[width * height];
    for (DWORD i = 0; i < height; ++i)
        for (DWORD j = 0; j < width; ++j)
            pixels[i * width + j] = 0;
    for (DWORD i = height / 4; i < 3 * height / 4; ++i)
        for (DWORD j = 0; j < width; ++j)
            pixels[i * width + j] = (masks[0] | header.bV5AlphaMask) &     0xFFFFFFFF;

    HGLOBAL hmem = GlobalAlloc(GHND | GMEM_DDESHARE, header.bV5Size +     sizeof(DWORD) * (3 + width * height));
    PVOID local_pointer = GlobalLock(hmem);

    memcpy(local_pointer, &header, sizeof(BITMAPV5HEADER));
    memcpy(reinterpret_cast<unsigned char*>(local_pointer) +     sizeof(BITMAPV5HEADER), masks, sizeof(DWORD) * 3);
    memcpy(reinterpret_cast<unsigned char*>(local_pointer) +     sizeof(BITMAPV5HEADER) + sizeof(DWORD) * 3, pixels, 
        sizeof(DWORD) * width * height);
    GlobalUnlock(hmem);
    return pair<int, HGLOBAL>(CF_DIBV5, hmem);
}

int main(void)
{
    cout << sizeof(DWORD) << endl;
    if (!OpenClipboard(0))
        return 1;
    if (!EmptyClipboard())
        return 1;
    const auto data = dibv5_img();
    SetClipboardData(data.first, data.second);
    if (!CloseClipboard())
        return 1;
    return 0;
}

所有应用中的输出如下:

enter image description here

0 个答案:

没有答案