Hi, I'm having a problem with a motion tracing effect in an animation on a Windows Form in C#. The effect generated by painting a transparent black background over the Form each time it is redrawn. The idea is that, as an object moves, it's previous images should be gradually covered up and eventually disappear, leaving a motion trail. However, the trail never seems to disappear completely, as if the multiple transparent repaints never composite to 100%.

I used to programme in Objective-C on the OS X operating system and never had this problem. Then I switched to Java to make my application available on other platforms and still had no problem. However, last year I moved to a PC using Windows Vista; when I recompiled my Java application the problem appeared. I've tried looking everywhere to find a solution, but without success. More recently, I've been learning C#, and though I would have another go, but the problem is still there, leading me to think this may have something to do with Vista itself.

I've pasted a simplified version of the code below. Does anyone know how to solve this problem? It's been bugging me for a year now, and there's still no solution in sight.

Cheers,
Simon

// Timer event to repaint the screen
private void TimerEvent(object sender, System.Timers.ElapsedEventArgs e)
        {
            screen.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs paintEvnt)
        {
            this.DoubleBuffered = true;

            // Image is drawn to a Bitmap then to the Form
            // The Bitmap variable, buffer, has previously been declared and initialised:
            // Bitmap buffer = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            Graphics g = Graphics.FromImage(buffer);

            // I tried various Compositing properties, but they made no difference
            g.CompositingMode = CompositingMode.SourceOver;
            g.CompositingQuality = CompositingQuality.HighQuality;

            // Screen dimensions
            screenWidth = this.Width;
            screenHeight = this.Height;

            // Background black set to 5% transparent
            // Theoretically should accumulate to 100% after 20 repaints
            // But this never happens
            SolidBrush backgroundBrush = new SolidBrush(Color.FromArgb(13, 0, 0, 0));
            // Opaque brush for painting the object
            Color colour = Color.FromArgb(255, 255, 255);
            SolidBrush objectBrush = new SolidBrush(colour);

            
            double x = (Math.Sin(time / 1000.0) + 1.0) * 0.5;

            // Draw the background over the previous image
            g.FillRectangle(backgroundBrush, 0, 0, screenWidth, screenHeight);
            // Then draw the object over the top
            g.FillRectangle(objectBrush, (int)(x * screenWidth), (int)(x * screenHeight), 10, 10);
 
            // Paint the Form
            paintEvnt.Graphics.DrawImageUnscaled(buffer, 0, 0);

            // time is an integer variable used to animate the image
            ++time;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.