animating text and image in a deskband

Hi,

I have a deskband application, where I want an image and a text string to animate into the area, and the out again.

I tried doing this with simple GDI+ programming to draw an image and a string, and then animating the coordinates for that drawing using a Timer object set to about 50 milliseconds. This gave a terrific result, with the only drawback being that the CPU takes a lot of punishment. It's ok at first, but after a minute, the CPU usage rises to about 100%.

I use the DrawString() method to draw the string. This doesn't take any CPU at all. For the image (I use an .ico file) I use the DrawImage() method. This method takes a lot of cpu (but only after a while). It is important that it is alpha blended images.

Why do does DrawImage() take so much CPU, and why only after a while

Is there another way of doing it

Thanks
CrissCross



Answer this question

animating text and image in a deskband

  • Rab Lucas

    Hi,

    I use this code to draw the icon and string.

    'icon' is an Icon object instanciated earlier.
    'font' is a Font object, also instanciated earlier.

    'Y' is a float that I update to create the animation.

    I invoke this.Invalidate() to redraw the deskband every 20 milliseconds.

    protected override void OnPaintBackground ( PaintEventArgs e )
    {
    //--- Draw the theme background (Make the bar trasparent...)
    IntPtr hDC = e.Graphics.GetHdc();

    RECT rect = new RECT( e.ClipRectangle );

    DrawThemeParentBackground(
    this.Handle, hDC, ref rect );

    e.Graphics.ReleaseHdc( hDC );
    //---//

    e.Graphics.DrawIcon( icon, X, Y );
    e.Graphics.DrawString( "Some text", font,
    Brushes.White, X + 20F, Y + 2F );
    }

    This code causes the CPU to go up to almost 100% after a minute or so. I've used Windows.Forms.Timer, System.Timers.Timer and System.Threading.Timer to update. I also had a single Thread in a loop with a Sleep every 20 milliseconds.

    If I remove the DrawIcon method call, the CPU won't break a sweat. The icon used is alpha blended 16x16.

    Without the DrawIcon, I can have a whole bunch of DrawString calls after eachother, without affecting the CPU usage.

    This really confuses me... Should I try DirectX or is ther another simple way of doing it

    Regards
    CrissCross


  • Windrider67

    Hi. I'll do that tonight, when I get home from work.

    Thanks

    Chris


  • JumpyNET

    One thing I'm sure - DirectX won't be easier.

    Use System.Windows.Forms.Timer - this one runs in your thread, while others are more special (typically for server apps) and they can give you problems with multithreading. Calling Sleep() will freeze your UI thread and prevent repainting, this is not what you really need

    Really strange, you don't leave undisposed resources and CPU overloaded after a long time after. Probably bug in DrawIcon().

    Try to render inside Bitmap and use this bitmap instead of icon. If it's bug inside DrawIcon() - you will get a chance.

    Or you may try Win32 API DrawIcon() for rendering icons - that may help too.



  • Arjuna Chiththananda

    Hi!

    Sounds strange, I thought that DrawString() must be slower than DrawImage(), because it involves fonts.

    Since problem arise after a while - it's probably some unreleased resources, some kind of leak. Can you post source code for details



  • pab

    Hi again.

    I changed the images from .ico to .png and used DrawImage() instead of DrawIcon() .

    Works perfectly! I don't even think the CPU know that the application is there anymore =)

    Seems that using icon files is very slow.

    Thanks though

    Regards
    CrissCross


  • animating text and image in a deskband