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?
Clearing a bitmap
Collapse
X
-
Tags: None
-
I'm a C# guy, not VB sorry. But the process should be the same.Originally posted by WynandMurrayI 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?
Make a brush
give it properties of solid and white
fill your graphic object using the brush you defined.
Code:Image imgMatte = new Bitmap(100, 100); Rectangle FullSize = new Rectangle(0, 0, imgMatte.Width, imgMatte.Height); Graphics myGraphic = Graphics.FromImage(imgMatte); PaintEventArgs e = new PaintEventArgs(myGraphic, FullSize); SolidBrush MatteBrush = new SolidBrush(Color.Black); e.Graphics.FillRectangle(MatteBrush, e.ClipRectangle);Comment
-
Yes. However, if I now again attempt to write to the bitmap an exception occurs.Originally posted by kenobewanHave you tried bitmap.dispose( )?Comment
-
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.Originally posted by kenobewanNot 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.Comment
-
The OP states:Originally posted by kenobewanI agree that the sentence, , is ambiguous. What do you mean by clear?
So by "clear" I think the OP means to repaint the bitmap in all white so as to start on a fresh canvas.Originally posted by WynandMurrayor is there a fast way of copying another (white) bitmap of the same size into my bitmap?
Moderator Plater gave the following tip:
Originally posted by PlaterCode:myGraphic.Clear(Color.White);
Code:if (this.Horse == Dead) { Beat(); }
Regards,
tlhIn'toQComment
-
Comment
-
"clear" means to reset all the pixels to white.Originally posted by kenobewanI agree that the sentence, , is ambiguous. What do you mean by clear?
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.Comment
-
Originally posted by WynandMurray"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.Comment
-
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!Originally posted by tlhintoqDon'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.Comment
Comment