C# 允许用户通过 mouse/screen 次点击在 winform 上绘图
C# Allow user to draw on winform with mouse/screen clicks
我正在处理一个个人项目,我需要允许客户在新的弹出窗体上绘制一个 "signature",这是通过处理事件(可能是单击和鼠标悬停)事件实现的。
此签名必须存储在图像对象上,以便将其保存到数据库的 varbinary(max) 字段中。
谷歌搜索无效,知道如何实现吗?
我检查了我的触摸屏笔记本电脑,touchdown 事件可以通过 MouseDown 事件处理,touchup 通过 MouseUp 和 touchmove 通过 MouseMove 窗体事件.
注意:我的机器同时支持触摸和鼠标。我不确定仅触摸设备或机器。
以下代码允许您通过 touch/mouse 交互在表单上绘图。
public partial class Form1 : Form
{
Image signature;
bool clicked = false;
Point previousPoint;
public Form1()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Paint += Form1_Paint;
this.MouseDown += Form1_MouseDown;
this.MouseUp += Form1_MouseUp;
this.MouseMove += Form1_MouseMove;
this.MouseLeave += Form1_MouseLeave;
this.FormClosing += Form1_FormClosing;
}
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//Dispose signature after closing the form to avoid memory leak
signature.Dispose();
}
void Form1_Paint(object sender, PaintEventArgs e)
{
if (signature != null)
e.Graphics.DrawImage(signature, 0, 0);
}
void Form1_MouseDown(object sender, MouseEventArgs e)
{
clicked = true;
previousPoint = e.Location;
}
void Form1_MouseLeave(object sender, EventArgs e)
{
clicked = false;
}
void Form1_MouseUp(object sender, MouseEventArgs e)
{
clicked = false;
}
void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (clicked)
{
if (signature == null)
signature = new Bitmap(this.Width, this.Height);
using (Graphics g = Graphics.FromImage(signature))
{
g.DrawLine(Pens.Black, previousPoint, e.Location);
previousPoint = e.Location;
this.Invalidate();
}
}
}
}
签名绘制在图像上。所以您可以根据需要将图像保存在您的数据库中。
我正在处理一个个人项目,我需要允许客户在新的弹出窗体上绘制一个 "signature",这是通过处理事件(可能是单击和鼠标悬停)事件实现的。
此签名必须存储在图像对象上,以便将其保存到数据库的 varbinary(max) 字段中。
谷歌搜索无效,知道如何实现吗?
我检查了我的触摸屏笔记本电脑,touchdown 事件可以通过 MouseDown 事件处理,touchup 通过 MouseUp 和 touchmove 通过 MouseMove 窗体事件.
注意:我的机器同时支持触摸和鼠标。我不确定仅触摸设备或机器。
以下代码允许您通过 touch/mouse 交互在表单上绘图。
public partial class Form1 : Form
{
Image signature;
bool clicked = false;
Point previousPoint;
public Form1()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Paint += Form1_Paint;
this.MouseDown += Form1_MouseDown;
this.MouseUp += Form1_MouseUp;
this.MouseMove += Form1_MouseMove;
this.MouseLeave += Form1_MouseLeave;
this.FormClosing += Form1_FormClosing;
}
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//Dispose signature after closing the form to avoid memory leak
signature.Dispose();
}
void Form1_Paint(object sender, PaintEventArgs e)
{
if (signature != null)
e.Graphics.DrawImage(signature, 0, 0);
}
void Form1_MouseDown(object sender, MouseEventArgs e)
{
clicked = true;
previousPoint = e.Location;
}
void Form1_MouseLeave(object sender, EventArgs e)
{
clicked = false;
}
void Form1_MouseUp(object sender, MouseEventArgs e)
{
clicked = false;
}
void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (clicked)
{
if (signature == null)
signature = new Bitmap(this.Width, this.Height);
using (Graphics g = Graphics.FromImage(signature))
{
g.DrawLine(Pens.Black, previousPoint, e.Location);
previousPoint = e.Location;
this.Invalidate();
}
}
}
}
签名绘制在图像上。所以您可以根据需要将图像保存在您的数据库中。