![](/img/trans.png)
[英]I did zoom in/out of image in a pictureBox but how do i avoid from the image to become distorted ?
[英]How do i insert a Box which i drew out from pen to a picturebox image?
我需要將畫出的盒子插入畫框的幫助。
這是我寫出的筆的代碼,我不知道如何將其放在圖片框中。 圖片框的背景上將運行一個網絡攝像頭,我希望我的矩形位於圖片框內。
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";
}
}
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);
}
}
}
您可能需要將網絡攝像頭圖像繪制到位圖緩沖區上,並將其用作圖片框的圖像。
這是msdn頁面,底部帶有示例:
http://msdn.microsoft.com/zh-CN/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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.