私はTCPソケットを介してイメージを送信しているプログラムを作成しました。ストリーム。C# - イメージ - バイナリフォーマッタの逆シリアル化エラー - 有効なBinaryHeaderを含んでいません
Additional information: Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.
エラーの原因となっている完全な機能は次のとおりです。
private void handleClientThread(TcpClient tcpClient)
{
while (true)
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
pictureBox1.Image = (Image)binaryFormatter.Deserialize(tcpClient.GetStream()); // this line is the one throwing an exception
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
クライアントがすべてを正しく送信されます。
private Image TakePicture()
{
Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(bmpScreenCapture);
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, bmpScreenCapture.Size, CopyPixelOperation.SourceCopy);
return bmpScreenCapture;
}
private void SendPicture()
{
Image clientPicture = TakePicture();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(sharerClient.GetStream(), clientPicture);
}
誰もがこの問題で私を助けたり、私に送って助けてもらえ例外を投げることなくTCPソケット上のイメージ
'BinaryFormatter'をまとめてドロップして、ピクセルデータ(PNGのような圧縮されたピクセルデータ)を送信してください。 'BinaryFormatter'は遅く、相互運用性がなく、バージョン管理の悪夢です。これはずっと前に廃止されていたはずです。 –
私はそれを送信する方法の例を教えてもらえますか、私は約2時間を費やして、BinaryFormatterを使用せずに動作させようとしました – user6278056
私は答えを書く時間があまりありませんが、 ['Bitmap.Save'](https://msdn.microsoft.com/en-us/library/ms142147.aspx)を呼び出し、ネットワークストリームを渡します。あなたはおそらく最初にバイト単位のイメージサイズを持つヘッダを送る必要があります。だから、このために中間の 'MemoryStream'を使うことができます。もう一方で、 'Stream'パラメータを取るコンストラクタのオーバーロードを使って' Bitmap'を構築してください。 –