我需要帮助才能插入一个我从画框中取出的盒子 这是我编码的笔的代码,我不知道如何把它放在图片框中。 在图片框的背景上会有一个网络摄像头,我希望我的矩形在图片框内。
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Start")
{
Graphics myGraphics = base.CreateGraphics();
myGraphics.Clear(Color.White);
Pen myPen = new Pen(Color.DarkBlue);
Rectangle rect = new Rectangle(480, 70, 120, 120);
myGraphics.DrawRectangle(myPen, rect);
stopWebcam = false;
button1.Text = "Stop";
}
else
{
stopWebcam = true;
button1.Text = "Start";
}
}
答案 0 :(得分:1)
winforms中的痛苦主要在OnPaint事件中完成。您的ButtonClick事件处理程序应该只为OnPaint设置阶段并可能激活它。例如:
public class MyForm : Form
...
private Rectangle? _boxRectangle;
private void OnMyButtonClick(object sender, EventArgs e)
{
if (button1.Text == "Start")
{
_boxRectangle = new Rectangle(...);
button1.Text = "Stop";
}
else
{
_boxRectangle = null;
button1.Text = "Start";
}
Invalidate(); // repaint
}
protected override OnPaint(PaintEventArgs e)
{
if (_boxRectangle != null)
{
Graphics g = e.Graphics.
Pen pen = new Pen(Color.DarkBlue);
g.DrawRectangle(_boxRectangle);
}
}
}
答案 1 :(得分:0)
您可能需要将网络摄像头图像绘制到位图缓冲区上,并将其用作图片框的图像。
这是msdn页面,底部是示例:
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.aspx
这是我的方法。
public void GraphicsToPictureBox (ref PictureBox pb, Graphics graphics,
Int32 width, Int32 height)
{
Bitmap bitmap = new Bitmap(width,height,graphics);
pb.Image = bitmap;
}