469,286 Members | 2,522 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,286 developers. It's quick & easy.

Clearing a bitmap

I am a beginner. I need a fast way to clear a bitmap in VB.NET. (Looping around and setting pixel colours to white is too slow). Is there a simple instruction to do this, or is there a fast way of copying another (white) bitmap of the same size into my bitmap?
Oct 31 '08 #1
13 11574
kenobewan
4,871 Expert 4TB
Have you tried bitmap.dispose()?
Oct 31 '08 #2
Plater
7,872 Expert 4TB
Use the Graphics object? It has a .Clear(Color) function
Oct 31 '08 #3
tlhintoq
3,525 Expert 2GB
I am a beginner. I need a fast way to clear a bitmap in VB.NET. (Looping around and setting pixel colours to white is too slow). Is there a simple instruction to do this, or is there a fast way of copying another (white) bitmap of the same size into my bitmap?
I'm a C# guy, not VB sorry. But the process should be the same.

Make a brush
give it properties of solid and white
fill your graphic object using the brush you defined.

Expand|Select|Wrap|Line Numbers
  1.                 Image imgMatte = new Bitmap(100, 100);
  2.                 Rectangle FullSize = new Rectangle(0, 0, imgMatte.Width, imgMatte.Height);
  3.                 Graphics myGraphic = Graphics.FromImage(imgMatte);
  4.                 PaintEventArgs e = new PaintEventArgs(myGraphic, FullSize);
  5.                 SolidBrush MatteBrush = new SolidBrush(Color.Black);
  6.                 e.Graphics.FillRectangle(MatteBrush, e.ClipRectangle);
  7.  
Oct 31 '08 #4
Plater
7,872 Expert 4TB
If you are doing this:
Graphics myGraphic = Graphics.FromImage(imgMatte);

You can just do this:
myGraphic.Clear(Color.White);
Oct 31 '08 #5
Have you tried bitmap.dispose()?
Yes. However, if I now again attempt to write to the bitmap an exception occurs.
Oct 31 '08 #6
kenobewan
4,871 Expert 4TB
Not sure why you would want to write to a bitmap after disposing it? There are two other suggestions by experts, suggest try and let us know the result.
Nov 1 '08 #7
tlhintoq
3,525 Expert 2GB
Not sure why you would want to write to a bitmap after disposing it? There are two other suggestions by experts, suggest try and let us know the result.
Disposing of the bitmap was never part of the OP question. It was a suggestion by someone else. The original question was to clear it (to a color). Which as you point out has been addressed a couple time.
Nov 2 '08 #8
kenobewan
4,871 Expert 4TB
I agree that the sentence,
I need a fast way to clear a bitmap in VB.NET. (Looping around and setting pixel colours to white is too slow).
, is ambiguous. What do you mean by clear?
Nov 2 '08 #9
tlhintoq
3,525 Expert 2GB
I agree that the sentence, , is ambiguous. What do you mean by clear?
The OP states:

or is there a fast way of copying another (white) bitmap of the same size into my bitmap?
So by "clear" I think the OP means to repaint the bitmap in all white so as to start on a fresh canvas.

Moderator Plater gave the following tip:

Expand|Select|Wrap|Line Numbers
  1. myGraphic.Clear(Color.White);
Expand|Select|Wrap|Line Numbers
  1. if (this.Horse == Dead)
  2. {
  3.      Beat();
  4. }
  5.  

Regards,
tlhIn'toQ
Nov 2 '08 #10
JamieHowarth0
533 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. if (this.Horse == Dead)
  2. {
  3.      Beat();
  4. }
  5.  
LMAO XD

codegecko
Nov 2 '08 #11
I agree that the sentence, , is ambiguous. What do you mean by clear?
"clear" means to reset all the pixels to white.
I repeatedly want to create a different graph, eg y=mx. Each time I calculate the y-value for each x-value and set the bitmap pixels x,y to black. When the graph has been completed I then display it on a picturebox. To plot a new graph I must first "clear" the bitmap to save a new set of x,y. The bitmap is quite large and to "clear" it pixel by pixel takes a long time.
Nov 13 '08 #12
tlhintoq
3,525 Expert 2GB
"clear" means to reset all the pixels to white.
I repeatedly want to create a different graph, eg y=mx. Each time I calculate the y-value for each x-value and set the bitmap pixels x,y to black. When the graph has been completed I then display it on a picturebox. To plot a new graph I must first "clear" the bitmap to save a new set of x,y. The bitmap is quite large and to "clear" it pixel by pixel takes a long time.

Don't consider that you have to erase the old graphic.
Just make the new bitmap and put it in the picture box.
It replaces the old graphic.
Manually clearing the picturebox is an unneeded step.
Nov 13 '08 #13
Don't consider that you have to erase the old graphic.
Just make the new bitmap and put it in the picture box.
It replaces the old graphic.
Manually clearing the picturebox is an unneeded step.
Yes, I do just that, but to recreate the bitmap I first have to "clear" it before I can set a new sequence of pixels in the bitmap, thus creating a new graph, ready for display in the picturebox. I do not have a problem in displaying the bitmap in the picturebox, or in clearing the picturebox, this is vitually instantaneous. The delay comes with clearing the bitmap pixel by pixel. I really appreciate your responses!
Nov 13 '08 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by ljlevend | last post: by
8 posts views Thread by Nathan Sokalski | last post: by
1 post views Thread by asdf | last post: by
7 posts views Thread by Fir5tSight | last post: by
6 posts views Thread by \Frank\ | last post: by
2 posts views Thread by Peter Oliphant | last post: by
5 posts views Thread by =?Utf-8?B?QVRU?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.