简体   繁体   English

在C#Windows窗体中绘制一条直线

[英]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. 我有一个简单的Windows窗体程序,该程序允许用户在图片框中绘制直线。 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;
    }
}

} }

You may create a bitmap with same size of PictureBox and draw that bitmap in the Paint event of PictureBox. 您可以创建一个与PictureBox大小相同的位图,并在PictureBox的Paint事件中绘制该位图。 Then draw lines to bitmap in your mouse events. 然后在鼠标事件中绘制线条以绘制位图。 This retains lines in windows minimize/restore events. 这将在Windows中保留行以最小化/还原事件。 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM