GDI +对大型简单对象的显示速度较慢 [英] GDI+ slow on large simple objects

查看:152
本文介绍了GDI +对大型简单对象的显示速度较慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位编码员!

我遇到了GDI +问题.我正在.NET Framework 3.5上编码C#.我遇到的问题是在使用自己开发的图形工具进行绘图时,应用程序的响应速度很慢.我正在通过控件的MouseMove事件和控件上的CreateGraphics()方法进行绘制,但是首先是在我的双缓冲图像上进行绘制.当我在小矩形上使用工具时,效果很好,但是当我增加尺寸时,使用计算机上的2个屏幕,以fps为单位的图形会降低到小于1.

像我想做的那样正确的绘画方法是什么?我已经尝试过Invalidate方法来提高性能,但是没有运气.当然,如果不使用doublebuffering的话,问题就更少了,但是它忽隐忽现,我还遇到其他问题,例如在绘制时擦除临时矩形,这样就不会留下痕迹.

这是我解决这个问题的视频.
质量不是很好,但是您仍然可以很清楚地看到问题,绘图始终位于鼠标后面,而不像在其他程序中一样,绘图似乎是即时的.

http://screencast.com/t/ZDY3ZmRkM2I

抱歉,我已经尝试过您的解决方案,但响应速度仍然很慢.我做了一个小的测试表来尝试这个,甚至遇到了我遇到的这个问题.你们两个都没有提到如何进行绘制,因此我通过Invalidate()进行了假设?

这是代码:

Hi there fellow coders!

I''ve ran into a GDI+ problem. I''m coding C# on .NET Framework 3.5. The problem I''m having is slow response in my application when drawing with my own-developed graphics tools. I''m drawing via the MouseMove events of a Control and the CreateGraphics() method on the Control, but first on my doublebuffer Image. It works great when I use the tools on small rectangles but when I increase the size the drawing goes down in fps to less than 1, using 2 screens on my computer.

What is the proper way to draw like im trying to do? I''ve tried Invalidate methods to improve performance with no luck. The problem if of course less when not doublebuffering, but then it is flickering and I get other problems like erasing the temporary rectangle when drawing so it don''t leaves traces.

This is a video I made of the problem.
The quality isn''t that good but you can still see the problem quite clear, the drawing is always behind the mouse, not like in other programs where the drawing seems instant.

http://screencast.com/t/ZDY3ZmRkM2I

Im sorry, I''ve tried your solutions but it still is slow in response. I made a small testform to try this and even that suffers from this problem I''m having. Neither of you mentioned how to make the drawing happen so I assumed via Invalidate() ?

Here is the code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.SetStyle(
              ControlStyles.AllPaintingInWmPaint |
              ControlStyles.UserPaint |
              ControlStyles.DoubleBuffer, true);

    }

    Point down = Point.Empty;
    Point now = Point.Empty;

    Pen b = Pens.Black.Clone() as Pen;

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        if (down != Point.Empty)
            e.Graphics.DrawRectangle(b, down.X, down.Y, now.X - down.X, now.Y - down.Y);
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        down = e.Location;
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        down = Point.Empty;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        now = e.Location;
        if(down != Point.Empty)
            this.Invalidate();
    }
}



当表格较小时,响应正常,但放大时为香蕉.我对此的下一个想法是,是否有可能在不继续使用XNA或其他加速的情况下获得比这更好的结果?

非常感谢您的努力和提示,快速的响应!



When the form is small, the response is ok, but when enlarged it goes bananas. My next thought on this is, is it even possible to get it better than this without moving on to XNA or something else with acceleration?

Appreciate your efforts and tips very much, and the fast responds!

推荐答案

Elof Wecksell写道:
Elof Wecksell wrote:

和控件上的CreateGraphics()方法,但首先在我的双缓冲图像上

and the CreateGraphics() method on the Control, but first on my doublebuffer Image



两个坏举动.首先,使用Paint事件,其次告诉Winforms为您加倍缓冲.



Two bad moves. First, use the Paint event, second tell Winforms to double buffer for you.

this.SetStyle( 

  ControlStyles.AllPaintingInWmPaint | 

  ControlStyles.UserPaint | 

  ControlStyles.DoubleBuffer,true);


您似乎正在创建绘图程序.您的响应时间如此之差的原因是,您的设计存在严重缺陷.不必使用CreateGraphics,因为它不会在最小化或调整大小时持久保存更改.

我不久前创建了一个类似这样的程序,它的工作原理如下:
It looks like you''re creating a drawing program. The reason you''re having such bad response times, is that your design is horribly flawed. CreateGraphics shouldn''t need to be used, because it doesn''t persist changes on minimise or resize.

I created a program somewhat like this a while ago, and it worked something like this:

  1. 用户单击并拖动.调用MouseDown事件并存储当前位置,并创建一个新的DrawableRectangle实例并将其添加到列表中.当用户移动鼠标时,DrawableRectangle实例被更改(偏移量的计算,就像您现在正在做的那样)
  2. 在Paint事件中,遍历每个DrawableRectangle并绘制它们


您可以通过继承使其更复杂,并通过使用堆栈而不是列表来支持撤消操作


You could make this more complex through inheritance, and support Undo through the use of a Stack instead of a list


Elof Wecksell写道:
Elof Wecksell wrote:

对不起,我已经尝试了您的解决方案,但响应速度仍然很慢.

Im sorry, I''ve tried your solutions but it still is slow in response.



然后,您可能已经达到GDI +的极限,需要使用XNA等,是的.您可以尝试使用WPF,它可以免费提供DirectX.



Then you''ve probably hit the limits of GDI+ and need to use XNA, etc, yes. you could try using WPF, that gives you DirectX for free, kind of.


这篇关于GDI +对大型简单对象的显示速度较慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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