473,414 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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
Sep 15 '07 #1
3 2197
Hi Jon,

It is considered bad practice to draw onto the graphics object outside the paint events, and since you do it asyncronously as well I wonder what Bob Powell would say (if he doesn't read this thread, try posting in the Drawing newsgroup). You are locking the bitmaps and form and aim for a 30 frames per second drawing. I suspect the flickering is because your code cannot accomplish this all the time. Having your form doublebuffered may not help if you don't use the Paint events. Not sure on this.

Furthermore, your code could easily be accomplished by creating a bitmap for the calculator program and have Paint draw this to the screen whenever necessary (for instance by calling Invalidate()).

To get rid of the GDI+ error when closing, stop the paint timer on your closing event. You should also dispose FormGraphics before you leave.

Try dropping all references to FormGraphics or this.CreateGraphics(). Have the timer event call Invalidate() and put the drawing of the bitmap inside the Paint event (you still need to lock the bitmap or the paint will work, but with black bitmaps whenever the calculator locks it). This method does not require you to stop the painter when you close either.

--
Happy coding!
Morten Wennevik [C# MVP]
Sep 15 '07 #2

"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.tyomgqxbdj93y5@ubuan...
Hi Jon,

It is considered bad practice to draw onto the graphics object outside the
paint events, and since you do it asyncronously as well I wonder what Bob
Powell would say (if he doesn't read this thread, try posting in the
Drawing newsgroup). You are locking the bitmaps and form and aim for a 30
frames per second drawing. I suspect the flickering is because your code
cannot accomplish this all the time. Having your form doublebuffered may
not help if you don't use the Paint events. Not sure on this.

Furthermore, your code could easily be accomplished by creating a bitmap
for the calculator program and have Paint draw this to the screen whenever
necessary (for instance by calling Invalidate()).

To get rid of the GDI+ error when closing, stop the paint timer on your
closing event. You should also dispose FormGraphics before you leave.

Try dropping all references to FormGraphics or this.CreateGraphics(). Have
the timer event call Invalidate() and put the drawing of the bitmap inside
the Paint event (you still need to lock the bitmap or the paint will work,
but with black bitmaps whenever the calculator locks it). This method
does not require you to stop the painter when you close either.

Ok, I did all this and I think its working. Basically removed all form
graphics and call invalidate inside the timer and draw in the onPaint. I
tried something like this before but it didn't work but I suppose I screwed
up somehwere. (I think I was calling refresh or something... I forgot about
invalidate ;/)

Thanks,
Jon
Sep 15 '07 #3
Whats strange is that setting

this.DoubleBuffered = true;

on my form seems to speed up the rendering by about 10x. I still get
shearing problems but I guess I would need to sync with the vrefresh to fix
that?

Thanks again,

Jon
Sep 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Shoaib | last post by:
I have an IFRAME in HTML page whose source is set to some xml page.XML page gets displayed properly. But when i open some other IFRAME on top that IFRAME displaying xml, there is lot of flickering...
6
by: Joaquin Grech | last post by:
Hi I did alot of research on this on the web and msdn and I couldn't find anything. I have a listview showing as a grid (table looking, with rows and columns and no images at all, only text)....
1
by: Jack Smash | last post by:
Hi, I'm using a UserControl object for all the graphics handling in my application. However, if I select an image and move it around, I get flickering. So in order to get rid of the flickering,...
2
by: John Lee | last post by:
Hi, I have a windows application that uses the listview to display about 50 items in detailed view - 4 columns. The first column is static and other columns will be updated in 100-1000ms - it...
2
by: John Lee | last post by:
Thanks Jay for your response. I tried your code and it still flickering a lot. To demonstrate it, you can grab a listview, create 3 columns - name, value, timestamp, in form_load event to add 50...
8
by: benben | last post by:
I created a form and overrided OnPaint, OnClick and OnResize methods. My OnPaint calls base.OnPaint then repaints the whole screen. The screen flickers a lot! It didn't happen when the app was...
0
by: Imran | last post by:
Hi , I have an mdi applicaton. When user switch between child forms they are flickering like crazy! When a Child Form loads, you can see it in it's original size (the size from Design-time) then...
5
by: n00b | last post by:
I have some forms with maybe around 30 controls on each and anytime I load these forms different parts of it start flickering though not all of them at once and not the same ones everytime. the...
6
by: Mark Thompson | last post by:
I have a problem with my Visual Basic .NET 2003 application which I have so far been unable to resolve. I am trying to get an OpenGL control that I have created working properly as a control on...
1
by: sj | last post by:
Flickering Subform! I am developing a Quotation system in Access 03. At entry, users enter data thru' a form with subform. However, the subform keep flicker when I run the system on computer...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.