Почему неправильно рисуется квадрат Win Forms?

Добрый день. В общем делаю я Paint на win forms. Сделал рисовку карандашом и прямоугольники, но когда я добавляю изорбражение на picturebox и его перемещаю либо же растягиваю, то прямоугольник рисуется на правильно.
Вот мой код:

void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    isLMBclicked = true;

    if (mode == "rectangle")
    {
        point1 = new Point(e.X, e.Y);
        rec.Location = e.Location;
        newRectangle.FlatStyle = FlatStyle.Flat;
        newRectangle.FlatAppearance.BorderColor = pen.Color;
        newRectangle.FlatAppearance.BorderSize = trackBar1.Value;
    }
}

void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    isLMBclicked = false;
    if (mode == "pen")
    {
        arrayPoints.resetPoint();
    }
    else if (mode == "rectangle")
    {
        graphics.DrawRectangle(pen, rec);
        pictureBox1.Image = map;
        rec = new Rectangle();

        newRectangle.Visible = false;
    }
}

void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (!isLMBclicked) { return; }

    if (mode == "pen")
    {
        arrayPoints.setPoint(e.X, e.Y);

        if (arrayPoints.GetCountPoint() >= 2)
        {
            graphics.DrawLines(pen, arrayPoints.GetPoints());
            pictureBox1.Image = map;
            arrayPoints.setPoint(e.X, e.Y);
        }
    }
    else if (mode == "rectangle")
    {
        point2 = new Point(e.X, e.Y);
        if (point2.X > rec.Location.X && point2.Y > rec.Location.Y)
        {
            newRectangle.Visible = true;
            //newRectangle.BackColor = pen.Color;
            var newSize = new Size(point2.X - rec.Location.X, point2.Y - rec.Location.Y);

            if (MouseButtons == MouseButtons.Left)
                if (newSize.Width > 5 && newSize.Height > 5)
                {
                    rec.Size = newSize;

                    newRectangle.Location = new Point(point1.X, point1.Y);
                    newRectangle.Size = newSize;
                    Invalidate();
                }
        }
    }
}


Так надо:
61ee9e7b145ab548328573.jpeg

А получается так:
61ee9e911d8c5216445732.jpeg

То есть курсор в одном месте, а квадрат в другом, но когда я отпускаю ЛКМ то он становится на правильно место
  • Вопрос задан
  • 158 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы