-3

I have tried many times to open file in "Open File Dialog" just like image then read it in bytes, then display the source bits using TextBox.

FileStream stream = File.OpenRead(@"c:\image1.jpg");

For Ex:

Read all offsets from 0 to length and display file source into TextBox like 001101001101111000001101......

Problem:

I'm beginner in C# and i have seen many functions but i don't know how to put it in C#.

5
  • 2
    SO can't help with problem specified in your post - it is site for coding question, not really life/career advice site. Note that if problem (beginner in C#) is not what you wanted to ask about than both "how to use File.ReadAllBytes" and "convert byte array to binary" have many duplicates which you may want to search for. May 24, 2014 at 6:55
  • Try getting a book and LEARNING. You will never learn by getting topics answered like this.
    – TomTom
    May 24, 2014 at 6:57
  • thank you for replay but the problem that i dont know how to use functions in c# for ex: i don't know how to call the function that user insert it in here
    – Sami
    May 24, 2014 at 6:57
  • 1
    Then that's the first thing to learn ...
    – H H
    May 24, 2014 at 7:01
  • i think i must search for these two methods. 1st File.ReadAllBytes & the 2nd convert byte array to binary ... thank you alexei
    – Sami
    May 24, 2014 at 7:02

3 Answers 3

1

This way is better to real all bytes of a file:

        DialogResult dr = openFileDialog1.ShowDialog();
        if (dr == DialogResult.OK)
        {
            string filename = openFileDialog1.FileName;

            int len= openFileDialog1.FileName.Length;
            byte[] ATM = File.ReadAllBytes(filename);
        }
1
  • Hello @Pranav Thank you so much, this is help and i use your code .. it's work but i have a small problem.. when i load a large file like 30MB the solution stop working and i get this error OutOfMemoryException please help, Best reagrds
    – Sami
    May 24, 2014 at 23:58
0

This a way to do it:

    private string ToBinary()
    {
        FileStream stream = File.OpenRead(@"c:\image1.jpg"
        var sb = new StringBuilder();

        int b = 0;
        while ((b = stream.ReadByte()) > -1)
        {
            sb.Append(Convert.ToString(b, 2));
        }
        return sb.ToString();
    }

Convert.ToString(b, 2)) automatically converts a number to a binary

0
0
FileStream stream = new FileStream("D:/pqr.jpeg", FileMode.Open);
int a = stream.ReadByte();

Not the answer you're looking for? Browse other questions tagged or ask your own question.