MFCにおけるCImageの簡単なコピー方法(Copy CImage)

1354 ワード

bool COriViewDlg::ImageCopy(const CImage &srcImage, CImage &destImage)
{
	int i,j;// 
	if(srcImage.IsNull())
		return FALSE;
	// 
	BYTE* srcPtr=(BYTE*)srcImage.GetBits();
	int srcBitsCount=srcImage.GetBPP();
	int srcWidth=srcImage.GetWidth();
	int srcHeight=srcImage.GetHeight();
	int srcPitch=srcImage.GetPitch(); 
	// 
	if( !destImage.IsNull())
	{
		destImage.Destroy();
	}
	// 
	if(srcBitsCount==32)   // alpha 
	{
		destImage.Create(srcWidth,srcHeight,srcBitsCount,1);
	}
	else
	{
		destImage.Create(srcWidth,srcHeight,srcBitsCount,0);
	}
	// 
	if(srcBitsCount<=8&&srcImage.IsIndexed())// 
	{
		RGBQUAD pal[256];
		int nColors=srcImage.GetMaxColorTableEntries();
		if(nColors>0)
		{     
			srcImage.GetColorTable(0,nColors,pal);
			destImage.SetColorTable(0,nColors,pal);// 
		}   
	} 
	// 
	BYTE *destPtr=(BYTE*)destImage.GetBits();
	int destPitch=destImage.GetPitch();
	// 
	for(i=0 ; i<srcHeight;i++)
	{
		memcpy( destPtr+i*destPitch, srcPtr+i*srcPitch, abs(srcPitch) );
	} 

	return TRUE;
} 

記事の出典:http://hi.baidu.com/yuxiangyunei/item/d9974213cae331ec9913d64f