How can I draw a line and make C# convert it into a straight line?

93 Views Asked by At

panel A its paint, can draw something, just like simple paint in windows, and panel B its result

want the program to understand that I have drawn a line and then convert it to a straight line in panel B. Or if I draw a square, it becomes a perfect square

like this

Code

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;
        }
    }
1

There are 1 best solutions below

3
Keegan Fisher On

You could get the y-coordinate of the first pixel of the curved line, then loop through all pixels and set their y-coordinate to this value.

To find the start of a line, you can loop through the bitmap pixels until you find a pixel with only 1 neighbor pixel that is black (assuming the bitmap consists of white/black colored pixels).

i.e.

// Retrieve the image.
image1 = new Bitmap(@"\\ path \ to \ file.bmp", true);

int x, y;
int yLevel = -1; // use for straight line

// Loop through the images pixels to get start of line
for(x=0; x < image1.Width; x++) {
  for(y=0; y < image1.Height; y++) {
    Color pixelColor = image1.GetPixel(x, y);
    if (pixelColor != Color.Black) continue;
    
    int blackNeighbors = 0;
    
    if(image1.GetPixel(x-1,y-1) == Color.Black) { 
      blackNeighbors++;
    }
    if(image1.GetPixel(x-1,y) == Color.Black) { 
      blackNeighbors++;
    }
    if(image1.GetPixel(x-1,y+1) == Color.Black) { 
      blackNeighbors++;
    }
    if(image1.GetPixel(x,y-1) == Color.Black) { 
      blackNeighbors++;
    }
    if(image1.GetPixel(x,y+1) == Color.Black) { 
      blackNeighbors++;
    }
    if(image1.GetPixel(x+1,y+1) == Color.Black) { 
      blackNeighbors++;
    }
    if(image1.GetPixel(x+1,y) == Color.Black) { 
      blackNeighbors++;
    }
    if(image1.GetPixel(x+1,y-1) == Color.Black) { 
      blackNeighbors++;
    }
    
    if(blackNeighbors == 1) {
      yLevel = y;
      break;
    }
  }
  if (yLevel != -1) {
    break;
  }
}



// Loop through the images pixels to straighten line
for(x=0; x < image1.Width; x++) {
  for(y=0; y < image1.Height; y++) {
    Color pixelColor = image1.GetPixel(x, y);
    if (pixelColor == Color.Black && y != yLevel) {
      image1.SetPixel(x, y, Color.White);
      image1.SetPixel(x, yLevel, Color.Black);
    }
  }
}

// Set the PictureBox to display the image.
PictureBox1.Image = image1;