Link to home
Start Free TrialLog in
Avatar of Hitesh Manglani
Hitesh ManglaniFlag for India

asked on

Draw a line pixel by pixel using C#

hi i need to connect two lines pixel by pixel using C#, so that the user sees a progressive line.any ideas how to do that
Avatar of surajguptha
surajguptha
Flag of United States of America image

You mean to say you want to connect two points pixel by pixel so it seems like a line?
This should get you started in the right direction...
Create a simple C# windows application.
Drop a button on the form in the lower righthand corner.
On the button click event handler for the button put this code:
Pen BlackPen = new Pen(Color.Black, 2);
System.Drawing.Graphics ThisFormsGraphics = this.CreateGraphics();
ThisFormsGraphics.DrawLine(BlackPen, 50, 50, 35, 35);

Avatar of Hitesh Manglani

ASKER

maybe i am not clear, i know i can use a Drawline Function to plot a line, but i need a progressive line (which plots the line pixel by pixel) this i believe cannot be achieved by the DrawLine function
Ahh, so in abstract terms, you need to be able to draw a line from one point to another point but slowly, pixel by pixel. In other words, the user would see the line slowly getting drawn from one point to the other. I also assume the line will not always be a nice verticle or horizontal line.
Am I on the right track now?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi sorry for the delay in response. I tried the code above, works well with a single line. but when i tried to use it in my application there is a problem. i need to draw two or more lines (continous) pixel by pixel, but after drawing the second line line the previous line disappears. Any suggestions how do i solve it?
Well...then you need to use an Array, ArrayList, or other structure to hold multiple pieces of data.

All of these variables need to be in that structure:

        private Point ptA = new Point(50, 50);
        private Point ptB = new Point(250, 125);
        private int deltaX;
        private int deltaY;
        private int curDist = -1;
        private int distance = -1;

Then you simply iterate over the structure and perform the same technique above on each one.  If you want you could even write a class to encapsulate the data...
Thanks Idle Mind