Eric Lopez-Reyes
Eric Lopez-Reyes

Reputation: 97

Drawing a straight line in C# windows form

I have a simple windows forms program which allows the user to draw straight lines in a picture box. There is a line, but it goes out of the picture box and is not visible within it (like in the picture i attached). How can i make it so line shows up only in the picture box. Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;

namespace LinePOD
{
public partial class LineTest : Form
{
    public LineTest()
    {
        InitializeComponent();
    }

    Point p1 = new Point();
    Point p2 = new Point();
    Pen pen = new Pen(Color.Magenta, 10);
    private void LineTest_Load(object sender, EventArgs e)
    {

    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
            p1 = e.Location;
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {

        if (e.Button == MouseButtons.Left)
        {
            p2 = e.Location;
            Graphics g = this.CreateGraphics();
            g.DrawLine(pen, p1, p2);
        }
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        pictureBox1.BackColor = Color.Aqua;
    }
}

}

Upvotes: 0

Views: 2579

Answers (1)

Cem O
Cem O

Reputation: 36

You may create a bitmap with same size of PictureBox and draw that bitmap in the Paint event of PictureBox. Then draw lines to bitmap in your mouse events. This retains lines in windows minimize/restore events. I put entire code for convenience:

public partial class Form1 : Form
{
    Bitmap bitmap;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        bitmap = new Bitmap(pictureBox1.ClientSize.Width, 
        pictureBox1.ClientSize.Height, 
        System.Drawing.Imaging.PixelFormat.Format32bppArgb);

    }

    Point p1 = new Point();
    Point p2 = new Point();
    Pen pen = new Pen(Color.Magenta, 10);

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
            p1 = e.Location;
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {

        if (e.Button == MouseButtons.Left)
        {
            p2 = e.Location;
            Graphics g = Graphics.FromImage(bitmap);
            g.DrawLine(pen, p1, p2);
            pictureBox1.Invalidate();
            g.Dispose();
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        pictureBox1.BackColor = Color.Aqua;
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height);
    }
}

Upvotes: 1

Related Questions