I'm using WinForms. In my form i have a picturebox that has the ability to Pan and crop. The issue with my program is i don't have the ability to switch between crop and pan with a click of a button. How can i do this? I provided my code below.
//------CROP::::::::::::::
int cropX;
int cropY;
int cropWidth;
int cropHeight;
public Pen cropPen;
//------PAN::::::::::::::::
private Point _pt;
private Point _pt2;
bool _isPanning = false;
Point startPt;
//-----Button on/off:::::::
private bool crop_btn_OFF = false;
private bool pan_btn_OFF = false;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
_isPanning = true;
startPt = e.Location;
if (e.Button == System.Windows.Forms.MouseButtons.Left )
{
Cursor = Cursors.Cross;
cropX = e.X;
cropY = e.Y;
cropPen = new Pen(Color.FromArgb(153, 180, 209), 3);
cropPen.DashStyle = DashStyle.DashDotDot;
}
pictureBox1.Refresh();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
//X and Y are the position of the crop
pictureBox1.Refresh();
cropWidth = e.X - cropX;
cropHeight = e.Y - cropY;
pictureBox1.CreateGraphics().DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight);
}
//if (_isPanning) Un-comment this to Pan the image
//{
// Cursor = Cursors.SizeAll;
// Control c = (Control)sender;
// c.Left = (c.Left + e.X) - startPt.X;
// c.Top = (c.Top + e.Y) - startPt.Y;
// c.BringToFront();
//}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
_isPanning = false;
Cursor = Cursors.Default;
}
private void btn_Crop_Click(object sender, EventArgs e)
{
crop_btn_OFF = true;
pan_btn_OFF = false;
if (cropWidth < 1)
{
return;
}
Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);
//First we define a rectangle with the help of already calculated points
Bitmap OriginalImage = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);
//Original image
Bitmap _img = new Bitmap(cropWidth, cropHeight);
// for cropinf image
Graphics g = Graphics.FromImage(_img);
// create graphics
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//set image attributes
g.DrawImage(OriginalImage, 0, 0, rect, GraphicsUnit.Pixel);
pictureBox1.Image = _img;
pictureBox1.Width = _img.Width;
pictureBox1.Height = _img.Height;
}
private void btn_Pan_Click(object sender, EventArgs e)
{
crop_btn_OFF = false;
pan_btn_OFF = true;
}