15,979
社区成员
发帖
与我相关
我的任务
分享
CImage image1;
image1.Load(picpath);
CImage image2 = image1;
bool CGraphDlg::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;
}
VOID Example_Clone(HDC hdc)
{
Graphics graphics(hdc);
// Create an Image object, and then clone it.
Image image1(L"Crayons.jpg");
Image* pImage2 = image1.Clone();
// Draw the original image and the cloned image.
graphics.DrawImage(&image1, 20, 20);
graphics.DrawImage(pImage2, 250, 20);
delete pImage2;
}