Click here to Skip to main content
12,398,000 members (45,711 online)
Click here to Skip to main content
Add your own
alternative version

Stats

197.5K views
5.7K downloads
82 bookmarked
Posted

Drawing and Editing Lines with GDI+

, 20 Oct 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
Here's the code to implement a basic graphic user interface to paint, and then edit, lines on a PictureBox.

Sample Image - lineditor.jpg

Introduction

My first article! I hope that this code will help someone... I searched through the web and I found many developers (like me) looking for a way to implement line drawing/editing, that seems to be a thing very hard to implement, due to C# limitations in this topic... GDI+ doesn't seem to come in help very much, and I decided to make a solution by myself, that seems to work pretty fine Big Grin | :-D . Here it is.

Once Upon a Time...

I started thinking about a Line Custom Control, but the result was very ugly, due to problems about transparency and Z-order (you cannot select a control that's under another one)... I then realized that the line itself had to be written directly to the PictureBox in order to avoid these problems, and the PictureBox.Image must be rewritten every time...

When the DrawLine button has been clicked, two red markers are placed in the PictureBox and a line is drawn. The two markers are actually two custom controls, inherited from UserControl, but they simply are a red square (that can be personalized, a cross, a circle, etc,), with no code inside. At the marker control creation, I attach three mouse events (MouseDown, MouseMove, MouseUp) in order to allow user to drag them around the PictureBox. Each time a marker is moved, all the lines (and the background image too) are redrawn. Very simple, isn't it?

Let's see some code...

private struct Line
{
    public MarkControl mark1;
    public MarkControl mark2;
    public int Width;
}

This object represents a line drawn on the PictureBox. It contains information about the two markers (their position, property Center) and the line width (editable through right mouse buttons). Each line drawn is added to an array of Lines; each time the image is redrawn, all the lines are redrawn using this info.

//Redraws all the lines and the background too
private void Redraw() 
{
    if(bmpBack!=null)
        image.Image = (Bitmap)bmpBack.Clone();
    else
    {
        image.Image = new Bitmap(image.Width,image.Height);
        Graphics.FromImage(image.Image).Clear(Color.White);
    }

    foreach(Line l in Lines)
    {
        DrawLine(l);
    }
    image.Refresh(); 
}

And this is the DrawLine function:

//Simply draws a line
private void DrawLine(Line line)
{
    Graphics g = null;

    g = Graphics.FromImage(image.Image);
    g.DrawLine(new Pen(Color.Black,(float)line.Width), 
               line.mark1.Center.X,line.mark1.Center.Y, 
               line.mark2.Center.X,line.mark2.Center.Y); 
    g.Dispose();
}

Amazing, it works! But there is a problem... When drawing lines on a big surface, like a desktop background, the program runs very slow, because the image is too heavy to be reloaded all the time... Then? I wrote a new Redraw(), that redraws all the lines in the same way, but refreshes only the region of the image that has been modified, through the Invalidate(Region region) method of the PictureBox... This trick code has to be a little enhanced, but it works very fine for now! If someone has a better idea, please tell me! Here's the code:

//Redraws all the lines and a part of the background
private void Redraw(Line line, Point p)
{
    Graphics.FromImage(image.Image).DrawImage(bmpBack,0,0, 
                       image.Image.Width, image.Image.Height);
    foreach(Line l in Lines)
    {
        DrawLine(l);
    }
    Region r = getRegionByLine(line,p);
    image.Invalidate(r);
    image.Update();
}

//Returns the region to update
private Region getRegionByLine(Line l, Point p)
{
    GraphicsPath gp = new GraphicsPath();
    gp.AddPolygon(new Point[]{l.mark1.Center,l.mark2.Center,p,l.mark1.Center});

    RectangleF rf = gp.GetBounds();
    gp.Dispose();

    rf.Inflate(100f,100f);

    return new Region(rf);
}

Line l is the line that has been moved, p is the Point where the marker has been moved... I calculate the region getting the triangle from the line and the point, then getting a rectangle that contains it, and then inflating it for a better result...

Lines Only?

This project can be modified as well, for other geometric figures like Circles, Ellipses, Rectangles, Polygons, and so on! Add more markers and use them as geometric points!

The End

Yeah, that's all folks Wink | ;) I hope that this article will help someone as other Code Project articles have helped me in my work everyday. Please send me feedbacks or/and advice! butch.er@tin.it.

Ciao!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Butch.er
Software Developer (Senior)
Italy Italy
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Yo Hiro4-Dec-13 14:23
memberYo Hiro4-Dec-13 14:23 
GeneralMy vote of 5 Pin
manoj kumar choubey21-Feb-12 23:44
membermanoj kumar choubey21-Feb-12 23:44 
Questioncan i do the same in panel?????? Pin
himani_jha3-Jan-12 21:39
memberhimani_jha3-Jan-12 21:39 
AnswerRe: can i do the same in panel?????? Pin
Butch.er3-Jan-12 22:31
memberButch.er3-Jan-12 22:31 
GeneralMy vote of 5 Pin
clingonboy5-Nov-11 2:43
memberclingonboy5-Nov-11 2:43 
GeneralMy vote of 5 Pin
jraju1142116-Oct-11 23:18
memberjraju1142116-Oct-11 23:18 
GeneralVertex image Pin
ISharda25-May-10 0:56
memberISharda25-May-10 0:56 
GeneralRe: Vertex image Pin
Butch.er25-May-10 1:01
memberButch.er25-May-10 1:01 
GeneralAWESOME!!!!!!!!!! Pin
Reidel2-Jan-10 6:32
memberReidel2-Jan-10 6:32 
JokeRe: AWESOME!!!!!!!!!! Pin
Butch.er2-Jan-10 10:49
memberButch.er2-Jan-10 10:49 
GeneralText Wraping in MarkControl Pin
rizwaanbutt31-Dec-07 19:06
memberrizwaanbutt31-Dec-07 19:06 
GeneralRe: Text Wraping in MarkControl Pin
Butch.er13-Feb-08 23:57
memberButch.er13-Feb-08 23:57 
QuestionProblem with: Picturebox size Mode & Marks Pin
xupi11-Jul-07 23:59
memberxupi11-Jul-07 23:59 
AnswerRe: Problem with: Picturebox size Mode & Marks Pin
Butch.er12-Jul-07 5:28
memberButch.er12-Jul-07 5:28 
QuestionRe: Problem with: Picturebox size Mode & Marks Pin
xupi12-Jul-07 7:00
memberxupi12-Jul-07 7:00 
QuestionNeed more Help Pin
rizwaanbutt22-May-07 21:14
memberrizwaanbutt22-May-07 21:14 
QuestionNeed your help to make it more faster Pin
rizwaanbutt13-May-07 20:22
memberrizwaanbutt13-May-07 20:22 
AnswerRe: Need your help to make it more faster Pin
Butch.er13-May-07 23:30
memberButch.er13-May-07 23:30 
QuestionRe: Need your help to make it more faster Pin
rizwaanbutt17-May-07 19:09
memberrizwaanbutt17-May-07 19:09 
QuestionRe: Need your help to make it more faster Pin
junzhi_li22-Jul-07 5:12
memberjunzhi_li22-Jul-07 5:12 
GeneralResizable graphic area Pin
GrusumZA12-Feb-07 23:26
memberGrusumZA12-Feb-07 23:26 
GeneralRe: Resizable graphic area Pin
Butch.er12-Feb-07 23:43
memberButch.er12-Feb-07 23:43 
GeneralRe: Resizable graphic area Pin
GrusumZA12-Feb-07 23:59
memberGrusumZA12-Feb-07 23:59 
GeneralRe: Resizable graphic area Pin
GrusumZA13-Feb-07 19:51
memberGrusumZA13-Feb-07 19:51 
GeneralI do not think so, it is not as good as it tis Pin
nn813719-Nov-06 1:35
membernn813719-Nov-06 1:35 
QuestionRe: I do not think so, it is not as good as it tis Pin
Butch.er27-Nov-06 21:41
memberButch.er27-Nov-06 21:41 
GeneralCool Pin
mindtalk18-Oct-06 17:49
membermindtalk18-Oct-06 17:49 
QuestionRe: Cool Pin
Butch.er29-Nov-06 5:12
memberButch.er29-Nov-06 5:12 
QuestionProblems with new operator and pointers Pin
FriendOfAsherah9-Oct-06 20:38
memberFriendOfAsherah9-Oct-06 20:38 
AnswerRe: Problems with new operator and pointers Pin
Butch.er10-Oct-06 4:41
memberButch.er10-Oct-06 4:41 
AnswerRe: Problems with new operator and pointers Pin
FriendOfAsherah11-Oct-06 10:09
memberFriendOfAsherah11-Oct-06 10:09 
GeneralBrilliant! Thanks so much!! Pin
BFENGINEERING6-Oct-06 11:55
memberBFENGINEERING6-Oct-06 11:55 
JokeRe: Brilliant! Thanks so much!! Pin
Butch.er10-Oct-06 4:11
memberButch.er10-Oct-06 4:11 
QuestionIncluding an eraser Pin
Broli6-May-06 13:20
memberBroli6-May-06 13:20 
AnswerRe: Including an eraser Pin
Butch.er10-Oct-06 23:21
memberButch.er10-Oct-06 23:21 
GeneralI need not the background Pin
lulupp21-Feb-06 14:21
memberlulupp21-Feb-06 14:21 
GeneralRe: I need not the background Pin
Butch.er21-Feb-06 23:53
memberButch.er21-Feb-06 23:53 
GeneralFine Project Pin
nick50127-Jan-06 2:34
membernick50127-Jan-06 2:34 
JokeRe: Fine Project Pin
Butch.er30-Jan-06 4:51
memberButch.er30-Jan-06 4:51 
Generalrecognition Pin
amir mortazavi26-Jan-05 18:49
memberamir mortazavi26-Jan-05 18:49 
GeneralRe: recognition Pin
Butch.er9-Mar-05 6:29
memberButch.er9-Mar-05 6:29 
GeneralBetter OO Design Pin
Heath Stewart21-Oct-04 5:22
protectorHeath Stewart21-Oct-04 5:22 
GeneralRe: Better OO Design Pin
Frank Olorin Rizzi21-Oct-04 6:33
memberFrank Olorin Rizzi21-Oct-04 6:33 
GeneralRe: Better OO Design Pin
Butch.er21-Oct-04 13:05
memberButch.er21-Oct-04 13:05 
Generalbadly edited article :( Pin
Mohammed Hossny21-Oct-04 1:31
memberMohammed Hossny21-Oct-04 1:31 
GeneralRe: badly edited article :( Pin
Butch.er21-Oct-04 12:52
memberButch.er21-Oct-04 12:52 
GeneralRe: badly edited article :( Pin
Mohammed Hossny22-Oct-04 1:58
memberMohammed Hossny22-Oct-04 1:58 
GeneralRe: badly edited article :( Pin
Butch.er22-Oct-04 3:11
memberButch.er22-Oct-04 3:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 21 Oct 2004
Article Copyright 2004 by Butch.er
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid