Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Why when i move the mouse around on the pictureBox1 it dosent draw the path on the right place in pictureBox2 ?

$
0
0

This is the code in form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace Find_Distance
{
    public partial class Form1 : Form
    {
        private int counter;
        private float xFactor, yFactor;
        List<PointF> points = new List<PointF>();
        double increment = 1.25;
        double factor = 1.0;
        Image img;
        private Point startingPoint = Point.Empty;
        private Point movingPoint = Point.Empty;
        private bool panning = false;
        GraphicsPath gp = new GraphicsPath();
        Point p;

        public Form1()
        {
            InitializeComponent();

            pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
            pictureBox1.MouseHover += new EventHandler(pictureBox1_MouseHover);
            xFactor = (float)pictureBox2.Width / pictureBox1.Width;
            yFactor = (float)pictureBox2.Height / pictureBox1.Height;
            label4.Visible = false;
            label5.Visible = false;
            label6.Visible = false;
            counter = 0;
            img = new Bitmap(@"d:\radar000075.png");
            pictureBox1.Load(@"d:\radar000075.png");
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            label4.Visible = true;
            label4.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
            if (panning)
            {
                movingPoint = new Point(e.Location.X - startingPoint.X,
                                        e.Location.Y - startingPoint.Y);
                pictureBox1.Invalidate();
            }
            if (checkBox2.Checked && e.Button == MouseButtons.Left)
            {
                gp.AddLine(p, e.Location);
                p = e.Location;
                pictureBox2.Invalidate();
            }
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                points.Add(new PointF(e.X * xFactor, e.Y * yFactor));
                pictureBox2.Invalidate();
                label5.Visible = true;
                label5.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
                counter += 1;
                label6.Visible = true;
                label6.Text = counter.ToString();
            }
        }

        private void pictureBox2_Paint(object sender, PaintEventArgs e)
        {
            Pen p;
            p = new Pen(Brushes.Green);
            foreach (PointF pt in points)
            {
                e.Graphics.FillEllipse(Brushes.Red, pt.X, pt.Y, 3f, 3f);
            }

            for (int i = 0; i < points.Count - 1; i++)
            {
                if (points.Count > 1)
                {
                    e.Graphics.DrawLine(p, points[i].X, points[i].Y, points[i+1].X, points[i+1].Y);
                }
            }

            if (checkBox2.Checked)
            {
                using (Pen pp = new Pen(Color.Green, 2f))
                {
                    pp.StartCap = pp.EndCap = LineCap.Round;
                    pp.LineJoin = LineJoin.Round;
                    e.Graphics.DrawPath(pp, gp);
                }
            }
        }

        private void pictureBox2_SizeChanged(object sender, EventArgs e)
        {
            xFactor = (float)pictureBox2.Width / pictureBox1.Width;
            yFactor = (float)pictureBox2.Height / pictureBox1.Height;
        }

        void pictureBox1_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Focus();
        }

        void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            CalculateNewSizeFactor(e.Delta);
            if (pictureBox1.Image != null) pictureBox1.Image.Dispose();
            pictureBox1.Image = null;
            pictureBox1.Image = ResizeImage(new Size((int)(img.Width * factor), (int)(img.Height * factor)));

        }

        public Image ResizeImage(Size size)
        {
            return new Bitmap(img, size);
        }

        private void CalculateNewSizeFactor(int delta)
        {
            if (delta > 0 && factor < 2)
            {
                factor *= increment;
            }
            else if (delta < 0 && factor > 0.25)
            {
                factor /= increment;
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (checkBox1.Checked)
            {
                panning = true;
            }
            startingPoint = new Point(e.Location.X - movingPoint.X,
                                      e.Location.Y - movingPoint.Y);
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            panning = false;
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {            
                e.Graphics.Clear(Color.White);
                e.Graphics.DrawImage(pictureBox1.Image, movingPoint);           
        }

When i click on pictureBox1 its dwring points each click a point on pictureBox2 and connect the points with a line.

Now i added checkBox2 and i wanted to do that when its checked if i click all the time on the left mouse button and drag the mouse around pictureBox1 it will draw a path line on pictureBox2.

The problems are:

1. If i check checkBox2 then make one left click on mouse button over pictureBox1 area its drawing a straight line on pictureBox2 from 0,0 to the other side without im draging the mouse at all.

2. Its drawing the path trail line on pictureBox2 but not in the right places i want it to be like i did with creating the points when i make left click single left click on pictureBox1 so the same the path to be in the right scale/size in pictureBox2.

How can i solve this two problems ?


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>