C# 通过单击按钮C在平移和裁剪之间切换#
我正在使用WinForms。在我的表格中,我有一个能够平移和裁剪的图片盒。我的程序的问题是,我无法通过点击按钮在裁剪和平移之间切换。我该怎么做?我在下面提供了我的代码C# 通过单击按钮C在平移和裁剪之间切换#,c#,.net,winforms,crop,pan,C#,.net,Winforms,Crop,Pan,我正在使用WinForms。在我的表格中,我有一个能够平移和裁剪的图片盒。我的程序的问题是,我无法通过点击按钮在裁剪和平移之间切换。我该怎么做?我在下面提供了我的代码 //------CROP:::::::::::::: int cropX; int cropY; int cropWidth; int cropHeight; public Pen cropPen; //------PAN:::::::::::::::: priva
//------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;
}
/----CROP:
int-cropX;
int cropY;
国际cropWidth;
内克罗菲特;
公共钢笔种植;
//------PAN:
私人点(pt);
专用点2;
bool_isPanning=false;
Point startPt;
//-----按钮开/关:
私人bool crop\u btn\u OFF=假;
私有布尔值pan_btn_OFF=false;
私有void pictureBox1\u MouseDown(对象发送方,MouseEventArgs e)
{
_isPanning=true;
startPt=e.位置;
if(e.Button==System.Windows.Forms.MouseButtons.Left)
{
游标=游标。十字;
cropX=e.X;
cropY=e.Y;
cropPen=新笔(颜色为argb(153180209),3);
cropen.DashStyle=DashStyle.DashDotDot;
}
pictureBox1.Refresh();
}
私有void pictureBox1\u MouseMove(对象发送方,MouseEventArgs e)
{
if(e.Button==System.Windows.Forms.MouseButtons.Left)
{
//X和Y是作物的位置
pictureBox1.Refresh();
cropWidth=e.X-cropX;
cropHeight=e.Y-cropY;
pictureBox1.CreateGraphics().DrawRectangle(cropPen、cropX、cropY、cropWidth、cropHeight);
}
//如果(\u isPanning)取消对此进行注释以平移图像
//{
//Cursor=Cursors.SizeAll;
//控制c=(控制)发送方;
//c.Left=(c.Left+e.X)-startPt.X;
//c.Top=(c.Top+e.Y)-startPt.Y;
//c.布林托夫隆();
//}
}
私有无效图片box1u MouseUp(对象发送器,MouseEventArgs e)
{
_isPanning=false;
游标=游标。默认值;
}
私有无效btn\u裁剪\u单击(对象发送者,事件参数e)
{
crop_btn_OFF=真;
pan_btn_OFF=假;
如果(cropWidth<1)
{
返回;
}
矩形rect=新矩形(cropX、cropY、cropWidth、cropHeight);
//首先,我们在已经计算的点的帮助下定义一个矩形
位图原始图像=新位图(pictureBox1.Image,pictureBox1.Width,pictureBox1.Height);
//原始图像
位图_img=新位图(cropWidth,cropHeight);
//用于cropinf图像
Graphics g=Graphics.FromImage(_img);
//创建图形
g、 插值模式=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g、 PixelOffsetMode=System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g、 合成质量=System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//设置图像属性
g、 DrawImage(原始图像、0、0、rect、GraphicsUnit.Pixel);
pictureBox1.Image=\u img;
图B宽度=_img.Width;
图B高度=_img.Height;
}
私有无效btn\u平移\u单击(对象发送者,事件参数e)
{
crop_btn_OFF=假;
pan_btn_OFF=真;
}
如果我理解您的意图,那么您需要使用名为pictureBox1\u MouseDown的方法和名为pictureBox1\u MouseMove的方法来了解用户是处于平移模式还是裁剪模式。您已经从定义名为_isPanning的变量开始了这个想法,但是您需要在我提到的方法中使用这些数据
希望我的回答能让你走上正确的方向。我不太愿意给出更具体的答案,因为解决这个问题的方法不止一种。如果我理解您的意图,那么您需要名为pictureBox1\u MouseDown的方法和名为pictureBox1\u MouseMove的方法来了解用户是处于平移模式还是裁剪模式。您已经从定义名为_isPanning的变量开始了这个想法,但是您需要在我提到的方法中使用这些数据
希望我的回答能让你走上正确的方向。我不愿给出更具体的答案,因为解决这个问题的方法不止一种。一个好主意是使用枚举。例如,定义
状态
枚举如下:
public enum State
{
Pan,
Crop
}
并包括一个字段
private State currentState;
单击平移或裁剪按钮时,将currentState设置为:
public void btnCrop_Click()
{
currentState = State.Crop;
}
public void btnPan_Click()
{
currentState = State.Pan;
}
在方法中,使用currentState字段指定它们应该执行的操作
public void Foo()
{
if (currentState == State.Pan)
{
// Do Pan work
}
else if (currentState == State.Crop)
{
// Do Crop Work
}
}
一个好主意是使用枚举。例如,定义
状态
枚举如下:
public enum State
{
Pan,
Crop
}
并包括一个字段
private State currentState;
单击平移或裁剪按钮时,将currentState设置为:
public void btnCrop_Click()
{
currentState = State.Crop;
}
public void btnPan_Click()
{
currentState = State.Pan;
}
在方法中,使用currentState字段指定它们应该执行的操作
public void Foo()
{
if (currentState == State.Pan)
{
// Do Pan work
}
else if (currentState == State.Crop)
{
// Do Crop Work
}
}
当你点击按钮或拖动图像时,仔细考虑你想要发生什么。此时,裁剪按钮尝试执行两项操作:设置模式和应用裁剪 尝试更好地分离模式。您的按钮点击处理程序应仅按照@Leigh的建议切换模式,并且鼠标处理程序应根据模式进行平移或裁剪 我还没有测试你的方法的内容,所以它们可能需要一些调试。为了简化此过程,在命名变量时尝试遵循某种约定:例如,私有字段以下划线开头,方法和属性以大写字母开头,方法中的变量以小写字母开头。我还在你的评论后面添加了一些空格,因为现在还不清楚它们是应用于前一行还是后一行
private enum State
{
Pan,
Crop
}
private State _currentState;
public void btnCrop_Click()
{
_currentState = State.Crop;
}
public void btnPan_Click()
{
_currentState = State.Pan;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (_currentState == State.Crop)
{
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();
}
}
else // state = pan
{
_isPanning = true;
_startPt = e.Location;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (_currentState == State.Crop)
{
Cursor = Cursors.Cross;
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);
}
}
else // state = pan
if (_isPanning)
{
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)
{
Cursor = Cursors.Default;
if (_currentState == State.Crop)
{
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;
}
else // state = pan
{
// nothing to do here but leaving it for symmetry with the other methods
}
}
私有枚举状态
{
潘,,
收成
}
私人国家(currentState);;
公共无效btnCrop_Click()
{
_currentState=State.Crop;
}
公共无效btnPan_单击()
{
_currentState=State.Pan;
}
私有无效图片框1\u鼠标向下(