Я пытался создать средство просмотра удаленных рабочих столов. В этой части приложения пользователь настраивает свой IP-адрес и порт для подключения другого пользователя. проблема заключается в следующем: каждый раз, когда я нажимаю кнопку подключения, он нажимает catch(exception)
и не может подключиться.
Поделиться
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
namespace Remote_Desktop
{
public partial class Form1 : Form
{
private readonly TcpClient client = new TcpClient();
private NetworkStream mainStream;
private int portNumber;
private static Image GrabDesktop()
{
Rectangle bounds = Screen.PrimaryScreen.Bounds;
Bitmap screenshot = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(screenshot);
graphics.CopyFromScreen(bounds.X, bounds.Y, 0, 0,bounds.Size, CopyPixelOperation.SourceCopy);
return screenshot;
}
private void SendDesktopImage()
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
mainStream = client.GetStream();
binaryFormatter.Serialize(mainStream, GrabDesktop());
}
public Form1()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
portNumber = int.Parse(txtPort.Text);
try
{
client.Connect(txtIp.Text, portNumber);
MessageBox.Show("Connected!");
}
catch(Exception)
{
MessageBox.Show("Failed To Connect!");
}
}
private void btnSend_Click(object sender, EventArgs e)
{
if (btnSend.Text.StartsWith("Share"))
{
timer1.Start();
btnSend.Text = "Stop Sharing";
}
else
{
timer1.Stop();
btnSend.Text = "Share Screen";
}
}
private void timer1_Tick(object sender, EventArgs e)
{
SendDesktopImage();
}
}
}
Любая помощь значит большое спасибо! Если возникнут проблемы, скажите мне, что я постараюсь это исправить!