Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Bitmap Image to Binary Image (Black & White) [modified]

Bitmap Image to Binary Image (Black & White) [modified]

Scheduled Pinned Locked Moved C#
comgraphicsdata-structureshelptutorial
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A ant damage

    Hello, I need to get each pixel (as 1's and 0's) and package them as bytes. For example, I have a bitmap with a resolution of 64x64 pixels and convert it into an array of 512 bytes. Sorry for the bad english EDIT: I did forgot something. The solution for my problem was in this article[^].

    modified on Saturday, April 24, 2010 10:12 AM

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #2

    If it is that small, then why not just use Bitmap.GetPixel? If you are doing a lot of them, or they are bigger you may have to directly access the bitmap bytes to save processing time.

    You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    A 1 Reply Last reply
    0
    • OriginalGriffO OriginalGriff

      If it is that small, then why not just use Bitmap.GetPixel? If you are doing a lot of them, or they are bigger you may have to directly access the bitmap bytes to save processing time.

      You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

      A Offline
      A Offline
      ant damage
      wrote on last edited by
      #3

      How to access directly to the bytes of the bitmap? Through the Bitmap.LockBits() method? The purpose of this conversion is to create binary images for use in programs for the Casio fx-9860G (graphic calculator)

      OriginalGriffO 1 Reply Last reply
      0
      • A ant damage

        How to access directly to the bytes of the bitmap? Through the Bitmap.LockBits() method? The purpose of this conversion is to create binary images for use in programs for the Casio fx-9860G (graphic calculator)

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #4

        Yes. If they are black and white, then specify the System.Drawing.Imaging.PixelFormat.Format1bppIndexed mode.

        You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        A 1 Reply Last reply
        0
        • A ant damage

          Hello, I need to get each pixel (as 1's and 0's) and package them as bytes. For example, I have a bitmap with a resolution of 64x64 pixels and convert it into an array of 512 bytes. Sorry for the bad english EDIT: I did forgot something. The solution for my problem was in this article[^].

          modified on Saturday, April 24, 2010 10:12 AM

          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #5

          I recall that someone has an article somewhere that has instructions that can do this very quickly. But I can't seem to recollect who that was. Painless yet unsafe grayscale conversion in C#[^] BTW, your English is fine.

          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

          L 1 Reply Last reply
          0
          • E Ennis Ray Lynch Jr

            I recall that someone has an article somewhere that has instructions that can do this very quickly. But I can't seem to recollect who that was. Painless yet unsafe grayscale conversion in C#[^] BTW, your English is fine.

            Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #6

            Your memory is completely fading now, he wants binary, not grayscale. :-D

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


            E 1 Reply Last reply
            0
            • L Luc Pattyn

              Your memory is completely fading now, he wants binary, not grayscale. :-D

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


              E Offline
              E Offline
              Ennis Ray Lynch Jr
              wrote on last edited by
              #7

              My memory may be failing but last time I checked that article provided a fast way to get all pixel information as well as write out said pixel information. The actual grayscale conversion is trivial when compared to the rest of the code. And if I recall the question, it was how can I quickly ... which the article addresses. As for binary image that is an exercise left to the reader. But then again, if you keep the gray scale logic you can then apply your threshold statement and get the binary result as well.

              Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

              L 1 Reply Last reply
              0
              • E Ennis Ray Lynch Jr

                My memory may be failing but last time I checked that article provided a fast way to get all pixel information as well as write out said pixel information. The actual grayscale conversion is trivial when compared to the rest of the code. And if I recall the question, it was how can I quickly ... which the article addresses. As for binary image that is an exercise left to the reader. But then again, if you keep the gray scale logic you can then apply your threshold statement and get the binary result as well.

                Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #8

                Of course. BTW: he may be needing a dynamic threshold to get acceptable results on more difficult images, e.g. when luminance is varrying wildly. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


                1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  Yes. If they are black and white, then specify the System.Drawing.Imaging.PixelFormat.Format1bppIndexed mode.

                  You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                  A Offline
                  A Offline
                  ant damage
                  wrote on last edited by
                  #9

                  See the link below. It is a test image which I want to convert to binary image I forgot to mention something. The binary image is to be used in C++ source files. http://img176.imageshack.us/img176/744/boxm.png[^] Here is the expected result:

                  unsigned char box[27] = {
                  0xf8,0xff,0x3,
                  0x6,0x0,0xc,
                  0x1,0x0,0x10,
                  0x1,0x0,0x10,
                  0x1,0x0,0x10,
                  0x1,0x0,0x10,
                  0x1,0x0,0x10,
                  0x1,0x0,0x10,
                  0x1,0x0,0x10 };

                  OriginalGriffO 1 Reply Last reply
                  0
                  • A ant damage

                    See the link below. It is a test image which I want to convert to binary image I forgot to mention something. The binary image is to be used in C++ source files. http://img176.imageshack.us/img176/744/boxm.png[^] Here is the expected result:

                    unsigned char box[27] = {
                    0xf8,0xff,0x3,
                    0x6,0x0,0xc,
                    0x1,0x0,0x10,
                    0x1,0x0,0x10,
                    0x1,0x0,0x10,
                    0x1,0x0,0x10,
                    0x1,0x0,0x10,
                    0x1,0x0,0x10,
                    0x1,0x0,0x10 };

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #10

                    ant-damage wrote:

                    The binary image is to be used in C++ source files.

                    Does this mean that you want to use this as an image in your application - for a button or other control? Because if so, you don't have to convert it to binary.

                    You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    A 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      ant-damage wrote:

                      The binary image is to be used in C++ source files.

                      Does this mean that you want to use this as an image in your application - for a button or other control? Because if so, you don't have to convert it to binary.

                      You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                      A Offline
                      A Offline
                      ant damage
                      wrote on last edited by
                      #11

                      I want to use it as a sprite for my CASIO fx-9860G calculator. It is to be used in add-ins.

                      A 1 Reply Last reply
                      0
                      • A ant damage

                        I want to use it as a sprite for my CASIO fx-9860G calculator. It is to be used in add-ins.

                        A Offline
                        A Offline
                        ant damage
                        wrote on last edited by
                        #12

                        I've now created a class that can convert bitmaps (*.bmp; *.png) to binary images (0xFF 0x89 0x78 0x65), but now my problem is that some bitmaps have grey, and to display it on a B&W LCD I need to emulate the grey effect by using double-buffering (I've mastered the double-buffering issue). Both buffers are displayed to the screen with an especial delay, to created the grey effect.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups