Clearing a bitmap

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • WynandMurray
    New Member
    • Oct 2008
    • 4

    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?
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Have you tried bitmap.dispose( )?

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Use the Graphics object? It has a .Clear(Color) function

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Originally posted by WynandMurray
        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.

        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

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          If you are doing this:
          Graphics myGraphic = Graphics.FromIm age(imgMatte);

          You can just do this:
          myGraphic.Clear (Color.White);

          Comment

          • WynandMurray
            New Member
            • Oct 2008
            • 4

            #6
            Originally posted by kenobewan
            Have you tried bitmap.dispose( )?
            Yes. However, if I now again attempt to write to the bitmap an exception occurs.

            Comment

            • kenobewan
              Recognized Expert Specialist
              • Dec 2006
              • 4871

              #7
              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.

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                Originally posted by kenobewan
                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.

                Comment

                • kenobewan
                  Recognized Expert Specialist
                  • Dec 2006
                  • 4871

                  #9
                  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?

                  Comment

                  • tlhintoq
                    Recognized Expert Specialist
                    • Mar 2008
                    • 3532

                    #10
                    Originally posted by kenobewan
                    I agree that the sentence, , is ambiguous. What do you mean by clear?
                    The OP states:

                    Originally posted by WynandMurray
                    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:

                    Originally posted by Plater
                    Code:
                    myGraphic.Clear(Color.White);
                    Code:
                    if (this.Horse == Dead)
                    {
                         Beat();
                    }

                    Regards,
                    tlhIn'toQ

                    Comment

                    • JamieHowarth0
                      Recognized Expert Contributor
                      • May 2007
                      • 537

                      #11
                      Originally posted by tlhintoq
                      Code:
                      if (this.Horse == Dead)
                      {
                           Beat();
                      }
                      LMAO XD

                      codegecko

                      Comment

                      • WynandMurray
                        New Member
                        • Oct 2008
                        • 4

                        #12
                        Originally posted by kenobewan
                        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.

                        Comment

                        • tlhintoq
                          Recognized Expert Specialist
                          • Mar 2008
                          • 3532

                          #13
                          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

                          • WynandMurray
                            New Member
                            • Oct 2008
                            • 4

                            #14
                            Originally posted by tlhintoq
                            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!

                            Comment

                            Working...