2017-07-24 86 views
1

請看看下面的代碼:的CImage ::抽獎()畫一個黑色的畫面

// 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就夠了。

我的代碼有什麼問題?

回答

0

您在創建設備上下文之前選擇了resImage。因此繪製嘗試不會改變它。你應該重新排列像這樣的方法:

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