gpt4 book ai didi

c++ - CImage::Draw() 绘制黑色图片

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:43:11 27 4
gpt4 key购买 nike

请看下面的代码:

// 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 创建单色位图,但我认为使用传递 32-bpp 的 Create() 方法就足够了。

我的代码有什么问题?

最佳答案

在创建之前将 resImage 选择到设备上下文中。因此平局尝试不会改变它。您应该像这样重新排序方法:

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

关于c++ - CImage::Draw() 绘制黑色图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45285823/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com