Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以C#windows形式绘制直线_C#_Visual Studio_Winforms - Fatal编程技术网

以C#windows形式绘制直线

以C#windows形式绘制直线,c#,visual-studio,winforms,C#,Visual Studio,Winforms,我有一个简单的windows窗体程序,允许用户在图片框中绘制直线。有一条线,但它从图片框中出来,并且在其中不可见(如我所附的图片)。我怎样才能使线条只显示在图片框中。这是我的密码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; usi

我有一个简单的windows窗体程序,允许用户在图片框中绘制直线。有一条线,但它从图片框中出来,并且在其中不可见(如我所附的图片)。我怎样才能使线条只显示在图片框中。这是我的密码:

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

}

您可以创建与PictureBox大小相同的位图,并在PictureBox的绘制事件中绘制该位图。然后在鼠标事件中绘制线以绘制位图。这将保留windows最小化/还原事件中的行。为了方便起见,我放置了整个代码:

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

永远不要使用CreateGraphics,不要使用Paint事件,不要使用/重复使用画笔等。这里有很多与drawingCheck相关的答案:,如@Proputinix所说,您应该使用OnPaint方法在winforms中绘制东西,这是最佳做法。您是否尝试过绘制第二条线,或者最小化/规范化表单?然后会发生什么?你是对的,在最小化和恢复后,它正在丢失内容,谢谢。编辑了答案。也许还有其他方法实现该功能,有什么意见吗?