2012-06-14 17 views
8

ich wollte nur eine Auswahl auf meine picturebox.image setzen, aber das ist nur schlimmer geworden als eine kleine nervige Situation. Ich dachte über eine andere Bilderbox über die Hauptbildbox nach, aber es schien mir so faul zu sein. Ich muss wissen, ob es eine Möglichkeit gibt, einen Auswahlbereich (welcher halbtransparent ist) auf einer Bildbox zu erstellen. Das Bild wird mit der Maus gezeichnet und sollte das Bild nicht verändern.So wählen Sie einen Bereich auf einer PictureBox.Image mit der Maus in C#

Beispiel:

// 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

Also möchten Sie eine Auswahlbox auf ein Bild in einer PictureBox erstellen? Funktioniert das Auswahlfeld genauso wie das Klicken und Ziehen auf dem Desktop, um ein transparentes blaues Quadrat zu erstellen? – 3aw5TZetdf

Antwort

30

ich Ihren Code verwendet, Sie waren fast da. Sie mussten die PictureBox1 anstelle des Rechtecks ​​invalidieren. Ich habe auch eine Überprüfung für das Rect hinzugefügt, damit es nicht gezeichnet wird, wenn es nicht initialisiert ist oder keine Größe hat.

Eine weitere wichtige Änderung: Ich habe das Rechteck nur einmal erstellt und seine Position und Größe angepasst. Weniger Müll zum Aufräumen!

EDIT

Ich habe eine Maus einen Rechtsklick-Handler für das Rechteck.

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

können Sie mir auch sagen, wie kann ich ein Rechtsklick-Ereignis auf dieser Auswahl erstellen? –

+1

BTW: In Ihrer Implementierung wird der Pinsel immer wieder neu erstellt. Versuche das zu verhindern. Ich werde meinen Code dafür anpassen. –

+0

jetzt scheint es wirklich weich und nützlich .. danke –

Verwandte Themen