Results 1 to 4 of 4

Thread: How to convert image to binary and get result in text box

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    37

    How to convert image to binary and get result in text box

    I need to convert image to binary and get result in text box .

    Image found in path D:/person.jpg

    i using the following function :




    public bool[] imageToBinaryArray(System.Drawing.Image imageIn)
    {
    MemoryStream ms = new MemoryStream();
    bool[] arr = new bool[50000000];
    int i =0;
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
    BitArray bitarray = new BitArray(ms.ToArray());
    foreach (bool item in bitarray)
    {
    arr[i] = item;
    i++;
    }
    return arr;
    }

    How to receive the value returned from function imageToBinaryArray in textbox1 ?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to convert image to binary and get result in text box

    Firstly, please don't post unformatted code snippets as they are hard to read.
    csharp Code:
    1. public bool[] imageToBinaryArray(System.Drawing.Image imageIn)
    2.         {
    3.             MemoryStream ms = new MemoryStream();
    4.             bool[] arr = new bool[50000000];
    5.             int i =0;
    6.             imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
    7.             BitArray bitarray = new BitArray(ms.ToArray());
    8.             foreach (bool  item in bitarray)
    9.             {
    10.                 arr[i] = item;
    11.                 i++;
    12.             }
    13.             return arr;
    14.         }
    As for the question, what are you actually expecting to see in the TextBox? Do you want a binary string, i.e. a string of 1 and 0 digits?

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    37

    Re: How to convert image to binary and get result in text box

    yes because i need to get image in binary string 0 and 1

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to convert image to binary and get result in text box

    You already know how to get a byte array from an Image so all you need is how to get a binary string from a byte array:
    csharp Code:
    1. // No gaps between bytes.
    2. var myBinaryString1 = string.Concat(myByteArray.Select(b => Convert.ToString(b, 2).PadLeft(8, '0')));
    3.  
    4. // One space between each pair of bytes.
    5. var myBinaryString2 = string.Join(" ", myByteArray.Select(b => Convert.ToString(b, 2).PadLeft(8, '0')));
    Just be aware that a string requires two bytes per character so, if your Image is large, your string will be very big and may not be practical to display in a TextBox or even store in memory.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width