GraphicsPath.AddArc 方法 (Int32, Int32, Int32, Int32, Single, Single)

4个int分别表示矩形的左上角X,Y坐标,矩形的宽和高,C#里面画的椭圆的大小是用矩形来定义的,你定义矩形后,绘制的就是矩形的内切椭圆,后面两个为起始角度和终止角度与起始角度的夹角。


1
2
3
4
5
6
7
8
9
10
11
         private  void  Form1_Paint( object  sender, PaintEventArgs e)
 
         {
 
             GraphicsPath path =  new  GraphicsPath();
 
             path.AddArc(0, 0, 100, 200, 10, 270);
 
             e.Graphics.DrawPath( new  Pen(Color.Black,1),path);
 
         }



上面的的是一个左上角在(0,0)处,宽和高分别为100,200px的矩形,更改(0,0)就可以在其他地方绘制。

1
2
3
4
5
6
7
8
9
10
11
         private  void  Form1_Paint( object  sender, PaintEventArgs e)
 
         {
 
             GraphicsPath path =  new  GraphicsPath();
 
             path.AddArc(100, 100, 100, 200, 10, 270);
 
             e.Graphics.DrawPath( new  Pen(Color.Black,1),path);
 
         }




从椭圆的10度位置开始画,然后画到280度的位置,椭圆其他的部分不画出来

要使用Gdiplus实现裁剪圆形头像,可以按照以下步骤进行: 1. 加载图片 首先,需要使用Gdiplus中的Image类加载一张图片,例如: ```c Image* pImage = new Image(L"avatar.png"); ``` 2. 创建设备上下文 接着,需要创建一个设备上下文,用于后面的绘制,例如: ```c HDC hdc = GetDC(hWnd); Graphics* pGraphics = new Graphics(hdc); ``` 其中,hWnd是窗口的句柄。 3. 创建裁剪区域 创建一个圆形的裁剪区域,可以使用Gdiplus中的GraphicsPath类和Region类,例如: ```c GraphicsPath path; path.AddEllipse(x, y, width, height); Region region(&path); pGraphics->SetClip(&region); ``` 其中,x、y、width、height分别表示圆形的位置和大小。 4. 绘制头像 最后,使用Gdiplus中的DrawImage函数绘制头像,例如: ```c pGraphics->DrawImage(pImage, x, y, width, height); ``` 其中,x、y、width、height分别表示头像的位置和大小。 5. 释放资源 绘制完成后,需要释放之前创建的资源,例如: ```c delete pImage; delete pGraphics; ReleaseDC(hWnd, hdc); ``` 完整的代码示例: ```c void DrawAvatar(Graphics* pGraphics, LPCWSTR lpszFileName, int x, int y, int width, int height) { Image* pImage = new Image(lpszFileName); HDC hdc = pGraphics->GetHDC(); GraphicsPath path; path.AddEllipse(x, y, width, height); Region region(&path); pGraphics->SetClip(&region); pGraphics->DrawImage(pImage, x, y, width, height); pGraphics->ReleaseHDC(hdc); delete pImage; } void OnPaint(HWND hWnd) { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); Graphics* pGraphics = new Graphics(hdc); DrawAvatar(pGraphics, L"avatar.png", 50, 50, 100, 100); delete pGraphics; EndPaint(hWnd, &ps); } ``` 注意,Gdiplus需要在程序开始时初始化,在程序结束时释放。可以使用以下代码进行初始化和释放: ```c GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); // ... GdiplusShutdown(gdiplusToken); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值