C#-graphic-中在PictureBox上使用橡皮筋画线

本文介绍在WinForm项目中,如何在PictureBox控件上添加透明图层并绘制线段,解决背景颜色问题,实现高效绘图。文章详细解释了使用BufferedGraphics缓存技术的具体步骤和代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

想法是在原有图层上加上一个透明图层,在那上面画线段。

来获取两点位置和线段长度。

在winform项目中添加一个PictureBox控件,然后添加鼠标在PictureBox上的事件。

目前遇到了两个问题,
1.透明图层的添加,BufferedGraphics bg对象使用bg.Graphics.Clear(Color.Transparent);时背景为黑色,

如何设置透明色?

在网上看了许多,是用图像缓存技术来实现的。具体代码如下:

private void drawlines(Graphics g, Point mPoint1, Point mPoint2){
     BufferedGraphicsContext context = BufferedGraphicsManager.Current;//开辟空间
     BufferedGraphics bg = context.Allocate(g,new Rectangle(0,0,PictureBox1.Width,PictureBox1.Height));//开辟空间的大小
     bg.Graphics.Clear(Color.White);
     //在缓冲区上画线
     foreach (Line line in lines) {
         line.Draw(bg.Graphics);
     }
     //在缓存图层中画直线的样式,并没有实际显示,尽量与绘图的样式一致
     bg.Graphics.DrawLine(SystemPens.ControlText, mPoint1, mPoint2);
     bg.Render();
     bg.Dispose();
     bg = null;
}

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 橡皮技术3{
    public partial class Form2 : Form{
        //存放直线对象的集合
        List<Line> lines = new List<Line>();
        //起始点
        Point mStartPoint = Point.Empty;
        public Form2(){
            InitializeComponent();
        }

        //在图片对象上点击时
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e){
            if (e.Button == MouseButtons.Left)
            {
                //点击左键,存入起始点
                mStartPoint = e.Location;
            }
        }

        //在图片上移动时,绘制直线
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e){
            if (e.Button==MouseButtons.Left&& mStartPoint!=null){
                drawlines(pictureBox1.CreateGraphics(), mStartPoint, e.Location);//按住左键不放绘制
            }
        }

        //在松开左键时,存储点击,需要判定有无起始点,每次绘制结束后都要清空起始点
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e){
            if (!mStartPoint.IsEmpty){
                Line line = new Line(mStartPoint, e.Location);
                lines.Add(line);
                mStartPoint = Point.Empty;
            }
        }

        

        //通过paint绘制
        private void pictureBox1_Paint(object sender, PaintEventArgs e){
            foreach (Line line in lines){
                line.Draw(pictureBox1.CreateGraphics());
            }
        }


    }
}
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); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值