在具有backgroundImage的面板上绘制 [英] drawing on a panel which has a backgroundImage

查看:95
本文介绍了在具有backgroundImage的面板上绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个名为dPanel的面板.我使用名为dImage的图像设置该面板的backgroundImage现在,我想在面板上绘制点,换句话说,我想使用鼠标为面板着色,我希望能够将绘图和图像保存在一起之后.我的代码可以执行此操作,但是图片在绘制过程中会变亮并且非常缓慢.这是我的代码

Hello I have a panel named dPanel. I set the the backgroundImage of that panel with an image named dImage.Now I want to draw points on the panel, in other words I want to color the panel by using mouse.I want to be able to save the drawing and the image together later. My codes do this but the picture lights up during drawing and its very slow.Here is my code

private void drawP_MouseDown(object sender, MouseEventArgs e)
      {
          if (!drawbool)
          {
              dStartPoint = e.Location;
              drawbool = true;
          }
          drawP.Invalidate();
      }

      private void drawP_MouseMove(object sender, MouseEventArgs e)
      {
          if (drawbool)
          {
              
              dStartPoint = e.Location;//dStartPoint is a point
              drawP.Invalidate();
          }
      }


private void drawP_MouseUp(object sender, MouseEventArgs e)
       {
           if (drawbool)
           {
               drawbool = false;
               
           }
       }


private void drawP_Paint_1(object sender, PaintEventArgs e)
       {
           if (drawbool)
           {
               int dStartX = dStartPoint.X;
               int dStartY = dStartPoint.Y;


               

               e.Graphics.DrawEllipse(dP, dStartX, dStartY, 2, 2);//dP is a Pen



               Bitmap dPPB = new Bitmap(drawP.Width, drawP.Height);
               
       drawP.DrawToBitmap(dPPB, new Rectangle(0, 0, drawP.Width, drawP.Height));
               drawP.BackgroundImage = (Image)dPPB;
                }
           }

推荐答案

您的方法几乎是正确的,但是您应该在代码中解决逻辑和问题.简而言之,您应该在Paint事件的处理程序中进行所有渲染.首先,绘制背景图像,然后在顶部绘制数据生成的鼠标线.鼠标事件仅操纵数据并调用Invalidate.

通过使用参数(RectangleRegion)调用Invalidate可以提高性能-它会触发仅场景部分的重新渲染.

另外,我必须说您不需要Panel.您最好从Control派生自定义控件并覆盖OnPaint而不是使用Paint事件.不用担心,无论如何,您都将需要使用派生的控件.为什么?因为您还需要添加双缓冲,但要这样做,您将需要访问受保护的方法SetStyles.

您将需要添加样式System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer.这对于避免闪烁很重要.

参见:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.forms.control.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.forms.controlstyles.aspx [ ^ ].

您还需要修复代码样式,命名等.

—SA
You approach is almost correct, but you should fix your logic and problems in the code. In brief, you should do all rendering in your handler of your Paint event. First, you draw the background image, and, on top if it, you draw your mouse-generated line from data. The mouse events only manipulate data and call Invalidate.

Your performance can be improved by calling Invalidate with a parameter (Rectangle or Region) — it will trigger re-rendering of only the part of the scene.

Also, I must say you don''t need the Panel. You should better derive a custom control from Control and override OnPaint instead of using Paint event. Don''t worry, you will need to resort to a derived control anyway. Why? Because you also need to add double buffering, but to do that, you will need to access the protected method SetStyles.

You will need to add the styles System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer. This is important to avoid flicker.

See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.controlstyles.aspx[^].

You also need to fix your code style, naming, etc.

—SA


这篇关于在具有backgroundImage的面板上绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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