2011-05-09 20 views
5

Genişlik ve yükseklik ile çok sayıda görüntü ve koordinat var. Bir resim bir resim kutusu içine konur ve üzerine dikdörtgeni çizmek için koordinatları gönderirim. Panelde birçok resim kutusu var.C# resimdeki bir dikdörtgenin çiziminde mi?

Bir dikdörtgen çizmek için bazı koordinatlar ve genişlik/yükseklik özellikleriyle yollarını PicturePanel sınıfına gönderirim. Ancak, benim sorunum, onu çizer ve hemen siler. Her görüntüden sonra bir mesaj kutusu koymazsam, dikdörtgenleri göremiyorum. İşte kod;

if (IsRun()) 
{ 
    MessageBox.Show("rontool true"); 

    Rectangle ee = drawARectangle(xCoor, yCoor, MainScreen.tempR.wid/ratioOfx, MainScreen.tempR.heig/ratioOfy); // I wrote this, it only creates and returns the rectangle. 
    //MessageBox.Show("x : " + xCoor + " y: " + yCoor + " width : " + (MainScreen.tempR.wid/ratioOfx) + " height: " + (MainScreen.tempR.heig/ratioOfy)); 
    using (Pen pen = new Pen(Color.Red, 2)) 
    { 
     pictureBox.CreateGraphics().DrawRectangle(pen, ee); 
     // e.Graphics.DrawRectangle(pen, ee); 
    } 
} 

Bu döngü için ancak, çeker ve hemen siler y vs., başka bir sınıfta bir Picturebox oluşturur ve x başlatır

private void PictureBox_Paint(object sender, PaintEventArgs e). 

A bulunmaktadır. ya da bazen çizmiyor.

Her görüntüden sonra bir ileti kutusu koyarsam, dikdörtgenleri bile göremiyorum. Bana yardımcı olabilir misiniz?

cevap

3

resim kutusunu boya. Görünüşe göre dikdörtgeni sadece biraz çiziyorsun.

if (IsRun()) 

Her zaman çiziminizi yapmak için kodunuzu değiştirin. Örneğin, bu kod bir dikdörtgen çizmeyecektir. Ben'in örneği nerede.

private bool _once = true; 

     private void pictureBox1_Paint(object sender, PaintEventArgs e) 
     { 
      if (_once) 
      { 
       Rectangle ee = new Rectangle(10, 10, 30, 30); 
       using (Pen pen = new Pen(Color.Red, 2)) 
       { 
        e.Graphics.DrawRectangle(pen, ee); 
       } 
       _once = false; 
      } 
     } 
3

ben tamamen sorunuzu anlamak emin değilim ama tüm istediğiniz bir dikdörtgen çizmek için ise, aşağıdaki kod yapacağız:

, Windows sizi istediğinde picturebox boya yöntemi çağrılan
Private void pictureBox_Paint(object sender, PaintEventArgs e) { 
     Rectangle ee = new Rectangle(10, 10, 30, 30);   
     using (Pen pen = new Pen(Color.Red, 2)) { 
      e.Graphics.DrawRectangle(pen, ee); 
     } 
    } 
+0

Dikdörtgenlerin kaybolması için onu bir for döngüsü olarak adlandırıyorum. – Ada

+0

Sorunuzda yaptığınızdan daha fazla kod göstermeniz gerekecek. Daha spesifik olarak, bu döngüde neler oluyor. – Ben

0

Aşağıya bakın. Sadece kodu göstermek için bir resim yerine bir dikdörtgen ekledim:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     const int ROWS = 3; 
     const int COLUMNS = 4; 
     const int WIDTH = 10; 
     const int HEIGHT = 20; 
     const int SPACE = 10; 
     public Form1() 
     { 
      InitializeComponent(); 
      Panel panel = new Panel(); 
      panel.Width = COLUMNS * (WIDTH + SPACE); 
      panel.Height = ROWS * (HEIGHT + SPACE); 
      this.Controls.Add(panel); 
      for (int rows = 0; rows < ROWS; rows++) 
      { 
       for (int cols = 0; cols < COLUMNS; cols++) 
       { 
        PictureBox newPictureBox = new PictureBox(); 
        newPictureBox.Width = WIDTH; 
        newPictureBox.Height = HEIGHT; 
        newPictureBox.Top = rows * (HEIGHT + SPACE); 
        newPictureBox.Left = cols * (WIDTH + SPACE); 
        panel.Controls.Add(newPictureBox); 
        newPictureBox.Paint +=new PaintEventHandler(pictureBox_Paint); 

       } 
      } 
     } 
     private void pictureBox_Paint(object sender, PaintEventArgs e) { 
      Rectangle ee = new Rectangle(0, 0, WIDTH, HEIGHT);   
      using (Pen pen = new Pen(Color.Red, 2)) { 
       e.Graphics.DrawRectangle(pen, ee); 
      } 
     } 
    } 
} 
İlgili konular