2012-06-14 6 views
8

volevo solo mettere una selezione sul mio picturebox.image ma questo è appena diventato peggiore di una situazione poco fastidiosa. Ho pensato a un'altra casella di immagine sopra la finestra principale ma a me sembrava così laboriosa. Ho bisogno di sapere se c'è un modo per creare un'area di selezione (che sarà un'area blu trasparente a metà) su una picturebox.image che verrà disegnata con il mouse e non dovrebbe modificare l'immagine su cui sta lavorando.Come selezionare un'area su PictureBox.Image con il mouse in C#

campione:

// Start Rectangle 
    // 
    private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
    { 
     // Determine the initial rectangle coordinates... 
     RectStartPoint = e.Location; 
     Invalidate(); 
    } 

    // Draw Rectangle 
    // 
    private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) 
    { 
     if (e.Button != MouseButtons.Left) 
      return; 
     Point tempEndPoint = e.Location; 
     Rect = 
      new Rectangle(
       Math.Min(RectStartPoint.X, tempEndPoint.X), 
       Math.Min(RectStartPoint.Y, tempEndPoint.Y), 
       Math.Abs(RectStartPoint.X - tempEndPoint.X), 
       Math.Abs(RectStartPoint.Y - tempEndPoint.Y)); 
     Invalidate(Rect); 
    } 

    // Draw Area 
    // 
    private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 
    { 
     // Draw the rectangle... 
     if (pictureBox1.Image != null) 
     { 
      Brush brush = new SolidBrush(Color.FromArgb(128, 72, 145, 220)); 
      e.Graphics.FillRectangle(brush, Rect); 
     } 
    } 
+0

Quindi si desidera creare una casella di selezione su un'immagine in una pictureBox? La casella di selezione funzionerà come facendo clic e trascinando sul desktop per creare un quadrato blu trasparente? – 3aw5TZetdf

risposta

30

Ho usato il vostro codice, si erano quasi arrivati. È necessario invalidare PictureBox1 anziché il rettangolo. Ho anche aggiunto un controllo per Rect in modo che non venga disegnato quando non è inizializzato o non ha dimensioni.

Un altro cambiamento importante: ho creato il rettangolo solo una volta e ne ho regolato la posizione e le dimensioni. Meno rifiuti da pulire!

EDIT

ho aggiunto un clic destro del mouse gestore per il rettangolo.

private Point RectStartPoint; 
private Rectangle Rect = new Rectangle(); 
private Brush selectionBrush = new SolidBrush(Color.FromArgb(128, 72, 145, 220)); 

// Start Rectangle 
// 
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
{ 
    // Determine the initial rectangle coordinates... 
    RectStartPoint = e.Location; 
    Invalidate(); 
} 

// Draw Rectangle 
// 
private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) 
{ 
    if (e.Button != MouseButtons.Left) 
     return; 
    Point tempEndPoint = e.Location; 
    Rect.Location = new Point(
     Math.Min(RectStartPoint.X, tempEndPoint.X), 
     Math.Min(RectStartPoint.Y, tempEndPoint.Y)); 
    Rect.Size = new Size(
     Math.Abs(RectStartPoint.X - tempEndPoint.X), 
     Math.Abs(RectStartPoint.Y - tempEndPoint.Y)); 
    pictureBox1.Invalidate(); 
} 

// Draw Area 
// 
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 
{ 
    // Draw the rectangle... 
    if (pictureBox1.Image != null) 
    { 
     if (Rect != null && Rect.Width > 0 && Rect.Height > 0) 
     { 
      e.Graphics.FillRectangle(selectionBrush, Rect); 
     } 
    } 
} 

private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
{ 
    if (e.Button == MouseButtons.Right) 
    { 
     if (Rect.Contains(e.Location)) 
     { 
      Debug.WriteLine("Right click"); 
     } 
    } 
} 
+0

puoi anche dirmi come posso creare un evento click destro su quella selezione? –

+1

BTW: Nella tua implementazione il pennello viene creato ancora e ancora. Cerca di impedirlo. Adatterò anche il mio codice per quello. –

+0

ora sembra davvero morbido e utile .. grazie –

Problemi correlati