3

On top of this problem, I have another. I try to get binary data from a external process, but the data(a image) seems to be corrupted. The screenshot below shows the corruption: The left image was done by executing the program on command line, the right one from code. enter image description here

My Code so far:

var process = new Process
{
  StartInfo =
  {
    Arguments = string.Format(@"-display"),
    FileName = configuration.PathToExternalSift,
    RedirectStandardError = true,
    RedirectStandardInput = true,
    RedirectStandardOutput = true,
    UseShellExecute = false,
    CreateNoWindow = true,
  },
  EnableRaisingEvents = true
};

process.ErrorDataReceived += (ProcessErrorDataReceived);

process.Start();
process.BeginErrorReadLine();

//Reads in pbm file.
using (var streamReader = new StreamReader(configuration.Source))
{
  process.StandardInput.Write(streamReader.ReadToEnd());
  process.StandardInput.Flush();
  process.StandardInput.Close();
}
//redirect output to file.
using (var fileStream = new FileStream(configuration.Destination, FileMode.OpenOrCreate))
{
  process.StandardOutput.BaseStream.CopyTo(fileStream);
}

process.WaitForExit();

Is this some kind of encoding problem? I used the Stream.CopyTo-Approach as mentioned here in order to avoid there problems.

Community
  • 1
  • 1
0xBADF00D
  • 980
  • 1
  • 12
  • 25
  • 1
    I don't have an answer for you, but I might have a hint. Use a hex editor (I recommend fhred) and look at the output file and the input file. I bet whats happening is that the corruption is coming from newline characters being translated. – antiduh Jan 24 '12 at 14:18
  • You can't redirect binary data, only text. – Hans Passant Jan 24 '12 at 14:22
  • Thanks for the answers. First of all, its possible to redirect binary data. I found the answer just a minute ago. But due to my reputation I can't answer the question now. I will post it later. So far: The problem was not redirecting the output, it was on reading in the file. – 0xBADF00D Jan 24 '12 at 14:27

1 Answers1

5

I found the problem. The redirection of the output was correct, the reading of the input seems to be the problem. So I changed the code from:

using (var streamReader = new StreamReader(configuration.Source))
{
  process.StandardInput.Write(streamReader.ReadToEnd());
  process.StandardInput.Flush();
  process.StandardInput.Close();
}

to

using (var fileStream = new StreamReader(configuration.Source))
{
  fileStream.BaseStream.CopyTo(process.StandardInput.BaseStream);
  process.StandardInput.Close();
}

Not it works!

For all the people that might have the same problem, here the corrected code:

var process = new Process
{
  StartInfo =
  {
    Arguments = string.Format(@"-display"),
    FileName = configuration.PathToExternalSift,
    RedirectStandardError = true,
    RedirectStandardInput = true,
    RedirectStandardOutput = true,
    UseShellExecute = false,
    CreateNoWindow = true,
  },
  EnableRaisingEvents = true
};

process.ErrorDataReceived += (ProcessErrorDataReceived);

process.Start();
process.BeginErrorReadLine();

//read in the file.
using (var fileStream = new StreamReader(configuration.Source))
{
    fileStream.BaseStream.CopyTo(process.StandardInput.BaseStream);
    process.StandardInput.Close();
}
//redirect output to file.
using (var fileStream = new FileStream(configuration.Destination, FileMode.OpenOrCreate))
{
    process.StandardOutput.BaseStream.CopyTo(fileStream);
}

process.WaitForExit();
0xBADF00D
  • 980
  • 1
  • 12
  • 25