【问题标题】:Draw Graphics on bitmap在位图上绘制图形
【发布时间】:2017-07-17 11:48:29
【问题描述】:

我尝试使用鼠标上下在位图上绘制矩形。但问题是矩形总是延迟一个事件。例如,我第一次尝试绘制一个矩形(0,0,50,50),但位图上没有矩形绘制。我继续绘制一个矩形(50,50,100,100),但创建了一个矩形(0,0,50,50)(不是矩形(50,50,100,100)。如果我继续绘制下一个矩形,它总是这样延迟。请帮助我! 这是我的代码:

Rectangle rect = new Rectangle(0, 0, 0, 0);
int Xmouse;
int Ymouse;
public Form1()
{
    InitializeComponent();
    pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
      this.DoubleBuffered = true;
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    Xmouse = e.X;
    Ymouse = e.Y;
    drawOK = true;
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    if (drawOK)
    {
        drawOK = false;
        rect = new Rectangle(Xmouse * 3676 / 800, Ymouse * 3676 / 800, (e.X - Xmouse) * 3676 / 800, (e.Y - Ymouse) * 3676 / 800);
        pictureBox1.Invalidate();
    }

}


private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    using (var g = Graphics.FromImage(pictureBox1.Image))
    {
        using (Pen myPen = new Pen(Color.Black, 6))
        {
            g.DrawRectangle(myPen, rect);
        }
    }

}

【问题讨论】:

    标签: graphics bitmap mouseevent mousedown mouseup


    【解决方案1】:

    尝试将您的 draw 方法移到绘图之外的另一个子程序中,然后从鼠标向上手动调用该子程序:

    Rectangle rect = new Rectangle(0, 0, 0, 0);
    int Xmouse;
    int Ymouse;
    bool drawOK;
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        Xmouse = e.X;
        Ymouse = e.Y;
        Console.WriteLine("MouseDown({0},{1})", e.X, e.Y);
        drawOK = true;
    }
    
    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        if (drawOK)
        {
            drawOK = false;
            Console.WriteLine("MouseUp({0},{1})", e.X, e.Y);
            rect = new Rectangle(Xmouse , Ymouse , (e.X - Xmouse) , (e.Y - Ymouse) );
            DrawRect();
            pictureBox1.Invalidate();
        }
    }
    
    private void DrawRect()
    {
        Console.WriteLine("drawing {0}", rect);
        using (var g = Graphics.FromImage(pictureBox1.Image))
        {
            using (Pen myPen = new Pen(Color.Black, 6))
            {
                g.DrawRectangle(myPen, rect);
            }
        }
    }
    
    private void ClearPic()
    {
        using (var g = Graphics.FromImage(pictureBox1.Image))
        {
            using (Brush myPen = Brushes.White)
            {
                g.Clear(Color.White);                 
            }
        }
    }
    
    private void Form1_Load(object sender, EventArgs e)
    {
        Image bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        pictureBox1.Image = bmp;
        ClearPic();
        pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
        pictureBox1.MouseDown += pictureBox1_MouseDown;
        pictureBox1.MouseUp += pictureBox1_MouseUp;
    }
    

    【讨论】:

    • 没问题。欢迎来到堆栈溢出。当你有机会阅读这个。 stackoverflow.com/help/someone-answers
    • 你能解释一下我错了什么吗?在这种情况下为什么不应该使用 Paint 事件??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多