[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Draw a line pixel by pixel using C#

Posted on 2007-10-04
8
Medium Priority
?
5,573 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:hiteshgoldeneye
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 21

Expert Comment

by:surajguptha
ID: 20013875
You mean to say you want to connect two points pixel by pixel so it seems like a line?
0
 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 20014022
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);

0
 
LVL 13

Author Comment

by:hiteshgoldeneye
ID: 20015457
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 20015564
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?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 20015625
So something like?...

        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;
        private System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer();

        private void PaintExample_Load(object sender, EventArgs e)
        {
            tmr.Enabled = false;
            tmr.Tick += new EventHandler(tmr_Tick);
            tmr.Interval = 25;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;

            curDist = 0;
            deltaX = ptB.X - ptA.X;
            deltaY = ptB.Y - ptA.Y;
            distance = (int)Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2));
            tmr.Start();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (curDist > 0)
            {
                int offsetX = (int)((double)curDist / (double)distance * (double)deltaX);
                int offsetY = (int)((double)curDist / (double)distance * (double)deltaY);
                Pen p = new Pen(Color.Blue, 3);
                e.Graphics.DrawLine(p, ptA, new Point(ptA.X + offsetX, ptA.Y + offsetY));
                p.Dispose();
            }
            e.Graphics.FillEllipse(Brushes.Red, new Rectangle(ptA.X - 4, ptA.Y - 4, 8, 8));
            e.Graphics.FillEllipse(Brushes.Red, new Rectangle(ptB.X - 4, ptB.Y - 4, 8, 8));
        }

        void tmr_Tick(object sender, EventArgs e)
        {
            if (curDist < distance)
            {
                curDist++;
                pictureBox1.Refresh();
            }
            else
            {
                tmr.Stop();
                button1.Enabled = true;
            }
        }
0
 
LVL 13

Author Comment

by:hiteshgoldeneye
ID: 20039230
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?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 20041256
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...
0
 
LVL 13

Author Comment

by:hiteshgoldeneye
ID: 20049559
Thanks Idle Mind
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month20 days, 10 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question