private bool seleccionar = false;
private Rectangle RectanguloSeleccion;
private Point PuntoInicio;
private Point PuntoFin;
private int anchoSeleccion;
private int alturaSeleccion;
private void MouseDown_PictureBox(object sender, MouseEventArgs e)
{ this.Click_Raton(sender,e);
}
private void MouseUp_PictureBox(object sender, MouseEventArgs e)
{ this.Levantar_Raton(sender,e);
}
private void MouseMove_PictureBox(object sender, MouseEventArgs e)
{ this.Mover_Raton(sender,e);
}
private void Click_Raton(object sender,MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
seleccionar = true;
}
Control control = (Control)sender;
PuntoInicio = control.PointToScreen(new Point(e.X, e.Y));
}
private void Mover_Raton(object sender,MouseEventArgs e)
{
if (seleccionar)
{
ControlPaint.DrawReversibleFrame(RectanguloSeleccion,Color.Silver, FrameStyle.Dashed);
PuntoFin = ((Control)sender).PointToScreen(new Point(e.X, e.Y));
anchoSeleccion = PuntoFin.X - PuntoInicio.X;
alturaSeleccion = PuntoFin.Y - PuntoInicio.Y;
RectanguloSeleccion = new Rectangle(PuntoInicio.X, PuntoInicio.Y, anchoSeleccion, alturaSeleccion);
ControlPaint.DrawReversibleFrame(RectanguloSeleccion,Color.Silver, FrameStyle.Dashed);
}
}
private void Levantar_Raton(object sender, MouseEventArgs e)
{
seleccionar = false;
ControlPaint.DrawReversibleFrame(RectanguloSeleccion,Color.Blue, FrameStyle.Thick);
Rectangle r1 = this.pictureBoxPrincipal.RectangleToClient(RectanguloSeleccion);
Bitmap imagenPequeña = new Bitmap(anchoSeleccion, alturaSeleccion, this.pictureBoxPrincipal.Image.PixelFormat);
Rectangle rectDest = new Rectangle(0, 0, anchoSeleccion, alturaSeleccion);
Graphics g = Graphics.FromImage(imagenPequeña);
g.DrawImage(this.pictureBoxPrincipal.Image, rectDest, r1, GraphicsUnit.Pixel);
g.Dispose();
PictureBox newPB = new PictureBox();
newPB.Image = imagenPequeña;
newPB.BorderStyle = BorderStyle.Fixed3D;
this.pictureBoxPrincipal.Controls.Add(newPB);
newPB.Location = new System.Drawing.Point(r1.X, r1.Y);
newPB.Size = new System.Drawing.Size(anchoSeleccion, alturaSeleccion);
newPB.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Normal;
}