Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

c# - how to draw a line on a image?

i want to draw a line on a bmp image which is pass into a method using drawline method in C#

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);
}

this give a error.So i want to know how to include paint event here (PaintEventArgs e )

and also want to know how to pass parameters when we calling drawmethod? example

DrawLineInt(Bitmap bmp);

this give the following error "The name 'e' does not exist in the current context "

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

"Draw a line on a bmp image which is pass into a method using drawline method in C#"

PaintEventArgs e would suggest that you are doing this during the "paint" event for an object. Since you are calling this in a method, then no you do not need to add PaintEventArgs e anywhere.

To do this in a method, use @BFree's answer.

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);
    }
}

The "Paint" event is raised when the object is redrawn. For more information see:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...