转载:https://blog.csdn.net/xhlzjd/article/details/85230493
1 bool GetCircleImage(CString strSource, CString strTarget) 2 { 3 Image image(strSource); 4 Bitmap b(image.GetWidth(), image.GetHeight()); 5 Graphics g(&b); 6 g.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality); 7 GraphicsPath p(FillModeAlternate); 8 p.AddEllipse(0, 0, image.GetWidth(), image.GetHeight()); 9 g.FillPath(&TextureBrush(&image), &p); 10 11 //获取编码器的CLSID [class id] 12 UINT num = 0; //图像编码器的数量 13 UINT size = 0; //图像编码器数组的字节数 14 CLSID encoderClsid; 15 ImageCodecInfo* pICI = NULL; 16 17 GetImageEncodersSize(&num, &size);//获得系统编码器的数量和大小 18 if (size == 0) return false;//失败 19 20 pICI = (ImageCodecInfo*)(malloc(size)); //编码器 21 if (pICI == NULL) return false;//失败 22 23 GetImageEncoders(num, size, pICI); //获取编码器信息 24 for (UINT j = 0; j < num; ++j) 25 { 26 if (wcscmp(pICI[j].MimeType, L"image/png") == 0)//jpeg bmp gif tiff png 27 { 28 encoderClsid = pICI[j].Clsid; 29 } 30 } 31 free(pICI); 32 Status s = b.Save(strTarget, &encoderClsid);//只有png才能保存下来透明信息 33 return true; 34 }
下面是转换前后的对比图:
C++将方形的头像转换为圆形的头像
图1 转换前(方形)
C++将方形的头像转换为圆形的头像
图2 转换后(圆形)