Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6871647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:49:54+00:00 2026-05-27T03:49:54+00:00

I am trying to write a C# program designed to convert a .bmp file

  • 0

I am trying to write a C# program designed to convert a .bmp file into binary.
The file is 16×16 pixels. Each black pixel represents a one in binary, so the number 10 would be █[]█[][][][][]

The problem I am having is that my code is not recognizing the black pixels, so the output is always zero.

    public Bitmap imgToDecode;

    private void button2_Click(object sender, EventArgs e)
    {
        int i = (imgToDecode.Height * imgToDecode.Width);
        bool[] pixData = new bool[i];

        int p = 0;

        for (int k = 1; k < imgToDecode.Height; k++)
        {
            for (int m = 1; m < imgToDecode.Width; m++)
            {
                if (imgToDecode.GetPixel(m, k) == Color.Black)
                {
                    pixData[p] = true;
                }
                else
                {
                    pixData[p] = false;
                }
                p++;
            }
        }

        for (int n = 0; n < pixData.Length; n++)
        {
            textBox2.Text = (textBox2.Text + (Convert.ToInt32(pixData[n])));
        }


    }

If anyone has an idea as to why the output is 0, could they please help me. Also any ways of improving the code would be welcomed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:49:54+00:00Added an answer on May 27, 2026 at 3:49 am

    The source of the problem is probably that Color.Black is not equal to Color.FromArgb(0, 0, 0).

    The solution would probably be to change the line:

    if (imgToDecode.GetPixel(m, k) == Color.Black)
    

    to:

    if (imgToDecode.GetPixel(m, k) == Color.FromArgb(0, 0, 0))
    

    or even better, declare a variable containing the (0,0,0) color and then use it in this if statement.
    So do something like:

     Color black = Color.FromArgb(0, 0, 0);
    

    in the beginning of your method and then change if to:

    if (imgToDecode.GetPixel(m, k) == black)
    

    UPDATE:
    There seemed to be some minor issues with loops start values. I’ve updated your code.

    public Bitmap imgToDecode;
    
    private void button2_Click(object sender, EventArgs e)
    {
        textBox2.Text = "";
    
        Color black = Color.FromArgb(0, 0, 0);
    
        int i = (imgToDecode.Height * imgToDecode.Width);
        bool[] pixData = new bool[i];
    
        int p = 0;
    
        for (int k = 0; k < imgToDecode.Height; k++)
        {
            for (int m = 0; m < imgToDecode.Width; m++)
            {
                pixData[p] = (imgToDecode.GetPixel(m, k) == black);
    
                p++;
            }
        }
    
        for (int n = 0; n < pixData.Length; n++)
        {
            textBox2.Text = (textBox2.Text + (Convert.ToInt32(pixData[n])));
        }
    }
    

    If you don’t need pixData array to contain bool values, you can change it to int and asign 1 or 0. This way you don’t have to convert it later :).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program that reads in entries from a file into
I'm trying to write a program to process the BSD-style process accounting file under
I'm trying to write a program that reads text from external file (string string
I am trying to write a program that allows a binary to be run,
I trying to write a program that navigates the local file system using a
Im trying to write a program to read a text file through args but
I am trying to write a program that will hook into application startup and
I'm trying to write a program that takes in a plaintext file as it's
I'm trying to write a program that alters a .txt file of a variable
I'm trying to write a program that uses sockets to connect with other instances

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.