J
Jon Slaughter
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
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