2012-12-04 3 views
0

클라이언트에서 서버로 웹캠을 스트리밍하려고하는데 바이트 배열에서 서버의 비트 맵으로 변환하는 데 어려움이 있습니다.비트 맵 변환

public void handlerThread() 
{ 
    Socket handlerSocket = (Socket)alSockets[alSockets.Count-1]; 
    NetworkStream networkStream = new 
    NetworkStream(handlerSocket); 
    int thisRead=0; 
    int blockSize=1024; 
    Byte[] dataByte = new Byte[blockSize]; 
    lock(this) 
    { 
     // Only one process can access 
     // the same file at any given time 
     while(true) 
     { 
      thisRead=networkStream.Read(dataByte,0,blockSize); 

      pictureBox1.Image = byteArrayToImage(dataByte); 
      if (thisRead==0) break; 
     } 
     fileStream.Close(); 
    } 
    lbConnections.Items.Add("File Written"); 
    handlerSocket = null; 
} 

public Image byteArrayToImage(byte[] byteArrayIn) 
{ 
    MemoryStream ms = new MemoryStream(byteArrayIn); //here is my error 
    Image returnImage = Image.FromStream(ms); 
    return returnImage; 
} 

난 다시 이미지와 충돌로 변환 할 때 "매개 변수가 유효하지 않습니다"얻을 위에 표시된 지점에서 :

여기에 코드입니다. 내가 뭘 잘못하고 있는지에 대한 제안은?

+0

내 버퍼 크기가 너무 작았습니다. 먼저 좀 더 테스트해야합니다. – windowskm

답변

0

참고이 비트 : Image.Save(..) throws a GDI+ exception because the memory stream is closed

당신은 확장 메서드를 만들거나 제거 할 수 있습니다 "이"전통적인 방법. 이것은 당신의 코드와 같아 보이므로 어떤 타입의 인코딩이나 기본이되는 바이트 배열을 만드는 것에 관련된 다른 이슈가 있는지 궁금합니다.

public static Image ToImage(this byte[] bytes) 
{ 
    // You must keep the stream open for the lifetime of the Image. 
    // Image disposal does clean up the stream. 

    var stream = new MemoryStream(bytes); 
    return Image.FromStream(stream); 
} 
관련 문제