Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 634
  • Last Modified:

Winform Image Flickering

Hi,
When I am loading the form the image which I draw on Paint is flickering. How do I avoid this?

 void Initial_Paint(object sender, System.EventArgs e)
 {
            Image newImage = Image.FromFile(ConfigurationManager.AppSettings["InitialFormImage"]);
            Graphics g = this.CreateGraphics();
            g.DrawImage(newImage, new Rectangle(new Point(CommonFunctions.GetWidth(60), CommonFunctions.GetHeight(100)), new Size(CommonFunctions.GetWidth(660), CommonFunctions.GetHeight(450))));
        }
0
AdibRahman77
Asked:
AdibRahman77
  • 3
1 Solution
 
Joel CoehoornDirector of Information TechnologyCommented:
You need a separate graphics object that you draw to, and then replace the entire image (or at least, the entire portion that changed) at once.

Google "double buffering" for more information.  The third link down is .Net specific and indicates .Net may have a feature that does most of the work for you.
0
 
Joel CoehoornDirector of Information TechnologyCommented:
Hmm.. the sixth link down is even better.
0
 
Joel CoehoornDirector of Information TechnologyCommented:
0
 
wizrrCommented:
And ofcourse you should not load that image every time OnPaint occurs. Hmm. Now i see that Initial_Paint is not paintevent, but you drawing from that method. You should draw only from OnPaint. Do not paint from another methods - this is OS rule, if you paint from another methods this is possible that OS will redraw (clean) all things you painted - that is why all can flicker.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now