C# 屏幕捕获和流式传输
C# Screen Capture and Streaming
我只是想在 C# 上使用 TCP 协议流式捕获屏幕。
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();
}
我使用该代码部分并发送 screenToByteArray()
但我遇到了问题。如果我的屏幕没有很多这样的图像 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 。当我的屏幕有任何复杂的图像时,听众无法看到整个显示。我怎样才能做到这一点。感谢您的帮助。
编辑
我在下面分享我的 tcp 代码
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;
}
}
我做到了。流式传输图片格式已更改。
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();
}
我只是想在 C# 上使用 TCP 协议流式捕获屏幕。
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();
}
我使用该代码部分并发送 screenToByteArray()
但我遇到了问题。如果我的屏幕没有很多这样的图像 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 。当我的屏幕有任何复杂的图像时,听众无法看到整个显示。我怎样才能做到这一点。感谢您的帮助。
编辑 我在下面分享我的 tcp 代码
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;
}
}
我做到了。流式传输图片格式已更改。
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();
}