Search code examples
c#imagefiletga

Why does not read the whole text file?


In C#, I would like to read a .tga picture into a string variable. I use many of variations to read from a text file, but there is a problem with every solution. The file size 17Kb Why doesn't read the whole text?

For example this does not work:

string item = "";
while ((item = sr.ReadLine()) != null)
{
   picture_string += sr.ReadLine()+"";                      
}

It does not work:

picture_string = sr.ReadToEnd();

It does not work

picture_string = File.ReadAllText(path);

Solution

  • The file you are trying to read is a binary file, not a text file. Stop trying to read a binary file as if it is a text file.

    var fileContents = File.ReadAllBytes(path);