Flickering



I always get flicking in my test code and all graphics code I'm using. It's
random but happens about once a second. Its quite annoying cause I expected
that with double buffering enabled and the fact that I'm drawing everythign
to my own bitmap that there would be no way it could flicker. (even in a
stationary scene)


I'm sure that its a programming issue... probably having to do with locking.
I figure that if I lock the bitmap that I can draw on it without the painter
using it but maybe thats not how it works?

I also get a generic GDI+ error on the painter when closing down. I have no
clue why but again, its probably some programming issue.

Thanks,
Jon

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Threading;


namespace GDITest2
{
public partial class Form1 : System.Windows.Forms.Form
{

System.Timers.Timer painter;
System.Threading.Thread thread;
Bitmap bitmap;

Graphics FormGraphics;
Graphics BitmapGraphics;


public Form1()
{
FormGraphics = this.CreateGraphics();
//FormGraphics.SmoothingMode = SmoothingMode.HighQuality;
//FormGraphics.InterpolationMode =
InterpolationMode.HighQualityBicubic;
//FormGraphics.CompositingQuality = CompositingQuality.HighQuality;
this.TopMost = true;
this.BringToFront();
this.Show();
this.TopMost = false;
this.BringToFront();
this.Show();
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint, true);
this.ClientSize = new System.Drawing.Size(700, 700);
this.Left = (1240 - this.ClientSize.Width) / 2;
this.Top = (1024 - this.ClientSize.Height) / 2;

thread.Start();
painter.Start();
}


protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
FormGraphics = this.CreateGraphics();
bitmap = new Bitmap(this.Width, this.Height);
BitmapGraphics = Graphics.FromImage(bitmap);

ThreadStart ts = new ThreadStart(calculator);
thread = new Thread(ts);

painter = new System.Timers.Timer(1000 / 30F);
painter.Elapsed += new
System.Timers.ElapsedEventHandler(painter_Elapsed);

}



void painter_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{

lock (FormGraphics)
{
lock (bitmap)
{
try
{
FormGraphics.DrawImageUnscaled(bitmap, 0, 0);
}
catch {}
}
}
}



protected override void OnPaintBackground(PaintEventArgs e) { }
protected override void OnPaint(PaintEventArgs e) { }

protected override void OnResize(EventArgs e)
{

base.OnResize(e);
lock (FormGraphics)
{
FormGraphics.Dispose();
FormGraphics = this.CreateGraphics();
lock (bitmap)
{
lock (BitmapGraphics)
{
bitmap = new Bitmap(this.Width, this.Height);
BitmapGraphics = Graphics.FromImage(bitmap);
}
}
}
}

bool closing = false;
protected override void OnClosing(System.ComponentModel.CancelEventArgs
e)
{
base.OnClosing(e);
closing = true;
}


double t = 0;
void calculator()
{
t = 0;
double dt = 0.001;
BitmapGraphics.ScaleTransform(2F, 2F);
while (!closing)
{
t += dt;
lock (bitmap)
{
BitmapGraphics.Clear(Color.Black);

//BitmapGraphics.TranslateTransform(1, -1);
BitmapGraphics.FillRectangle(Brushes.Beige, 120, 120,
30, 30);
}
Thread.Sleep(10);
} // While
}
} // Form
} // Namespace


.



Relevant Pages

  • Re: 2.6.17-rc6-mm1 -- BUG: possible circular locking deadlock detected!
    ... What happened is that the rwlock_init() 'lock type keys' ... for the two runlists were 'merged': ... lcnbmp_lock locks accesses to the cluster bitmap (i.e. the physical blocks ... The locks are taken when a cluster is being allocated / an mft record is ...
    (Linux-Kernel)
  • Re: [RFC][PATCH] Reduce ext3 allocate-with-reservation lock latencies
    ... Maybe that lock is rarely ... I will try it on a relatively full filesystem(where bitmap ... > scan to find a free block takes time), and on a 4 cpu box with many ... overhead of drop/re-grab lock and extra window search/link/unlink ...
    (Linux-Kernel)
  • Re: Having trouble with multiple threads and a shared resource
    ... I read that you should use a static private object as a lock, ... access between the threads to the bitmap. ... private void _replayPathTaken_Click ... HighlightSelectedCells method need access to MyBitmap and sometimes they ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Gradient fill a bitmap
    ... You can accomplish this effect by creating a bitmap of the same size as the original image and drawing a gradient fill onto it. ... Find great Windows Forms articles in Windows Forms Tips and Tricks ... protected override void OnPaintBackground ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: SetPixel() / GetPixel() for Format64bppArgb in VB using LockBits
    ... I didn't know that one can lock just some parts of the bitmap. ... So you mean if one has to set 1 pixel, ... Find great Windows Forms articles in Windows Forms Tips and Tricks ...
    (microsoft.public.dotnet.framework.drawing)