2012-01-24 10 views
2

thisの上に問題があります。私は外部プロセスからバイナリデータを取得しようとしますが、データ(イメージ)は壊れているようです。以下のスクリーンショットは、破損を示しています。左のイメージは、コードから正しいものをコマンドラインで実行することによって実行されました。 enter image description hereProcess.StandardOutputからバイナリデータをリダイレクトすると、データが正しく処理されます

マイコードこれまで:

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

これはエンコーディングの問題のいくつかの種類ですか?問題を回避するために、前述のようにStream.CopyTo-Approachを使用しました(here)。

+1

私はあなたのための答えを持っていないが、私はヒントがあるかもしれません。ヘキサエディタ(私はfhredをお勧めします)を使用し、出力ファイルと入力ファイルを見てください。私は起こっていることは、改行文字が翻訳されて腐敗が起こっていることだと思う。 – antiduh

+0

バイナリデータはリダイレクトできません。テキストのみです。 –

+0

答えをありがとう。まず、バイナリデータのリダイレクトが可能です。私はちょうど1分前に答えを見つけました。しかし私の評判のせいで私は今質問に答えることができません。私は後でそれを掲示するでしょう。これまでのところ、問題は出力をリダイレクトしていなかったため、ファイルを読み込んでいました。 – 0xBADF00D

答えて

4

問題が見つかりました。出力のリダイレクションは正しいですが、入力の読みが問題のようです。それが動作

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

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

ない:だから私はからコードを変更しました!ここでは、同じ問題を抱えている可能性のあるすべての人については、

訂正コード:

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(); 
関連する問題