gpt4 book ai didi

c++ - 如何在圆圈内绘制图像?

转载 作者:行者123 更新时间:2023-11-28 04:47:53 25 4
gpt4 key购买 nike

我有一个 MFC 应用程序,我需要在圆圈内绘制图像(或在圆圈内裁剪图像)。

我使用 FreeImage 加载图像,因此使用 FreeImage 或 MFC 函数的答案将有效。

这是我加载图像的方式:

void DrawImage(CDC *pDC, RECT *pRect, const BYTE *pBuffer, int nBufferLength)
{
FIMEMORY *pfmem = FreeImage_OpenMemory((BYTE*)pBuffer, nBufferLength);
FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(pfmem);
FIBITMAP *pdib = FreeImage_LoadFromMemory(fif, pfmem);

sz.cx = FreeImage_GetWidth(pdib);
sz.cy = FreeImage_GetHeight(pdib);

CRect rc(pRect);

BITMAPINFO *pbmpi = FreeImage_GetInfo(pdib);
BYTE *pDibBits = FreeImage_GetBits(pdib);

::SetDIBitsToDevice(pDC->GetSafeHdc(),
rc.left,
rc.top,
rc.Width(),
rc.Height(),
0,
0,
0,
rc.Height(),
pDibBits,
pbmpi,
DIB_RGB_COLORS);

FreeImage_Unload(pdib);
FreeImage_CloseMemory(pfmem);
}

最佳答案

发布我的答案以防有人遇到同样的问题,实际上很容易做到@Karthik Krish 写的:

//  Get the rect of a circle in the center o the image.
CRect rectMask(pRect);

int cx = rectMask.Width();
int cy = rectMask.Heigth();

if (cx > cy)
{
rectMask.left += (cx - cy) / 2;
rectMask.right = rectMask.left + cy;
}
else
{
rectMask.top += (cy - cx) / 2;
rectMask.bottom = rectMask.top + cx;
}

HRGN hRegiaoClip = NULL;

// Create a region for the circle
hRegiaoClip = ::CreateEllipticRgnIndirect(&rectMask);

// Select circle in the Device context.
::SelectClipRgn(pDC->GetSafeHdc(), hRegiaoClip);

// Call function to draw the image (the one in the question).
DrawImage(pDC, pRect, pImgBytes, pSerial->GetImageBufferSize(), TRUE);

// Clean up
::SelectClipRgn(pDC->GetSafeHdc(), NULL);
::DeleteObject(hRegiaoClip);

关于c++ - 如何在圆圈内绘制图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48893361/

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