将文本文件隐藏到图像中

时间:2016-06-27 17:44:55

标签: c# image file text hide

我正在尝试使用c#

hide text file into image
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();
}

但使用后我无法看到图像。怎么了?

1 个答案:

答案 0 :(得分:1)

您正在尝试将二进制数据视为文本。

试试这个(完全未经测试):

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);
    }
}