【问题标题】:How to prevent "drawstring" from being removed?如何防止“拉绳”被拆除?
【发布时间】:2019-01-12 09:44:12
【问题描述】:
    private void Side_pictureBox_Paint(object sender, PaintEventArgs e)
        {

            if (doubleclicked == true)
            {


                Side_pictureBox.Refresh();
                for (int numbering_for_digram = 1; numbering_for_digram <= No_of_circle; numbering_for_digram++)
                {
                    //MessageBox.Show(numbering_for_digram.ToString());
                    String drawString = numbering_for_digram.ToString();
                    // Create font and brush.


                    Font drawFont = new Font("Calibri (Body)", 20);
                    SolidBrush drawBrush = new SolidBrush(Color.Blue);

                    // Create point for upper-left corner of drawing.
                    //float x = 0;
                    //float y = 0;
                    doubleclicked = false;
                    // Draw string to screen.

                    e.Graphics.DrawString(drawString, drawFont, drawBrush, lastPoint);




                    Side_pictureBox.Update();


                    //MessageBox.Show("passed graphics draw");


                }

            }
        }


        }

private void Side_pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown == true && Edit_Variables.add_remark_now==false)//check to see if the mouse button is down

            {

                if (lastPoint != null)//if our last point is not null, which in this case we have assigned above

                {

                    if (Side_pictureBox.Image == null)//if no available bitmap exists on the picturebox to draw on

                    {
                        //create a new bitmap
                        Bitmap bmp = new Bitmap(Side_pictureBox.Width, Side_pictureBox.Height);

                        Side_pictureBox.Image = bmp; //assign the picturebox.Image property to the bitmap created

                    }

                    using (Graphics g = Graphics.FromImage(Side_pictureBox.Image))

                    {//we need to create a Graphics object to draw on the picture box, its our main tool

                        //when making a Pen object, you can just give it color only or give it color and pen size


                        g.DrawLine(new Pen(Color.DarkRed, 2), lastPoint, e.Location);
                        g.SmoothingMode = SmoothingMode.AntiAlias;
                        //this is to give the drawing a more smoother, less sharper look


                    }

                    Side_pictureBox.Invalidate();//refreshes the picturebox

                    lastPoint = e.Location;//keep assigning the lastPoint to the current mouse position


                }

            }

这就是整个过程的工作方式,首先我将使用“笔”在图像上进行绘制。此绘图过程以 mousedown 开始,以 mouseup 事件结束。一旦检测到 mouseup,我需要单击图像中的一个点,并且它假设在图像本身上绘制字符串(在图像上绘制“1”)。对于第一个拉绳事件,结果很好。但是,一旦我触发 mousedown 事件,它将删除之前的 drawstring("1")。有人可以帮助我如何防止“以前的”拉绳被移除吗?非常感谢大家!

【问题讨论】:

  • 保留所需的 所有 数据(可能在 List 集合中)并在 each 调用上绘制 所有 元素! - 可扩展示例DrawAction
  • 每次移动鼠标时都会为 PicturBox.Image 属性设置一个新图像(并且您永远不会丢弃旧图像或笔)。您需要在控件的 Paint 事件中执行所有绘画。如果您需要添加和删除不同的形状,请使用某种容器。例如,一个列表。在 MouseDown 事件中将新形状添加到其中,您将在其中创建一个新对象。然后,您将在 MouseUp 事件中完成对象。如果你需要保存绘画,将它们保存在Bitmap文件中,一次将所有形状绘制在Bitmap中,然后将其保存到光盘中。
  • 更好,通过使用专门的类,如链接示例中所示的 TaW。
  • 抱歉回复晚了!谢谢大家的所有建议,我会给他们我尝试更新我的结果!
  • @Jimi dispose 的目的是什么?奇怪的是我可以画多条线并且不会消失,而只有拉绳会消失。

标签: c# picturebox mousedown drawstring paintevent


【解决方案1】:

不要直接在颜料盒中绘制。改为在 PaintBox 的图像中绘制。在 MouseMove 事件结束时执行此操作,然后调用 Side_pictureBox.Invalidate();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    相关资源
    最近更新 更多