I have a image that is larger than the PictureEdit box issue is when the Rectangle is created with the mouse the correct points on the image are not selected. It seems that it is selecting points in the PictureEdit not the image it self. I’m trying to crop the image and place the cropped image in a new Pictureedit. Example code below.
private void originalPictureEdit_MouseDown(object sender, MouseEventArgs e)
{
//Point TextStartLocation = e.Location;
Cursor = Cursors.IBeam;
if (_drawStarted == false)
{
_drawStarted = true;
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Cursor = Cursors.Cross;
_cropX = e.X;
_cropY = e.Y;
_selection = new Rectangle(new Point(e.X, e.Y), new Size());
_cropPen = new Pen(Color.Black, 1);
_cropPen.DashStyle = DashStyle.DashDotDot;
}
}
originalPictureEdit.Refresh();
}
private void originalPictureEdit_MouseMove(object sender, MouseEventArgs e)
{
if (originalPictureEdit.Image == null)
return;
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
originalPictureEdit.Refresh();
_selection.Width = e.X - _selection.X;
_selection.Height = e.Y - _selection.Y;
originalPictureEdit.CreateGraphics().DrawRectangle(_cropPen, _cropX, _cropY, _selection.Width, _selection.Height);
}
}
private void originalPictureEdit_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && _drawStarted && _selection.Size != new Size())
{
_croppedImage = (_displayedImage as Bitmap).Clone(_selection, _displayedImage.PixelFormat);
modifiedPictureEdit.Image = _croppedImage;
modifiedPictureEdit.Width = _croppedImage.Width;
modifiedPictureEdit.Height = _croppedImage.Height;
}
Cursor = Cursors.Default;
_drawStarted = false;
}
I needed to find the positions of the scrollbars. Here is the working code for MouseUp: