我试图用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();
}
但是我用完后看不到图像。什么错了吗?
您试图将二进制数据视为文本。
试试这个(完全未经测试):
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);
}
}