使用C#从图片框中的图像绘制和擦除图形 [英] Draw and erase graphics from an image in a picture box using C#

查看:931
本文介绍了使用C#从图片框中的图像绘制和擦除图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在图像上绘制图形。我面临的问题是,当数据读取线程开始时,图形开始绘制在PictureBox中的图像上。



我希望当数据线程停止图形消失时然后在数据读取线程恢复时重新出现。请告诉我怎么做?



我正在分享我的代码块。



我的尝试:



Hello everyone,

I am drawing graphics on an image. The problem which I am facing that when data Read thread begins the graphics starts plotting on an image in a PictureBox.

I want when the data thread stops the graphics disappear and then reappear when the data Read thread resume. please tell me how I can do it??

I am sharing the block of my code.

What I have tried:

bool draw;

private void Form1_Load(object sender, EventArgs e)
    {
        pictureBox1.Image = new Bitmap("Image.jpg");
        draw = true;
        Thread DataRead = new Thread (Data_Read);
        DataRead.Start();
    }
      public void DataRead()
    {
        //code for data reading//;
         
         if (data_read !=null)
           {
             draw = true;
           }
        else 
           {
             draw = false;
           }         
      }

private void picbox_paint(object sender, PaintEventArgs e)
    {
         if (draw == true)
        {
            e.Graphics.DrawImage(new Bitmap("interface.png"), Convert.ToInt32(x), Convert.ToInt32(w), 16, 16);
            e.Graphics.DrawImage(new Bitmap("interface.png"), Convert.ToInt32(z), Convert.ToInt32(y), 16, 16);
         }
         
        else
        {
          // what should I do in this part?
        }
 e.Dispose(); pictureBox1.invalidate();
    }

推荐答案

简单:不要在图像上绘图。当您在图像上绘制时,更改是半永久性的 - 它们是图像本身的真实更改,但它们不会反映在图像来自的文件中 - 因此撤消它们的唯一方法是从原始来源重新加载图像。



如果你想要临时图形,那么要么在顶部放置透明图像并在上面绘图,要么存储你的每次在PictureBox的Paint事件中更改并实现它们,以便它们绘制到您提供的图形上下文中。这样,您在顶部绘制的图形可以在每次数据执行时更改 - 您所要做的就是将新值添加到要绘制的集合中,并使PictureBox无效。如果数据停止,您将从集合中暂停绘图,并在数据执行时恢复。
Simple: don't draw on the image. When you draw on an image, the changes are semi-permanent - they are "real" changes to the image itself, but they aren;t reflected in an file the image came from - so the only way to "undo" them is to reload the image itself from the original source.

If you want "temporary" graphics, then either place a transparent image over the top and draw on that, or store your changes and implement them in the Paint event for the PictureBox each time so they draw onto the graphics context you are supplied with. That way, the graphics you draw over the top can change each time the data does - all you have to do is add the new values to a collection to be drawn, and invalidate the PictureBox. If the data stops, you suspend drawing from your collection, and resume when the data does.


这篇关于使用C#从图片框中的图像绘制和擦除图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆