如何在图像上画线?

我想在bmp图像上绘制一条线,该线在C#中使用drawline方法传递给方法

public void DrawLineInt(Bitmap bmp) { Pen blackPen = new Pen(Color.Black, 3); int x1 = 100; int y1 = 100; int x2 = 500; int y2 = 100; // Draw line to screen. e.Graphics.DrawLine(blackPen, x1, y1, x2, y2); } 

这给了一个错误。所以我想知道如何在这里包含paint事件(PaintEventArgs e)

并且还想知道在调用drawmethod时如何传递参数? 例

 DrawLineInt(Bitmap bmp); 

这给出了以下错误“当前上下文中不存在名称’e’”

“在bmp图像上绘制一条线,使用C#中的drawline方法传递给一个方法”

PaintEventArgs e会建议您在对象的“paint”事件中执行此操作。 因为你在一个方法中调用它,所以不需要在任何地方添加PaintEventArgs e。

要在方法中执行此操作,请使用@ BFree的答案。

 public void DrawLineInt(Bitmap bmp) { Pen blackPen = new Pen(Color.Black, 3); int x1 = 100; int y1 = 100; int x2 = 500; int y2 = 100; // Draw line to screen. using(var graphics = Graphics.FromImage(bmp)) { graphics.DrawLine(blackPen, x1, y1, x2, y2); } } 

重绘对象时会引发“Paint”事件。 有关更多信息,请参阅

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.paint.aspx

您需要从Image获取Graphics对象,如下所示:

 using(var graphics = Graphics.FromImage(bmp)) { graphics.DrawLine(...) }