[英]Create rectangle drawing inside the picturebox
按鈕單擊事件后,我希望程序在圖片框內繪制一個矩形。 我不知道該怎么做。 到目前為止,我只在圖片框之外制作了一個矩形,就像這樣 。 之后,我想實現泛洪填充算法,以用一種顏色填充矩形。 這就是為什么我必須在那里有那些位圖的原因。
namespace howto_floodfill
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// The background image.
private Bitmap bm;
private Bitmap32 bm32;
// Make a rectangle
private void button1_Click(object sender, EventArgs e)
{
bm = new Bitmap(ClientSize.Width, ClientSize.Height);
using (Graphics gr = Graphics.FromImage(bm))
{
gr.Clear(Color.Silver);
gr.DrawRectangle(Pens.Black, 5, 5, 100, 60);
}
bm32 = new Bitmap32(bm);
this.BackgroundImage = bm;
}
}
}
您可以為PictureBox
設置Paint
事件處理程序,以允許您在PictureBox
內容的頂部進行繪制:
public partial class Form1 : Form
{
private bool _drawRectangle = false;
public Form1()
{
InitializeComponent();
pictureBox1.Paint += new PaintEventHandler(this.pictureBox1_Paint);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (_drawRectangle) {
Graphics g = e.Graphics;
// use g to do your drawing
}
}
}
只需在按鈕單擊處理程序中將_drawRectangle
設置為true
,然后調用pictureBox1.Invalidate()。 在Paint
事件處理程序中進行Paint
確保每當重新繪制PictureBox
(例如,因為另一個窗口在其上),矩形也會被繪制。
這將使您在PictureBox的內容上繪制一個矩形。 您是否考慮過僅使用FillRectangle而不是泛洪填充算法?
如果您確實要進行泛洪填充,則必須像以前一樣處理圖像,以便可以從圖像中讀取像素以了解是否已達到邊界。 然后,您可以使用生成的位圖設置PictureBox's
Image
屬性。 (在這種情況下,不必在Paint事件處理程序中完成圖形工作,因為PictureBox
將確保在每個繪畫上繪制其Image
。)
您可以使用從圖片框調用的control.CreateGraphics()方法。
namespace WindowsFormsApplication_Test
{
public partial class Form1 : Form
{
private Bitmap bm;
public Form1()
{
InitializeComponent();
bm = new Bitmap(pB.ClientRectangle.Width, pB.ClientRectangle.Height);
}
private void btn_Click(object sender, EventArgs e)
{
Graphics gF = pB.CreateGraphics();
gF.Clear(Color.Silver);
gF.DrawRectangle(Pens.Black, 5, 5, 100, 60);
}
private void button1_Click(object sender, EventArgs e)
{
Graphics gF = Graphics.FromImage(bm);
gF.Clear(Color.Silver);
gF.DrawRectangle(Pens.Black, 5, 5, 100, 60);
pB.Image = bm;
}
}
}
btn_click()是從控件直接獲取圖形上下文並直接繪制圖形的示例。
點擊之前的表格...
單擊該按鈕將直接在控件上繪制-請注意,默認情況下,它將為您提供控件的clientRectangle部分,這很不錯...
單擊按鈕即可獲得此信息。
現在,我調整了表格的大小。
不幸的是,以這種方式繪制表格使您有責任在窗口無效時重新繪制數據。
您也可以只將表單的Image屬性設置為您的私有位圖,然后框架為您處理重繪。 這在button1_Click()方法中顯示。
如果願意,您仍然可以直接在控件上繪制,但是您需要鈎住控件事件並在表單創建時重新繪制。 例如,將一個標志作為私有布爾添加到表單類,重寫OnPaint方法,並向按鈕事件添加代碼以交換該標志。 您不必翻轉按鈕事件中的繪圖代碼,而只需翻轉標記並調用this.Invalidate();。
public partial class Form1 : Form
{
private Bitmap bm;
private bool bDrawn = false;
public Form1()
{
InitializeComponent();
bm = new Bitmap(pB.ClientRectangle.Width, pB.ClientRectangle.Height);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (bDrawn)
{
Graphics gF = pB.CreateGraphics();
gF.Clear(Color.Silver);
gF.DrawRectangle(Pens.Black, 5, 5, 100, 60);
}
}
private void btn_Click(object sender, EventArgs e)
{
//Graphics gF = pB.CreateGraphics();
//gF.Clear(Color.Silver);
///gF.DrawRectangle(Pens.Black, 5, 5, 100, 60);
bDrawn = true;
this.Invalidate();
}
將創建的位圖分配給圖片框的Image屬性。 現在,您可以在位圖上繪制所需的任何內容,然后處理其數據。
namespace howto_floodfill {
public partial class Form1 : Form {
private Bitmap bitmap;
public Form1() {
InitializeComponent();
this.bitmap = new Bitmap(100, 100);
this.pictureBox1.Image = this.bitmap;
}
// Make a rectangle
private void button1_Click(object sender, EventArgs e) {
if (this.bitmap != null) {
this.bitmap.Dispose();
}
this.bitmap = new Bitmap(ClientSize.Width, ClientSize.Height);
using (Graphics gr = Graphics.FromImage(bitmap)) {
gr.Clear(Color.Silver);
gr.DrawRectangle(Pens.Black, 5, 5, 100, 60);
}
this.pictureBox1.Image = this.bitmap;
}
}
}
請注意,如果位圖具有固定大小,則可能不必每次都重新創建它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.