Solved

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

Posted on 2011-03-09
4
1,140 Views
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?
0
Comment
Question by:e_livesay
4 Comments
 
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.
0
 
LVL 85

Expert Comment

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

Accepted Solution

by:
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.
0
 

Author Closing Comment

by:e_livesay
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()
      {
         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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Duplicate a row 2 31
String manipulation 15 49
C# Error - Add Failed 12 38
Loop through Multiple Processes Async 2 17
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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 video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

18 Experts available now in Live!

Get 1:1 Help Now