Groups | Blog | Home
all groups > dotnet drawing api > july 2007 >

dotnet drawing api : How to clear the content of a PictureBox which has a BG colour set to white? (.NET 2.0)


nvx
7/31/2007 5:42:35 AM
Hello,
how can I permanently clear the content of a PictureBox which has a
background colour set to white? I have tried to do it the same way as
I do the painting (create a Bitmap, clear it with white colour and
draw it onto a Graphics on the PictureBox), but it does not work as I
expect it would.

The PictureBox is on a TabPage of a TabControl. After I try to clear
the content of the PictureBox, it becomes white, but if I switch to
another TabPage and return back, the previous image is there again.
The same thing happens when I click a menu item. It rolls down, covers
the PictureBox partially, and after it rolls up, the part of the
PictureBox that was covered contains the previous content. What do I
do wrong?

Sample code supposed to permanently clear the contents of a
PictureBox:

Bitmap bm = new Bitmap(pictureBox.Width, pictureBox.Height);
Graphics g = Graphics.FromImage(bm);
g.Clear(System.Drawing.Color.White);
Graphics pbg = pictureBox.CreateGraphics();
pbg.DrawImageUnscaled(bm, new Point(0, 0));
g.Dispose();


Thanks for any help...

Best regards
nvx
nvx
7/31/2007 6:27:08 AM
SOLVED... There was one wrong declaration in the code.

nvx
Bob Powell [MVP]
7/31/2007 4:00:20 PM
A picture box displays images. You don't need to draw the image onto it, you
just set the Picturebox's Image property to the image.

Bitmap bm=new Bitmap(...);
this.pictureBox1.Image=bm;

To clear the image from the picture box just simply set the Image parameter
to null.


--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


[quoted text, click to view]
AddThis Social Bookmark Button