C# GraphicsPath相交区域边框绘制实现

 

                GraphicsPath path1 ;
                GraphicsPath path2 ;

                Region region1 = new Region(path1);
                Region region2 = new Region(path2);
                Pen p = new Pen(Color.Red, 5);
                path1.Widen(p);
                path2.Widen(p);
                Region region3 = new Region(path1);
                Region region4 = new Region(path2);
                Region region5 = region1.Clone();

                region1.Intersect(region2);
                region1.Intersect(region3);

                region2.Intersect(region4);
                region2.Intersect(region5);

                g.FillRegion(Brushes.Red, region2);
                g.FillRegion(Brushes.Green, region1);

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当然,这里是一个示例代码,可以在WinForms中使用GraphicsPath在PictureBox上绘制图形,并使用鼠标滚轮实现图形的缩放显示。 首先,你需要在窗体中添加一个PictureBox控件,并将其大小设置为适当的值。然后在PictureBox的MouseWheel事件中编写代码来处理鼠标滚轮事件。 以下是示例代码: ```csharp using System; using System.Drawing; using System.Windows.Forms; namespace GraphicsPathExample { public partial class Form1 : Form { private GraphicsPath graphicsPath; private float scaleFactor = 1.0f; public Form1() { InitializeComponent(); // 创建一个GraphicsPath对象,并添加一些图形路径 graphicsPath = new GraphicsPath(); graphicsPath.AddEllipse(50, 50, 200, 100); graphicsPath.AddRectangle(new Rectangle(100, 150, 150, 100)); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); // 创建一个缩放变换矩阵 Matrix matrix = new Matrix(); matrix.Scale(scaleFactor, scaleFactor); // 在PictureBox上绘制图形路径 e.Graphics.Transform = matrix; e.Graphics.DrawPath(Pens.Black, graphicsPath); } private void pictureBox1_MouseWheel(object sender, MouseEventArgs e) { // 计算缩放因子 float delta = e.Delta / 1200.0f; scaleFactor += delta; // 限制缩放因子的范围 if (scaleFactor < 0.1f) scaleFactor = 0.1f; if (scaleFactor > 5.0f) scaleFactor = 5.0f; // 重新绘制PictureBox pictureBox1.Invalidate(); } } } ``` 在这个示例中,我们创建了一个GraphicsPath对象,并添加了一个椭圆和一个矩形的路径。在窗体的OnPaint事件中,我们使用缩放变换矩阵将图形路径按照缩放因子进行缩放,并在PictureBox上绘制出来。 在PictureBox的MouseWheel事件中,我们根据鼠标滚轮的滚动值来计算缩放因子,并限制缩放因子的范围在0.1到5之间。然后,我们调用PictureBox的Invalidate方法来重新绘制PictureBox,从而实现图形的缩放显示。 希望这个示例代码能帮到你!如果有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值