TCP 소켓을 사용하여 이미지를 보내고 받도록 코드를 만들었지 만 수신 코드가 작동하지 않습니다. 코드를받을 여기에C# 보내기/받기 크기 (소켓)
public void SendImage()
{
int ScreenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int ScreenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap bmpScreenShot = new Bitmap(ScreenWidth, ScreenHeight);
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
gfx.CopyFromScreen(0, 0, 0, 0, new Size(ScreenWidth, ScreenHeight));
bmpScreenShot.Save(Application.StartupPath + "/ScreenShot.jpg", ImageFormat.Jpeg);
byte[] image = new byte[1];
bmpScreenShot = ResizeBitmap(bmpScreenShot, 300, 300);
image = ImageToByte(bmpScreenShot);
//get the length of image (length of bytes)
int NumberOfBytes = image.Length;
//put the size into a byte array
byte[] numberofbytesArray = BitConverter.GetBytes(NumberOfBytes);
//send the size to the Client
int sizesend = sck.Send(numberofbytesArray, 0, numberofbytesArray.Length, 0);
if (sizesend > 0)
{
MessageBox.Show("Size Sent");
}
//send the image to the Client
int imagesend =sck.Send(image, 0, NumberOfBytes, 0);
if (imagesend > 0)
{
MessageBox.Show("Image Sent");
}
}
그리고 :
이
은 전송 코드입니다public void ReceiveImage()
{
if (sck.Connected)
{
{
NetworkStream stream = new NetworkStream(sck);
byte[] data = new byte[4];
//Read The Size
stream.Read(data, 0, data.Length);
int size = (BitConverter.ToInt32(data,0));
// prepare buffer
data = new byte[size];
//Load Image
int read = 0;
while (read != data.Length)
{
read += stream.Read(data, read, data.Length - read);
}
//stream.Read(data, 0, data.Length);
//Convert Image Data To Image
MemoryStream imagestream = new MemoryStream(data);
Bitmap bmp = new Bitmap(imagestream);
pictureBox1.Image = bmp;
}
}
}
난 크기의 5킬로바이트하지만 때와 같이 전송을 보낼 때 문제가 2GB를 찾으면이 오류가 나타납니다.
전송 연결에서 데이터를 읽을 수 없습니다. 시스템에 충분한 버퍼 공간이 없거나 큐가 가득 차서 소켓에 대한 조작을 수행 할 수 있습니다.
오류는이 문장 read += stream.Read(data, read, data.Length - read);
수신자가 여전히 추가 데이터를 기대하는 동안 (송신자가 연결을 닫는 상황을 정교하게 다룬다.'stream.Read()'는이 경우'0'을 리턴 할 것이다). –