c#绘制橡皮线

这篇博客分享了如何在C#中实现橡皮线的绘制,通过一个开源类库`Win32XORPenDrawer`进行交互式线条效果。代码示例展示了在鼠标移动和点击事件中如何使用该类进行画线操作,并提供了完整类的实现。
摘要由CSDN通过智能技术生成

<script type="text/JavaScript"> </script> <script src="http://a.alimama.cn/inf.js" type="text/javascript"></script>

在我们日常编程中经常会用到“橡皮线”,橡皮线在VC里面实现起来比较容易些。但是在c#中就比较麻烦些。下面是我以前工作中发现的一个开源类,然后加以改造,使用比较方便。现共享CSDN网友!

先给个图:


绘制代码:

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace DoubleBufferDraw { public partial class DrawLine : Form { class LineObj { private Point m_start; private Point m_end; public LineObj(Point start, Point end) { this.m_start = start; this.m_end = end; } public void Draw(Graphics g, Pen pen) { g.DrawLine(pen, m_start, m_end); } } private Point m_startPoint = Point.Empty; List lineList = new List(); public DrawLine() { InitializeComponent(); } private void drawLine(Graphics graphics, Point startPoint, Point endPoint) { BufferedGraphicsContext context = BufferedGraphicsManager.Current; BufferedGraphics bg = context.Allocate(graphics, this.ClientRectangle); bg.Graphics.Clear(this.BackColor); foreach (LineObj line in this.lineList) { line.Draw(bg.Graphics, SystemPens.ControlText); } bg.Graphics.DrawLine(SystemPens.ControlText, startPoint, endPoint); bg.Render(); bg.Dispose(); bg = null; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); foreach (LineObj line in this.lineList) { line.Draw(e.Graphics, SystemPens.ControlText); } } protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); this.m_startPoint = new Point(e.X, e.Y); } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (e.Button == MouseButtons.Left) { this.drawLine(this.CreateGraphics(), this.m_startPoint, new Point(e.X, e.Y)); } } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); LineObj line = new LineObj(this.m_startPoint, e.Location); this.lineList.Add(line); } } }
以下是使用C#和VS2005绘制坐标曲线的示例代码: 1. 在窗体中添加一个Panel控件,用于绘制坐标曲线。 2. 在窗体的Load事件中添加以下代码: ```csharp private void Form1_Load(object sender, EventArgs e) { // 设置Panel控件的背景颜色和边框样式 panel1.BackColor = Color.White; panel1.BorderStyle = BorderStyle.FixedSingle; } ``` 3. 在Panel控件的Paint事件中添加以下代码: ```csharp private void panel1_Paint(object sender, PaintEventArgs e) { // 创建Graphics对象 Graphics g = e.Graphics; // 设置画笔的颜色和宽度 Pen pen = new Pen(Color.Black, 2); // 绘制坐标轴 g.DrawLine(pen, 50, 250, 450, 250); // x轴 g.DrawLine(pen, 50, 250, 50, 50); // y轴 // 绘制坐标轴刻度和标签 Font font = new Font("宋体", 10); Brush brush = new SolidBrush(Color.Black); for (int i = 0; i <= 10; i++) { g.DrawLine(pen, 50 + i * 40, 250, 50 + i * 40, 245); // x轴刻度 g.DrawString((i * 10).ToString(), font, brush, 45 + i * 40, 255); // x轴标签 g.DrawLine(pen, 50, 250 - i * 20, 55, 250 - i * 20); // y轴刻度 g.DrawString((i * 5).ToString(), font, brush, 25, 245 - i * 20); // y轴标签 } // 绘制坐标曲线 pen.Color = Color.Red; Point[] points = new Point[11]; for (int i = 0; i <= 10; i++) { int x = 50 + i * 40; int y = 250 - i * i; points[i] = new Point(x, y); } g.DrawCurve(pen, points); } ``` 4. 运行程序,就可以看到绘制的坐标曲线了。 注意:以上代码仅为示例,实际应用中需要根据具体需求进行修改和优化。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值