如何用鼠标在C#中的图像上绘制矩形 [英] How to draw a rectangle on an image in c# from mouse

查看:815
本文介绍了如何用鼠标在C#中的图像上绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的问题是我正在从网络摄像头中获取视频流.我正在将其显示在图片框上.现在我希望用户单击四个位置,并希望使用这四个位置绘制一个矩形,即首先单击将是开始要点等等.
我是一个初学者,所以如果您觉得它太简单,请原谅.我可以使用硬编码的位置绘制一个矩形.以下是我在事件处理程序中具有新框架的代码.

钢笔= 钢笔(Color.Red, 2 );
图形g = Graphics.FromImage(bitmap);
g.DrawRectangle(pen, 30  30  60  30 );


但是如您所见,我已经对初始的x,y,宽度,高度进行了硬编码,我希望它们是用户定义的点击.

问候

首先,您不需要四个位置-它们不一定描述矩形,甚至不是平行四边形.两点定义一个矩形:TLHC和BRHC或TRHC和BLHC都足够.

确切地如何选择鼠标位置取决于您绘制位图的位置:如果位图位于表单表面,则只需处理Form.MouseDown或Form.MouseClick事件-它们都将鼠标坐标作为MouseEventArgs的一部分提供参数.


Hi
My question is that i am having video stream from my webcam.i am displaying it on my picture box.Now i want user to to click on four locations and i want a rectangle drawn on using those four locations i-e first click will be the starting point so on.
I am a beginner so please excuse if you feel its too simple.I can draw a rectangle using hard coded locations.Following is the code i have in my event handler that has the new frame.

Pen pen = new Pen(Color.Red, 2);
Graphics g = Graphics.FromImage(bitmap);
g.DrawRectangle(pen, 30, 30, 60, 30);


but as you see i have hard coded initial x,y,width,height i want them to be user defined clicks.

regards

解决方案

First off, you don''t want four locations - they will not necessarily describe a rectangle, or even a parallelogram. Two points define a rectangle: either TLHC and BRHC or TRHC and BLHC is sufficient.

Exactly how you pick up the mouse locations will depend on where you are drawing your bitmap: if it is on a form surface then just handle the Form.MouseDown or Form.MouseClick event - they both give your the mouse coordinates as part of the MouseEventArgs parameter.


这篇关于如何用鼠标在C#中的图像上绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆