-1

I am trying to hide text file into image with c#

void HideTextFileIntoImage(string TextPath, string ImagePath, string NewFilePath)
{
    string[] Text = File.ReadAllLines(TextPath);
    string[] Image = File.ReadAllLines(ImagePath);
    File.Create(NewFilePath).Close();
    string[] NewFile = new string[Text.Length + Image.Length];
    for (int i = 0; i < Image.Length; i++)
    {
        NewFile[i] = Image[i];
    }
    for (int i = 0; i < Text.Length; i++)
    {
        NewFile[i + Image.Length] = Text[i];
    }
    StreamWriter sw = new StreamWriter(NewFilePath);
    for (int t = 0; t < NewFile.Length; t++)
    {
        sw.WriteLine(NewFile[t]);
    }
    sw.Close();
}

But I can't see image after using it. What wrong?

Community
  • 1
  • 1
ARTAGE
  • 379
  • 2
  • 4
  • 14
  • You can't just throw a bunch of extra bytes onto the end of an arbitrary binary and expect it to work. What you've done is corrupted the image (eg. the image reader doesn't know how to interpret the extra "stuff" tacked on the end of your image). – DVK Jun 27 '16 at 17:58
  • `string[] Image = File.ReadAllLines(ImagePath);` this is not a way to read in an image. You would have to use a binary reader.. – TaW Jun 27 '16 at 17:58

1 Answers1

1

You're trying to treat binary data as though it were text.

Try this instead (completely untested):

void HideTextFileIntoImage(string TextPath, string ImagePath, string NewFilePath)
{
    var textBytes = File.ReadAllBytes(TextPath);
    var imageBytes = File.ReadAllBytes(ImagePath);
    using (var stream = new FileStream(NewFilePath, FileMode.Create)) {
        stream.Write(imageBytes, 0, imageBytes.Length);
        stream.Write(textBytes, 0, textBytes.Length);
    }
}
user94559
  • 59,196
  • 6
  • 103
  • 103