[英]How do i insert a Box which i drew out from pen to a picturebox image?
i need help on inserting a box which i drew out from into a picturebox. 我需要将画出的盒子插入画框的帮助。
here is the code of the pen that i code out, i do not know how to put it in the picturebox. 这是我写出的笔的代码,我不知道如何将其放在图片框中。 there will be a webcam running on the background of the picturebox, i want my rectangle to be inside the picturebox.
图片框的背景上将运行一个网络摄像头,我希望我的矩形位于图片框内。
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";
}
}
Paining in winforms is primarily done in the OnPaint event. Winforms中的痛苦主要是在OnPaint事件中完成的。 Your ButtonClick event handler should only setup the stage for OnPaint and possibly activate it.
您的ButtonClick事件处理程序应仅设置OnPaint的舞台并可能激活它。 Example:
例:
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);
}
}
}
You may have to draw the web cam image onto a bitmap buffer and use that as an image for the picture box. 您可能需要将网络摄像头图像绘制到位图缓冲区上,并将其用作图片框的图像。
Here is the msdn page with examples at the bottom: 这是msdn页面,底部带有示例:
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.aspx http://msdn.microsoft.com/zh-CN/library/system.windows.forms.picturebox.aspx
Here is my method for doing it. 这是我的方法。
public void GraphicsToPictureBox (ref PictureBox pb, Graphics graphics,
Int32 width, Int32 height)
{
Bitmap bitmap = new Bitmap(width,height,graphics);
pb.Image = bitmap;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.