CImage :: Draw()绘制一张黑色图片

时间:2017-07-24 16:39:45

标签: c++ gdi

请查看以下代码:

// Create an empty bitmap BRGA
CImage segImage;
HDC hSeg = CreateCompatibleDC(NULL);
SelectObject(hSeg, segImage);

int width = 640;
int height = 480;
segImage.Create(width, height, 32, CImage::createAlphaChannel);

// Fill it from a byte array (the size is height * width * 4)
int lineSize = width * 4;
for (int y = 0; y < height; y++)
{
    void*       dst = segImage.GetPixelAddress(0, y);
    const void* src = segmented_image_data.planes[0] + y * segmented_image_data.pitches[0];
    memcpy(dst, src, lineSize);
}

// inspecting segImage shows the actual image content

// create the destination image (resized)
CImage resImage;
HDC hRes = CreateCompatibleDC(NULL);
SelectObject(hRes, resImage);

int resWidth = 320;
int resHeight = 240;

resImage.Create(resWidth, resHeight, 32, CImage::createAlphaChannel);
segImage.Draw(hRes, 0, 0, resWidth, resHeight);

// inspecting resImage shows only 0x00

segImage.Destroy();
resImage.Destroy();

我知道内存DC默认创建monocrome位图,但我认为使用Create()方法传递32-bpp就足够了。

我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

您在创建设备上下文之前选择resImage。因此,绘制尝试不会改变它。你应该重新排序这样的方法:

resImage.Create(resWidth, resHeight, 32, CImage::createAlphaChannel);
SelectObject(hRes, resImage);