GetPixelAddress()函式Alpha通道會丟失

juk發表於2019-01-22

CImage類中GetPixelAddress()函式來設定獲取對應的顏色值是發現Alpha無效。

void CGBImage::Load(){
	CImage sourceImage;
	sourceImage.Load(L"C:\Users\Ju`k\Desktop\CGaussBlur\1.png");
	int bpp = sourceImage.GetBPP();
	int width = sourceImage.GetWidth();
	int height = sourceImage.GetHeight();

	CImage newImage;
	newImage.Create(width,height,32);
	for(int w = 0; w < width;w++){
		for(int h = 0; h < height;h++){
			if(w == 20 && h == 20){
				int xx = 0;
			}
			BYTE* buffer = (BYTE*)sourceImage.GetPixelAddress(w,h);
			BYTE b = buffer[0];
			BYTE g = buffer[1];
			BYTE r = buffer[2];
			BYTE a = buffer[3];


			BYTE* newBuffer = (BYTE*)newImage.GetPixelAddress(w,h);

			newBuffer[0] = b;
			newBuffer[1] = g;
			newBuffer[2] = r;
			newBuffer[3] = a;

		}
	}

	CLSID pngClsid;
	GetEncoderClsid(_T("image/png"), &pngClsid);
	newImage.Save(L"C:\Users\Ju`k\Desktop\CGaussBlur\1_1.png");


}

  

不管  newBuffer[3] = a; 如何設定 newImage始終無法透明。

這裡要注意的是

newImage.Create(width,height,32);

這裡第三個函式一定要設定為32

第四個函式這裡是預設的為0,應該設定為CImage::createAlphaChannel。

 

相關文章