I simply want streaming capture screen with TCP protocol on C#.
private Bitmap bmpScreenshot;
private byte[] screenToByteArray()
{
byte[] result;
try
{
if (bmpScreenshot != null)
bmpScreenshot.Dispose();
bmpScreenshot = new Bitmap(SystemInformation.VirtualScreen.Width,
SystemInformation.VirtualScreen.Height,
PixelFormat.Format32bppArgb);
using (var gfxScreenshot = Graphics.FromImage(bmpScreenshot))
{
gfxScreenshot.CopyFromScreen(SystemInformation.VirtualScreen.X,
SystemInformation.VirtualScreen.Y,
0,
0,
SystemInformation.VirtualScreen.Size,
CopyPixelOperation.SourceCopy);
result = ImageToByte(bmpScreenshot);
}
}
catch (Exception ex)
{
Console.WriteLine("[ERROR]screenToByteArray Error..{0}", ex.Message);
result = null;
}
return result;
}
private byte[] ImageToByte(Image iImage)
{
if (mMemoryStream != null)
mMemoryStream.Dispose();
mMemoryStream = new MemoryStream();
iImage.Save(mMemoryStream, ImageFormat.Png);
if (iImage != null)
iImage.Dispose();
return mMemoryStream.ToArray();
}
I use that code part and I'm sending screenToByteArray()
but I have a problem. If My screen does not have lots of image like that enter image description here, Listener can see correct display, but When my screen has complicated image(s) like that enter image description here , Listener sees distorted display like that enter image description here .When my screen has any complicate image, listener can't see the whole display. How can I do that. Thank for your help.
EDIT I share my tcp code below
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(new IPEndPoint(IPAddress.Parse("192.168.1.109"), 8500));
while(true)
{
try
{
byte[] sendData = screenToByteArray();
socket.Send(sendData, sendData.Length, SocketFlags.None);
sendData = null;
}
catch (Exception ex)
{
Console.WriteLine("[ERROR]sendScreen Error..{0}", ex.Message);
socket.Dispose();
break;
}
}
CodePudding user response:
I did it. The streamed picture format has been changed.
private byte[] ImageToByte(Image iImage)
{
if (mMemoryStream != null)
mMemoryStream.Dispose();
mMemoryStream = new MemoryStream();
iImage.Save(mMemoryStream, ImageFormat.Jpeg);
if (iImage != null)
iImage.Dispose();
return mMemoryStream.ToArray();
}