-1

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;
        }
    }
Mardibak
  • 61
  • 1
  • 7
  • You need to get a book or follow a tutorial; Windows Forms graphics are too complicated to make stuff up as you go. You need to draw to the window graphics context, not a fake one you made up. You must draw to the screen **only** in the `Paint` event handler, not in a click or `MouseMoved` event. Start your research with [How to use the Paint event](https://stackoverflow.com/questions/53706772/). – Dour High Arch Apr 24 '23 at 17:45

1 Answers1

1

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;
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Keegan Fisher
  • 133
  • 1
  • 8
  • Please don't use snippets for C#. The snippets feature is only for HTML, CSS and JavaScript questions. For all other languages, use normal code blocks. – Mark Rotteveel Apr 24 '23 at 08:35
  • Thanks for editing, I meant to convert it back to a code block (I just used the snippet for formatting/tab button) – Keegan Fisher Apr 25 '23 at 05:07
  • You can select your code and use the Ctrl+K key combination for that (or type three backticks before and after your code). – Mark Rotteveel Apr 25 '23 at 08:53