How to draw line in bitmap and picturebox in C #?

0

At the moment of drawing pixels it does it perfectly, but when drawing the line it does it in the wrong way, drawing the line out of the range of the second pixel.

I leave the code of the form in case someone is so kind to help me, I would greatly appreciate your support.

namespace ProyectoGraficacion
{
    public partial class Form1 : Form
    {
        //Variables
        Bitmap Lienzo; 
        Boolean BanderaPixel, BanderaRecta; //Permite activar el dibujado del pixel
        Boolean BanderaPoligono;
        Color MiColor; //variable para asignar el color al dibujar
        List<Point> Pixeles = new List<Point>();
        List<Point> Auxiliar = new List<Point>();
        int Grosor;
        int INCX=0;
        int INCY=0;
        float x = 0;
        float y = 0;

        public Form1()
        {
            InitializeComponent();
            Lienzo = new Bitmap(pbAreaDeImagen.Width, pbAreaDeImagen.Height); //El tamaño del lienzo tiene que ser igual al del picture Box
            MiColor = Color.Black;            
        }

        private void MouseDibujarPixel(object sender, MouseEventArgs e)
        {
            if (BanderaPoligono == true)
            {

                GrosorPixel(e.X, e.Y, MiColor, Grosor);
                Auxiliar.Add(new Point(e.X, e.Y));

                if (Auxiliar.Count == Convert.ToInt32(cbLados.SelectedItem))
                {
                    Irregular(Auxiliar, Convert.ToInt32(cbLados.SelectedItem), MiColor);
                    Auxiliar.Clear();
                }
            }

            if (BanderaRecta == true)
            {
                GrosorPixel(e.X, e.Y, MiColor, Grosor);
                Pixeles.Add(new Point(e.X, e.Y));

                if (Pixeles.Count == 2)
                {
                    Recta(Pixeles, MiColor);
                    Pixeles.Clear();
                }
            }

            if (BanderaPixel == true && Grosor>1)
            {
                GrosorPixel(e.X, e.Y, MiColor, Grosor);
            }
            else if (BanderaPixel == true)
            {
                Lienzo.SetPixel(e.X, e.Y, MiColor);
            }
                pbAreaDeImagen.Image = Lienzo;
        }


        private void ActivarPixel_Click(object sender, EventArgs e)
        {
            BanderaPixel = true;
        }

        private void LimpiarImagen_Click(object sender, EventArgs e)
        {
            Lienzo = new Bitmap(pbAreaDeImagen.Width, pbAreaDeImagen.Height);
            pbAreaDeImagen.Image = Lienzo;
        }

        private void ColorAzul_Click(object sender, EventArgs e)
        {
            MiColor = Color.Blue;
            ColorenUso.BackColor = MiColor;
        }

        private void ColorAmarillo_Click(object sender, EventArgs e)
        {
            MiColor = Color.Yellow;
            ColorenUso.BackColor = MiColor;
        }

        private void ColorRojo_Click(object sender, EventArgs e)
        {
            MiColor = Color.Red;
            ColorenUso.BackColor = MiColor;
        }

        private void ColorVerde_Click(object sender, EventArgs e)
        {
            MiColor = Color.Green;
            ColorenUso.BackColor = MiColor;
        }

        private void btnPaletaColores_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                this.MiColor = colorDialog1.Color;
            }
        }

        public void GrosorPixel(int x, int y, Color MiColor, int Grosor)
        {
            for ( int i = 0; i < Grosor; i++)
            {
                for (int j = 0; j < Grosor; j++)
                {
                    ValidarPixel(x + i, y + j, MiColor);
                }
            }
        }

        private void cbGrosor_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grosor = Convert.ToInt32(cbGrosor.SelectedItem);

        }

        public void ValidarPixel(int x, int y,Color MiColor)
        {
            if (x > 0 && y > 0 && x < pbAreaDeImagen.Width && y < pbAreaDeImagen.Height)
            {
                Lienzo.SetPixel(x , y , MiColor);
            }
        }

        private void btnTrazarLinea_Click(object sender, EventArgs e)
        {
            BanderaRecta = true;
            BanderaPixel = false;
        }

        public void Recta(List<Point> Pixeles, Color MiColor)
        {
            float m, b, DX, DY;
            DX = Pixeles[1].X - Pixeles[0].Y;
            DY = Pixeles[1].Y - Pixeles[0].Y;
            m = DY / DX;
            b = Pixeles[0].Y - m * Pixeles[0].X;
            if (DX > 0)
            {
                INCX = 1;
            }
            else
            {
                INCX = -1;
            }
            if (DY > 0)
            {
                INCY = 1;
            }
            else
            {
                INCY = -1;
            }
            if(Math.Abs(DX)>Math.Abs(DY))
            { 
                for (int x = Pixeles[0].X; x <= Pixeles[1].X; x = x+INCX)
                {
                    y = m * x + b;
                    GrosorPixel(Convert.ToInt32(x),Convert.ToInt32(y), MiColor, Grosor);
                    //Lienzo.SetPixel(Convert.ToInt32(x), Convert.ToInt32(Math.Round(y)), MiColor);
                }
            }
            else
            {
                for (int y = Pixeles[0].Y; y <= Pixeles[1].Y; y = y+INCY)
                {
                    x = (y - b) / m;
                    GrosorPixel(Convert.ToInt32(x), Convert.ToInt32(y), MiColor, Grosor);
                    //Lienzo.SetPixel(Convert.ToInt32(Math.Round(x)), Convert.ToInt32(y), MiColor);
                }
            }
        }



        public void Irregular(List<Point> Pixeles, int Lados, Color MiColor)
        {

            for (int i = 0; i <= Lados-1; i++)
            {
                Auxiliar[0] = Pixeles[i];
                Auxiliar[i] = Pixeles[i + 1];
                Recta(Auxiliar, MiColor);
            }
                Auxiliar[0] = Pixeles[Lados-1];
                Auxiliar[1] = Pixeles[0];
                Recta(Auxiliar, MiColor);

        }

         private void AdaptarPantalla(object sender, EventArgs e)
        {
            pbAreaDeImagen.Width = this.Width - 50;
            pbAreaDeImagen.Height = this.Height - 225;
            btnLimpiarImagen.Top = this.Height - 100;
            btnLimpiarImagen.Left = this.Width - 120;
            gbColores.Left = this.Width - 270;
            Lienzo = new Bitmap(pbAreaDeImagen.Width, pbAreaDeImagen.Height);
        }

         private void btnPoligono_Click(object sender, EventArgs e)
         {

             BanderaPixel = false;
             BanderaRecta = false;
             BanderaPoligono = true;
         }


    }
}
    
asked by Alan Fernando Castillo 28.09.2017 в 07:48
source

1 answer

2

You have a typo in your Recta method. When you calculate the difference in X, in your code you have DX = Pixeles[1].X - Pixeles[0].Y; . It should be DX = Pixeles[1].X - Pixeles[0].X;

    
answered by 28.09.2017 в 12:06