use*_*071 12 c# system.drawing
我想在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);
}
Run Code Online (Sandbox Code Playgroud)
这给了一个错误.所以我想知道如何在这里包含paint事件(PaintEventArgs e)
并且还想知道在调用drawmethod时如何传递参数?例
DrawLineInt(Bitmap bmp);
Run Code Online (Sandbox Code Playgroud)
这会产生以下错误"当前上下文中不存在名称'e'"
Tom*_*Tom 23
"在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);
}
}
Run Code Online (Sandbox Code Playgroud)
重绘对象时会引发"Paint"事件.有关更多信息,请参阅
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.paint.aspx
你需要Graphics从Image类似的方式获取对象:
using(var graphics = Graphics.FromImage(bmp))
{
graphics.DrawLine(...)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54291 次 |
| 最近记录: |