画一条线,C#把它转换成直线?

问题描述 投票:0回答:1

如何画一条线并用C#将其转换成直线? 面板 A 是我的油漆,面板 b 是它的结果

我想让程序明白我画了一条线,然后在面板B中把它转换成直线。或者如果我画了一个正方形,它就变成了一个完美的正方形

像这样

代码

public partial class frmPaint : Form
    {
        public Point x = new();
        public Point y = new();
        public Pen penA = new(Color.Red, 2);
        public Pen Eraser = new(Color.White, 10);
        public Graphics graphics;
        public frmPaint()
        {
            InitializeComponent();
            graphics = panelDraw.CreateGraphics();
        }

        private void panelDraw_MouseDown(object sender, MouseEventArgs e)
        {
            y = e.Location;

            if (rbLineWidth5.Checked)
                penA.Width = 5;
            if (rbLineWidth10.Checked)
                penA.Width = 10;
            if (rbLineWidth15.Checked)
                penA.Width = 15;
        }

        private void panelDraw_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                x = e.Location;
                graphics.DrawLine(penA, x, y);
                y = e.Location;
            }

            if (e.Button == MouseButtons.Right)
            {
                x = e.Location;
                graphics.DrawLine(Eraser, x, y);
                y = e.Location;
            }
        }

        private void btnColor_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new();
            if (colorDialog.ShowDialog() == DialogResult.OK)
                penA.Color = colorDialog.Color;
        }
    }
c# .net winforms graphics bitmap
1个回答
0
投票

这段代码使用MouseLeave事件来判断绘制结束,它还有一个sizeDiff变量来判断开始绘制的点到结束点的距离,从而判断是正方形还是直线。 确定后,如果是直线,就在起点和终点之间画一条直线,如果是正方形,就画一个正方形,从图形的最低点开始,一直延伸到图形的最高点绘图。

希望这对您有所帮助,如果您有任何问题,请告诉我。

    bool drawing = false; // Drawing Var

    Point xGlobal = new Point(); 
    Point yGlobal = new Point();

    int sizeDiff = 50; //Var pixels to difference square to line  

private void panelDraw_MouseUp(object sender, MouseEventArgs e)
    {
        xGlobal = e.Location; // X Point for draw in the MouseLeave

        int XDiff = (yGlobal.X - xGlobal.X); // Difference start point and end point int width
        int YDiff = (yGlobal.Y - xGlobal.Y); // Difference start point and end point int height

        // Convert difference to positive integer
        if (XDiff < 0)
        {
            XDiff = XDiff * -1;
        }
        if (YDiff < 0)
        {
            YDiff = YDiff * -1;
        }

        bool isSquare = false; // isSquare Var

        // Verify start point and end point 
        if (XDiff < sizeDiff)
        {
            if (YDiff < sizeDiff)
            {
                isSquare = true; // defines whether it is a square or not based on the start and end point
            }
        }

        if (drawing == true)
        {
            if (isSquare)
            {
                //Draw Square in Panel B
                graphicsPanelB.DrawRectangle(penA, new Rectangle(upPoint, new Size(downPoint.X - upPoint.X, downPoint.Y - upPoint.Y))); //Graphics panel B
            } else
            {
                //Draw Line in Panel B
                graphicsPanelB.DrawLine(penA, xGlobal, yGlobal); //Graphics panel B
            }
            

            drawing = false;
        }

    }

    Point upPoint = new Point(); //Point A for Square
    Point downPoint = new Point(); //Point B for Square


    private void panelDraw_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            drawing = true; //On drawing var to MouseLeave event
        }
        y = e.Location;
        yGlobal = y; // Y Point for draw in the MouseLeave
        upPoint = y; //Reference point for Square
        downPoint = y; //Reference point for Square

        if (rbLineWidth5.Checked)
            penA.Width = 5;
        if (rbLineWidth10.Checked)
            penA.Width = 10;
        if (rbLineWidth15.Checked)
            penA.Width = 15;
    }

    private void panelDraw_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            x = e.Location;
            graphics.DrawLine(penA, x, y);
            y = e.Location;

            //Verify to decrease upPoint
            if (x.X < upPoint.X)
            {
                upPoint.X = x.X;
            }
            if (x.Y < upPoint.Y)
            {
                upPoint.Y = x.Y;
            }

            //Verify to increment downPoint
            if (x.X > downPoint.X)
            {
                downPoint.X = x.X;
            }
            if (x.Y > downPoint.Y)
            {
                downPoint.Y = x.Y;
            }
        }

        if (e.Button == MouseButtons.Right)
        {
            x = e.Location;
            graphics.DrawLine(Eraser, x, y);
            y = e.Location;
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.