How can I stop a PictureBox Image from becoming distorted when another window is moved over it?

Posted on 2011-03-09
Last Modified: 2013-12-17
  I have written a .NET 2.0 C# program (Microsoft Visual Studio 2008) that has a PictureBox (named PB_1) on a form.  I have entered code into the Paint event for the PictureBox (i.e., into PB_1_Paint(...)) that draws an image into the PictureBox by using the Graphics property of the PaintEventArgs object that is passed to the Paint event handler (i.e., e.Graphics) by using e.Graphics.DrawRectangle(...).  The rectangle is drawn without any problem.  However, if I move another window over the PictureBox then the drawing in the PictureBox becomes distorted.
   As a temporary fix to this I added code to the ResizeEnd event handler for the form that does nothing but invalidate the PictureBox by calling PB_1.Invalidate().  Thus, when the drawing in the PictureBox becomes distorted I simply grab the lower-right corner of the form and adjust the size of the form slightly which causes the drawing in the PictureBox to be redrawn.
   How can I fix this?  That is, how can I make sure that the drawing in the PictureBox does not become distorted when another form is moved over my form?
Question by:e_livesay
LVL 96

Expert Comment

by:Bob Learned
ID: 35090950
I would start by setting the Form.DoubleBuffering to true, and see if that helps the PictureBox.  Another approach, is to create custom PictureBox control, that is set to double buffer.  That usually helps to alleviate flickering and painting problems.
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35091071
May we see your PB_1 Paint() event?
LVL 11

Accepted Solution

bansidhar earned 125 total points
ID: 35091091
you should do some things to stop flickering. because i don't know the exact situation i can give some items for you to try. easiest first

1. instead of using paint method create a routine to draw image and use the image as the "Image" property of the picture box. this way windows handles all the issues of keeping the picture clean.

2 use double buffering.

important: never draw directly to the picturebox. draw to an image in memory and apply it to the picture box whenever the picture needs to be changed. this way the picture will be steady just like it is a static picture added at design time. when i learned to draw every tutorial told me to use the paint method, but learned the hard way that you have not much control when to draw and when it is becomes dirty.

Author Closing Comment

ID: 35097729
Thank you all for your help.

My problem was solved by drawing my image in a routine outside of the Paint Event Handler of the PictureBox and then setting the Image property of the PictureBox to that Image (I have a user control (UC) in the program that spawned this problem and I did not have to set the DoubleBuffered property of that UC to true to fix this problem).  There is C# code below that illustrates how the problem was solved (see initialize_PB_2()).  That code is for a form that has 2 PictureBoxes named PB_1 and PB_2.  The original method that I was using is illustrated by PB_1_Paint(...).  Note that NO distortion was seen with the example below.  After further testing I realized that my initial problem was desribed incorrectly.  I should have said that the distortion occurred when I drew multiple rectangles, triangles, text, etc. NOT when I drew a single square (this is the reason for the comment "// In the 'real' code, of which this is a mock-up, there is code here that takes 'awhile' to perform." in the routine DrawInGraphic(...)).  Thus, the below code does not show the distortion but I'm submitting it here anyway for anyone that needs to draw an image and assign that Image to a PictureBox.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PictureBoxExample
   public partial class Form1 : Form
      public Form1()


      private void initialize_PB_2()
         // Create an image.
         Bitmap   b = new Bitmap(PB_2.Width, PB_2.Height);
         Graphics g = Graphics.FromImage(b);

         Rectangle r = new Rectangle(0, 0, PB_2.Width, PB_2.Height);
         g.Clip      = new Region(r);

         g.FillRectangle(Brushes.White, r);


         // Set the image of the PictureBox to the image created above.
         PB_2.Image = b;

      private void DrawInGraphic(Graphics g)
         // In the 'real' code, of which this is a mock-up, there is code here that takes 'awhile' to perform.

         int w       = (int)  PB_1.Width        / 2;  
         int w_wing  = (int) (PB_1.Width  - w)  / 2;
         int h       = (int)  PB_1.Height       / 2;
         int x_0     = (int)((PB_1.Width  - w)  / 2);
         int y_0     = (int)((PB_1.Height - h)  / 2);

         Rectangle r;
         // === Left-hand rectangle ===
         r = new Rectangle(new Point(0, y_0), new Size(w_wing, h));

         g.FillRectangle(new SolidBrush(Color.LightGreen ), r);
         g.DrawRectangle(new Pen       (Color.Black      ), r);

         // === Middle rectangle ===
         r = new Rectangle(new Point(x_0, y_0), new Size(w, h));

         g.FillRectangle(new SolidBrush(Color.Orange     ), r);
         g.DrawRectangle(new Pen       (Color.Black      ), r);

      private void PB_1_Paint(object sender, PaintEventArgs e)


Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now