[Last Call] Learn how to a build a cloud-first strategyRegister Now

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

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

  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?
0
e_livesay
Asked:
e_livesay
1 Solution
 
Bob LearnedCommented:
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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
May we see your PB_1 Paint() event?
0
 
bansidharCommented:
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.
0
 
e_livesayAuthor Commented:
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()
      {
         InitializeComponent();

         initialize_PB_2();
      }

      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);

         DrawInGraphic(g);

         // 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)
      {
         DrawInGraphic(e.Graphics);
      }

     
   }
}
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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