2012-06-14 35 views
8

solo quería poner una selección en mi picturebox.image pero esto ha empeorado más que una pequeña situación molesta. Pensé en otro cuadro de imagen sobre el recuadro principal, pero me pareció un trabajo tan vago. Necesito saber si hay una manera de crear un área de selección (que será el área azul medio transparente) en un picturebox.image que voy a dibujar con el mouse y no debería cambiar la imagen en la que estoy trabajando.Cómo seleccionar un área en una PictureBox.Imagen con el mouse en C#

muestra:

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

¿Desea crear un cuadro de selección en una imagen en un cuadro de imagen? ¿Actuará el cuadro de selección lo mismo que hacer clic y arrastrar en el escritorio para crear un cuadrado azul transparente? – 3aw5TZetdf

Respuesta

30

he utilizado el código, estaban cerca de allí. Necesitabas invalidar el pictureBox1 en lugar del rectángulo. También agregué un cheque para el Rect para que no se dibuje cuando no está inicializado o no tiene tamaño.

Otro cambio importante: Creé el Rectángulo una sola vez y ajusté su ubicación y tamaño. ¡Menos basura para limpiar!

EDITAR

he añadido un botón derecho del ratón controlador para el rectángulo.

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

también me puede decir cómo puedo crear un evento de clic derecho en esa selección? –

+1

BTW: en su implementación, el pincel se crea una y otra vez. Intenta prevenir eso. Voy a ajustar mi código para eso también. –

+0

ahora parece muy suave y útil ... gracias –

Cuestiones relacionadas