在面板上捕获绘图 [英] capture the drawing on a panel

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

问题描述

大家好,



i在c#中有一个带有面板和图片框的应用程序

用户可以在面板(使用mouseUp,mouseDown和mouseMove事件)

然后我需要显示用户在面板上绘制的内容,在图片框中(这是我的问题)



我的代码是:

hi all,

i have an application in c# with a panel and a picture box
the user is able to draw on the panel (using mouseUp, mouseDown and mouseMove events)
and then i need to display what the user drew on the panel, in the picture box (and that''s my problem)

my code is:

namespace myWhiteBoardTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static Boolean clck = false;
        public static int currentX;
        public static int currentY;
        public static int oldX;
        public static int oldY;
        Bitmap thePanelImg;
        Pen myP = new Pen(Color.Red, 3);
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            clck = true;
            oldX = e.X;
            oldY = e.Y;
        }
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            Graphics g = panel1.CreateGraphics();
            
            if (clck == true)
            {
                g.DrawLine(myP, oldX, oldY, e.X, e.Y);
                oldX = e.X;
                oldY = e.Y;
            }
        }
        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            clck = false;
            panel1.DrawToBitmap(thePanelImg, new Rectangle(0, 0, panel1.Width, panel1.Height));
            pictureBox1.Image = thePanelImg;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
          thePanelImg = new Bitmap(panel1.Width, panel1.Height);
        }





正在发生的事情是我只获得了图片框中面板的背景而没有绘制的图案



有人可以帮忙吗?



谢谢



what''s happening is that i''m getting only the background of the panel in the pictureBox without the drawn patterns

can anyone help?

thank you

推荐答案

天哪,你做错了!表单根本不会保留渲染图形。以下是该怎么做:



您需要在重写方法中执行所有绘图 OnPaint 或事件处理程序油漆。在这两种情况下,对于绘图,使用在事件参数参数中传递的 System.Drawing.Graphics 的实例。每次 WM_PAINT 都会调用您的渲染方法。例如,当您隐藏并再次显示窗体或某些其他图形掩盖您的图形时,会将Windows消息调度到您的窗口窗口,然后再次显示。永远不要为您的控件创建Graphics实例,始终使用event参数中的一个。事实上,有时你可以做到,但它是更先进的技术。在你的情况下,就是不要这样做。



做什么改变图形?您正在使用一些数据进行渲染。改变这些数据就是这样。下次 WM_PAINT 时,您的新数据将用于重新绘制。但如果没有其他任何触发,如何以编程方式触发此 WM_PAINT ?唯一有效的方法是在渲染图形的控件上调用 System.Windows.Forms.Control.Invalidate 。出于性能考虑,您可能希望使用带参数(矩形或区域)的 Invalidate 来仅使场景中真正需要更新的部分无效。



为避免闪烁,您还可能需要双缓冲选项,请参阅 Control.SetStyle 。它受到保护,因此在派生的 Control 类中也是如此。如果这是一个 Form ,你无论如何都是从 System.Windows.Forms.Form 派生出来的。



有关代码示例,请参阅: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpaint.aspx [ ^ ]。



为了以防万一,如果我错过了什么,请检查我过去的答案(基本上是相同的):

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

mdi子窗体之间的绘制线 [ ^ ]。







为了以防万一,更多参考我过去的答案:

如何从旧图纸中清除面板 [ ^ ],

画一个矩形le in C# [ ^ ],

附加一个图片框内的图片 [ ^ ];

mdi子表单之间的绘制线 [ ^ ],

在面板上捕获图形 [ ^ ],

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

如何加速我的vb.net应用程序? [ ^ ]。



-SA
Oh gosh, you do it all wrong! Forms do not persist rendered graphics at all. Here is what to do:

You need to do all your drawing in the overridden method OnPaint or event handler Paint. In both cases, for drawing, use the instance of the System.Drawing.Graphics passed in the event argument parameter. Your rendering method will be called each time WM_PAINT Windows message is dispatched to your window, for example, when you hide and show you form again or when your graphics is masked by some other window and later shown again. Never create an instance of Graphics for your control, always use the one from the event argument parameter. In fact, sometimes you can do it, but it''s much more advanced technique. In your case, just don''t do it.

What do to do change graphics? You''re using some data for rendering. Change this data, that''s it. Next time WM_PAINT comes, your new data will be used for re-drawing. But if nothing else is triggered, how to programmatically trigger this WM_PAINT? The only one valid way to do it is calling System.Windows.Forms.Control.Invalidate on the control where you render the graphics. For performance sake, you may want to use Invalidate with a parameter (Rectangle or Region) to invalidate only the part of the scene which really needs update.

To avoid flicker, you also may need double buffering options, see Control.SetStyle. It''s protected, so do it in the derived Control class. If this is a Form, you derive it from System.Windows.Forms.Form anyway.

For a code sample, see this: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpaint.aspx[^].

Just in case, if I missed something, check my past answers (essentially the same):
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^].



Just in case, some more references to my past answers:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^];
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my vb.net application?[^].

—SA


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

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