#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    [C#] Problem when drawing a line using GDI+


    Hi everybody,

    I have a problem drawing a line in C# using GDI+.

    Please take a look at the following image



    Image of the problem

    As you can see from the image the second line drawn with Line thickness 2 appears straight, but it is not (look at the code down)

    Code:
    Graphics g = e.Graphics;
    
    Pen p = new Pen(Color.Black, 1); 
    g.DrawLine(p, 50, 50, 51, 150);
    
    p = new Pen(Color.Black, 2);
    g.DrawLine(p, 60, 50, 61, 150);
    
    p = new Pen(Color.Black, 3);
    g.DrawLine(p, 70, 50, 71, 150);
    Every line is drawn in a way that lower X coordinate is 1px larger then top X.

    The same thing is happening to every line with even thickness (2px, 4px, 6px, ...)

    But in a program that I'm creating the line with event thickness must appear exactly the same as those with odd thickness.

    The same is happening for horizontal and vertical lines with 1px difference.

    Smoothness is set to none.

    Please, if anybody knows the solution to this problem, please tell me. This is very urgent for me.

    Thanks in advance!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Location
    England
    Posts
    24
    Rep Power
    0
    If you'd thought about this for a few minutes you'd have got it I'm quite sure... If you draw a straight line which is two pixels thick, and then draw a 1-pixel-thick line on top of it starting top left and finishing bottom right you will not be able to see it...

    That one pixel difference within the computers's memory is less than the thickness of the line itself and therefore fits within it and is not represented visually.

    Originally Posted by boskoc
    Hi everybody,

    I have a problem drawing a line in C# using GDI+.

    Please take a look at the following image



    Image of the problem

    As you can see from the image the second line drawn with Line thickness 2 appears straight, but it is not (look at the code down)

    Code:
    Graphics g = e.Graphics;
    
    Pen p = new Pen(Color.Black, 1); 
    g.DrawLine(p, 50, 50, 51, 150);
    
    p = new Pen(Color.Black, 2);
    g.DrawLine(p, 60, 50, 61, 150);
    
    p = new Pen(Color.Black, 3);
    g.DrawLine(p, 70, 50, 71, 150);
    Every line is drawn in a way that lower X coordinate is 1px larger then top X.

    The same thing is happening to every line with even thickness (2px, 4px, 6px, ...)

    But in a program that I'm creating the line with event thickness must appear exactly the same as those with odd thickness.

    The same is happening for horizontal and vertical lines with 1px difference.

    Smoothness is set to none.

    Please, if anybody knows the solution to this problem, please tell me. This is very urgent for me.

    Thanks in advance!

IMN logo majestic logo threadwatch logo seochat tools logo