如何在C#中同时在表单上绘制多个图像......? [英] How Can I Draw Multiple Image On Form In C# At Same Time......?

查看:59
本文介绍了如何在C#中同时在表单上绘制多个图像......?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows窗体应用程序。我想同时在同一个表格上绘制多个图像。

我有两个相同间隔的定时器一个绘制标志,第二个绘制一个树。现在我的问题是两个图像画一个另一个,但我想在当时绘制两个图像...任何人都可以帮助我......我的代码是什么



I am using windows form application. I want to draw multiple image on a same form at the same time.
I have two timers with same interval one draws flag and second draws a tree.Now my problem is that the two image draw one after another but i want to draw both images at the time...Can anyone help me..here is my code

Graphics G=this.CreateGraphics();
Point F_P=new Point(10,20);
Point T_P=new point(100,200);

 private void Flag_tick(object sender, EventArgs e)
        {
drawflag();
}
public void drawflag()
{
G.DrawImage(flag,F_P)
}

private void Tree_tick(object sender, EventArgs e)
        {
drawtree();
}
public void drawtree()
{

G.DrawImage(Tree,T_P)
}

推荐答案

没有相同的东西时间。但是,显然你面临一些真正的问题,因为你做错了。不幸的是,你没有提供足够的信息来表明你是如何做到的,所以现在让我们忽略它。根据我的建议,您可以轻松确保视觉上正确的演示。



首先,您应该抛出通过计时器代码绘制任何内容的想法。相反,您应该只在重写的方法 System.Windows.Forms.Control.OnPaint 或事件的处理程序 System.Windows.Forms中呈现图形.Control.Paint 并使用传递给你的 System.Drawing.Graphics 实例作为参数。在这两种情况下,绘图都是由Windows消息 WM_PAINT 触发的。您的计时器,以及任何交互式事件处理程序,代码在您的UI线程以外的某个线程中,应该只使用 System.Windows.Forms.Control.Invalidate 来刺激重新绘制。绘图应该从一些数据中完成。您的不同计时器将更新数据(不要忘记使用 lock 语句来共享线程之间的数据结构),然后使视图无效。来自不同来源的两个不同的无效,如果它们在时间上接近,将仅合并一个失效(这是一个对你透明的相当复杂的机制;这里需要解释它太多了),所以你会看到同时更新视图。



有关详细信息,请参阅我过去的答案:

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

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

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



此外,请我特别注意过去的答案如何加快我的vb.net应用程序? [ ^ ]。



这个线程如何与上述相关?我会解释一下。首先,请确保您不使用计时器类型 System.Windows.Forms.Timer 。是的,几乎没有。更准确地说,如果您需要任何至少有一点接近周期性的动作,或者根本不需要任何精确计时的任何动作,则不能使用它。由于一些众所周知的原因,这个计时器完全不准确。其他计时器将在您的UI线程以外的某个线程中执行您的处理程序。因此,您将具有与明确使用其他线程相同的情况。此外,由于许多原因,明确地使用单独的线程比任何计时器更简单,更可靠。因此,建议你更喜欢线程。



但你不能从非UI线程中调用与UI相关的任何内容。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



-SA
There is no such thing as "same time". However, it's apparent that you face some real problem because you are doing it wrong. Unfortunately, you did not provide enough information on how you do it, so let's ignore it for now. With my advice, you can easily ensure visually correct presentation.

First of all, you should throw out the idea of drawing anything by the timer code. Instead, you should render graphics only in the overridden method System.Windows.Forms.Control.OnPaint or a handler of the event System.Windows.Forms.Control.Paint and use the instance of System.Drawing.Graphics passed to you as an argument. In both cases, drawing is triggered by the Windows message WM_PAINT. Your timer, as well as any interactive event handler, code in some thread other then your UI thread, should only stimulate re-drawing using System.Windows.Forms.Control.Invalidate. The drawing should be done from some data. Your different timers would update the data (don't forget using lock statement to share data structures between threads) and then invalidate the views. Two different invalidates from different sources, if they are close in time, will merge in just one invalidation (this is a rather complicated mechanism transparent to you; it would take too much to explain it here), so you would see "simultaneous" update of the view.

For further detail, please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].

Also, please pay special attention for my past answer How to speed up my vb.net application?[^].

How is that threading thing related to the above? I'll explain. First of all, make sure you don't use the timer type System.Windows.Forms.Timer. Yes, pretty much never. More exactly, you cannot use it if you need any action at least a bit close to periodic, or anything which requires any accuracy in timing at all. This timer is utterly inaccurate, by some well known reasons. Other timers will execute your handler in some thread other than your UI thread. So, you will have the same situation as with explicit use of some other thread. Moreover, by many reasons, using a separate thread explicitly is much more straightforward and more reliable than any timer. So, you are advised to prefer threads anyway.

But you cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于如何在C#中同时在表单上绘制多个图像......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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