❔ Capture screen(video codec) and send to client simultaneously using socket

I have already made send image code (capture image and send to client continuously. But it causes lag. So i want to try new method. Code:
private void Screen()
{
try
{
listener_3.Start();
eventLog1.WriteEntry("Waiting for connection...");

while (true)
{
Socket client = listener_3.AcceptSocket();
Console.WriteLine("Client connected");

while (client.Connected)
{
// Capture the screen
Bitmap screen = CaptureScreen();

int newWidth = screen.Width / 4; // Reduce width by half
int newHeight = screen.Height / 4; // Reduce height by half

Bitmap resizedImage = new Bitmap(newWidth, newHeight);

// Use Graphics.DrawImage to resize the image
using (Graphics graphics = Graphics.FromImage(resizedImage))
{
graphics.DrawImage(screen, 0, 0, newWidth, newHeight);
}

// Convert the resized image to a byte array and send it to the client
byte[] imageData = ImageToByteArray(resizedImage);
client.Send(imageData);

// Delay between sending each frame (adjust as needed)
Thread.Sleep(20);
}

eventLog1.WriteEntry("Connection terminated");
client.Close();
}
}
catch (Exception e)
{
listener_3.Stop();
listener_3 = new TcpListener(IPAddress.Any, 50523);
eventLog1.WriteEntry("Error: " + e.Message);
}
}
private void Screen()
{
try
{
listener_3.Start();
eventLog1.WriteEntry("Waiting for connection...");

while (true)
{
Socket client = listener_3.AcceptSocket();
Console.WriteLine("Client connected");

while (client.Connected)
{
// Capture the screen
Bitmap screen = CaptureScreen();

int newWidth = screen.Width / 4; // Reduce width by half
int newHeight = screen.Height / 4; // Reduce height by half

Bitmap resizedImage = new Bitmap(newWidth, newHeight);

// Use Graphics.DrawImage to resize the image
using (Graphics graphics = Graphics.FromImage(resizedImage))
{
graphics.DrawImage(screen, 0, 0, newWidth, newHeight);
}

// Convert the resized image to a byte array and send it to the client
byte[] imageData = ImageToByteArray(resizedImage);
client.Send(imageData);

// Delay between sending each frame (adjust as needed)
Thread.Sleep(20);
}

eventLog1.WriteEntry("Connection terminated");
client.Close();
}
}
catch (Exception e)
{
listener_3.Stop();
listener_3 = new TcpListener(IPAddress.Any, 50523);
eventLog1.WriteEntry("Error: " + e.Message);
}
}
2 Replies
어부지망생
어부지망생12mo ago

private Bitmap CaptureScreen()
{
// Capture the screen using the GDI API
Rectangle screenBounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
Bitmap bitmap = new Bitmap(screenBounds.Width, screenBounds.Height, PixelFormat.Format32bppArgb);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(screenBounds.Left, screenBounds.Top, 0, 0, screenBounds.Size, CopyPixelOperation.SourceCopy);
}

return bitmap;
}

private byte[] ImageToByteArray(Image image)
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Jpeg);
return ms.ToArray();
}
}

private Bitmap CaptureScreen()
{
// Capture the screen using the GDI API
Rectangle screenBounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
Bitmap bitmap = new Bitmap(screenBounds.Width, screenBounds.Height, PixelFormat.Format32bppArgb);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(screenBounds.Left, screenBounds.Top, 0, 0, screenBounds.Size, CopyPixelOperation.SourceCopy);
}

return bitmap;
}

private byte[] ImageToByteArray(Image image)
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Jpeg);
return ms.ToArray();
}
}
Client:
private class ReceiveDataTask extends AsyncTask<Void, Void, Bitmap> {

@Override
protected Bitmap doInBackground(Void... voids) {
try {
// Establish a TCP connection with the server
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, 50523), 500);

while (screen_running) {
// Receive the screen image data
InputStream inputStream = socket.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

// Decode the image data into a Bitmap
Bitmap bitmap = BitmapFactory.decodeStream(bufferedInputStream);

// Update the ImageView on the UI thread
runOnUiThread(new Runnable() {
@Override
public void run() {
if (bitmap != null)
{
control_screen_computer_screen.setImageBitmap(bitmap);
Log.wtf("receive data task", "image changed");
}

else
{
Log.wtf("receive data task", "image data null");
}

}
});

Thread.sleep(20);
}

// Close the connection
socket.close();
}

catch (Exception e)
{
Log.wtf("receive data task", "error occur " + e);
}
return null;
}
}
private class ReceiveDataTask extends AsyncTask<Void, Void, Bitmap> {

@Override
protected Bitmap doInBackground(Void... voids) {
try {
// Establish a TCP connection with the server
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, 50523), 500);

while (screen_running) {
// Receive the screen image data
InputStream inputStream = socket.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

// Decode the image data into a Bitmap
Bitmap bitmap = BitmapFactory.decodeStream(bufferedInputStream);

// Update the ImageView on the UI thread
runOnUiThread(new Runnable() {
@Override
public void run() {
if (bitmap != null)
{
control_screen_computer_screen.setImageBitmap(bitmap);
Log.wtf("receive data task", "image changed");
}

else
{
Log.wtf("receive data task", "image data null");
}

}
});

Thread.sleep(20);
}

// Close the connection
socket.close();
}

catch (Exception e)
{
Log.wtf("receive data task", "error occur " + e);
}
return null;
}
}
client is java (android) anyone knows please mention me thanks for supporting
Accord
Accord12mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.