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

Posted on 2011-03-09
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

Expert Comment

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

Accepted Solution

bansidhar earned 500 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

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 (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

719 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