这是我到目前为止所做的。我一直在搜索代码并尝试将其应用到我的程序中。但它根本不起作用。
public Form1()
{
InitializeComponent();
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
}
int originalX, originalY;
int startX, startY, endX, endY;
int pointX1, pointY1;
int pointX2, pointY2;
Graphics g;
int i = 0, j = 0;
Bitmap image;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (i == 0)
{
image = (Bitmap)pictureBox1.Image;
pictureBox1.Invalidate();
originalX = startX = e.X;
originalY = startY = e.Y;
g = Graphics.FromImage(image);
}
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
pictureBox1.Refresh();
g = pictureBox1.CreateGraphics();
if (j < 10000000)
{
if (i != 0 )
{
startX = pointX1;
startY = pointY1;
}
pointX2 = e.X;
pointY2 = e.Y;
g.DrawLine(Pens.Black, new Point(startX, startY), new Point(pointX2, pointY2));
j++;
}
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
g = Graphics.FromImage(image);
if (i <100000000 )
{
if (i != 0)
{
startX = pointX1;
startY = pointY1;
}
pointX1 = e.X;
pointY1 = e.Y;
g.DrawLine(Pens.Black, new Point(startX, startY), new Point(pointX1, pointY1));
}
}
i++;
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(@"C:\Users\student03\Documents\image\Sky2.jpg");
}
private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
g = Graphics.FromImage(image);
g.DrawLine(Pens.Black, new Point(pointX1, pointY1), new Point(originalX, originalY));
pictureBox1.Image = image;
i = 0;
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back)
{
pictureBox1.Invalidate();
}
答案 0 :(得分:1)
在您的表单中,在this.KeyPreview = true;
电话:
InitializeComponent();
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
}
Form.KeyPreview
是一个布尔字段/属性,表示您的Form
对象能够在进入焦点Control
元素之前预览该按键。
修改强>
为了能够删除您正在绘制的行,您必须修改代码中的所有内容。但从头开始。您应该创建一种名为Line
的结构来存储有关绘图项的信息:
public struct Line
{
public Point StartingPoint;
public Point EndingPoint;
}
然后在您的Form1
代码中删除这些字段:
int originalX, originalY;
int startX, startY, endX, endY;
int pointX1, pointY1;
int pointX2, pointY2;
Graphics g;
并用以下内容替换它们:
List<Line> lines;
Line current;
现在您应该在构造函数中实例化lines
:
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
lines = new List<Line>();
}
这样做可以引用您将要创建的行,并且您有一个对象将保持当前正在生成的行。
接下来就是你直接在图像上绘图。我建议将原始图像存储为参考,并修改图像的副本,以便在Form1
中添加新字段时需要:
Image originalImage;
在button2_Click
:
originalImage = Image.FromFile(@"C:\Users\student03\Documents\image\Sky2.jpg");
pictureBox1.Image = originalImage.Clone();
进入绘图部分。将pictureBox1_MouseDown
的内容修改为:
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (i == 0)
{
current = new Line();
current.StartingPoint = new Point(e.X, e.Y);
}
}
现在您的当前行已填充了起始位置,您可以继续修改pictureBox1_MouseUp
:
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
current.EndingPoint = new Point(e.X, e.Y);
lines.Add(current)
pictureBox1.Image = originalImage.Clone();
Graphics g = Graphics.FromImage(pictureBox1.Image);
if (i <100000000 )
{
foreach(Line l in lines)
{
g.DrawLine(Pens.Black, l.StartingPoint, l.EndingPoint);
}
}
}
i++;
几乎相同的逻辑适用于pictureBox1_MouseMove
,但不是将current
行添加到lines
列表,而是必须在最后绘制它:
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
pictureBox1.Image = originalImage.Clone();
Graphics g = Graphics.FromImage(pictureBox1.Image);
if (i <100000000 )
{
foreach(Line l in lines)
{
g.DrawLine(Pens.Black, l.StartingPoint, l.EndingPoint);
}
}
current.EndingPoint = new Point(e.X, e.Y);
g.DrawLine(Pens.Black, current.StartingPoint, current.EndingPoint);
}
i++;
然后要删除该行,您只需删除行集合中的最后一项并重绘图像:
if (e.KeyCode == Keys.Back)
{
if ( lines.RemoveAt(lines.Count - 1) )
{
pictureBox1.Image = originalImage.Clone();
Graphics g = Graphics.FromImage(pictureBox1.Image);
foreach(Line l in lines)
{
g.DrawLine(Pens.Black, l.StartingPoint, l.EndingPoint);
}
}
}
即使它会起作用也是非常不合理的,因为你正在绘制图像本身,而不是pictureBox1
控件。我建议您使用pictureBox1.Paint
事件在图像上方进行绘画。