桌面图片抓下来转成字节数据要13万多, MemoryStream Stream = new MemoryStream(img); 在初始化时 提示错误--参数无效 img 为byte[]类型
private void Form1_Load(object sender, EventArgs e) { Bitmap bmp = new Bitmap("E:\\1.jpg"); string bmpStr = GetStr(bmp); bmp = GetBitmap(bmpStr); }private Bitmap GetBitmap(string bmpStr) { byte[] bytes = Convert.FromBase64String(bmpStr); ImageConverter ic = new ImageConverter(); return ic.ConvertFrom(bytes) as Bitmap; }private string GetStr(Bitmap bmp) { ImageConverter ic = new ImageConverter(); byte[] bytes = ic.ConvertTo(bmp, typeof(byte[])) as byte[]; return Convert.ToBase64String(bytes); }直接傳String
分别建立服务器客户端程序 服务器负责监听客户端的请求还需要建立一个客户端和服务器的沟通渠道,例如什么时候开始发送桌面,还有更进一步客户端可以发送命令让服务器端操作服务器所在电脑等.C#没有例子,VB.NET的有一个自己练习的,呵呵客户端主要代码: Private Sub MnuConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuConnect.Click Try ServerName = InputBox("请输入服务器地址:", "远程监视", "192.168.1.111") If ServerName = "" Then Exit Sub client = New TcpClient(ServerName, PORT_NUM) MnuConnect.Enabled = False MnuDisConnect.Enabled = True MnuCapture.Enabled = True client.GetStream.BeginRead(ReadBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing) Catch Ex As Exception MsgBox("服务器还没启动,请先运行远程服务器。", MsgBoxStyle.Exclamation, Me.Text) End Try End Sub Private Sub DoRead(ByVal ar As IAsyncResult) Dim BytesRead As Integer, strMessage As String Dim dataArray() As String Try BytesRead = client.GetStream.EndRead(ar) strMessage = Encoding.ASCII.GetString(ReadBuffer, 0, BytesRead - 1) dataArray = strMessage.Split(Chr(124)) If dataArray(0) = "ImgDataLength" Then ImgDataLength = CLng(dataArray(1)) Else ScreenStream.Write(ReadBuffer, 0, BytesRead) End If client.GetStream.BeginRead(ReadBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing) Catch e As Exception End Try End Sub Private Sub SendData(ByVal data As String) Try Dim writer As New IO.StreamWriter(client.GetStream) writer.Write(data & vbCr) writer.Flush() Catch e As Exception End Try End Sub服务器主要代码(完整的一个监控与发送类): Option Strict OnImports System.Net.Sockets Imports System.IO.Stream Imports System.TextPublic Class TcpIpSocket Const READ_BUFFER_SIZE As Integer = 256 Const SEND_BUFFER_SIZE As Integer = 2048 Private ReadBuffer(READ_BUFFER_SIZE - 1) As Byte Private SendBuffer(SEND_BUFFER_SIZE - 1) As Byte Private client As TcpClient Public Event LineReceived(ByVal sender As TcpIpSocket, ByVal Data As String) Public Sub New(ByVal client As TcpClient) Me.client = client Me.client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing) End Sub Public Sub SendData(ByVal ImgData As IO.MemoryStream) Dim SendSize As Integer Dim writeData As NetworkStream = client.GetStream Dim writeStr As New IO.StreamWriter(client.GetStream) ImgData.Position = 0 Try writeStr.Write("ImgDataLength|" & ImgData.Length.ToString & vbCr) writeStr.Flush() Do SendSize = ImgData.Read(SendBuffer, 0, SEND_BUFFER_SIZE) writeData.Write(SendBuffer, 0, SendSize) writeData.Flush() Loop Until ImgData.Length = ImgData.Position Catch e As Exception ''' MsgBox(e.ToString) End Try End Sub Private Sub StreamReceiver(ByVal ar As IAsyncResult) Dim BytesRead As Integer Dim strMessage As String Try SyncLock client.GetStream BytesRead = client.GetStream.EndRead(ar) End SyncLock strMessage = Encoding.ASCII.GetString(ReadBuffer, 0, BytesRead - 1) RaiseEvent LineReceived(Me, strMessage) SyncLock client.GetStream client.GetStream.BeginRead(ReadBuffer, 0, READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing) End SyncLock Catch e As Exception End Try End Sub End Class
MemoryStream Stream = new MemoryStream(img);
在初始化时 提示错误--参数无效
img 为byte[]类型
{
Bitmap bmp = new Bitmap("E:\\1.jpg");
string bmpStr = GetStr(bmp);
bmp = GetBitmap(bmpStr);
}private Bitmap GetBitmap(string bmpStr)
{
byte[] bytes = Convert.FromBase64String(bmpStr);
ImageConverter ic = new ImageConverter();
return ic.ConvertFrom(bytes) as Bitmap;
}private string GetStr(Bitmap bmp)
{
ImageConverter ic = new ImageConverter();
byte[] bytes = ic.ConvertTo(bmp, typeof(byte[])) as byte[];
return Convert.ToBase64String(bytes);
}直接傳String
服务器负责监听客户端的请求还需要建立一个客户端和服务器的沟通渠道,例如什么时候开始发送桌面,还有更进一步客户端可以发送命令让服务器端操作服务器所在电脑等.C#没有例子,VB.NET的有一个自己练习的,呵呵客户端主要代码:
Private Sub MnuConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuConnect.Click
Try
ServerName = InputBox("请输入服务器地址:", "远程监视", "192.168.1.111")
If ServerName = "" Then Exit Sub
client = New TcpClient(ServerName, PORT_NUM)
MnuConnect.Enabled = False
MnuDisConnect.Enabled = True
MnuCapture.Enabled = True
client.GetStream.BeginRead(ReadBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
Catch Ex As Exception
MsgBox("服务器还没启动,请先运行远程服务器。", MsgBoxStyle.Exclamation, Me.Text)
End Try
End Sub Private Sub DoRead(ByVal ar As IAsyncResult)
Dim BytesRead As Integer, strMessage As String
Dim dataArray() As String Try
BytesRead = client.GetStream.EndRead(ar)
strMessage = Encoding.ASCII.GetString(ReadBuffer, 0, BytesRead - 1)
dataArray = strMessage.Split(Chr(124))
If dataArray(0) = "ImgDataLength" Then
ImgDataLength = CLng(dataArray(1))
Else
ScreenStream.Write(ReadBuffer, 0, BytesRead)
End If
client.GetStream.BeginRead(ReadBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
Catch e As Exception End Try
End Sub Private Sub SendData(ByVal data As String)
Try
Dim writer As New IO.StreamWriter(client.GetStream)
writer.Write(data & vbCr)
writer.Flush()
Catch e As Exception
End Try
End Sub服务器主要代码(完整的一个监控与发送类):
Option Strict OnImports System.Net.Sockets
Imports System.IO.Stream
Imports System.TextPublic Class TcpIpSocket
Const READ_BUFFER_SIZE As Integer = 256
Const SEND_BUFFER_SIZE As Integer = 2048
Private ReadBuffer(READ_BUFFER_SIZE - 1) As Byte
Private SendBuffer(SEND_BUFFER_SIZE - 1) As Byte
Private client As TcpClient
Public Event LineReceived(ByVal sender As TcpIpSocket, ByVal Data As String) Public Sub New(ByVal client As TcpClient)
Me.client = client
Me.client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing)
End Sub Public Sub SendData(ByVal ImgData As IO.MemoryStream)
Dim SendSize As Integer
Dim writeData As NetworkStream = client.GetStream
Dim writeStr As New IO.StreamWriter(client.GetStream)
ImgData.Position = 0
Try
writeStr.Write("ImgDataLength|" & ImgData.Length.ToString & vbCr)
writeStr.Flush()
Do
SendSize = ImgData.Read(SendBuffer, 0, SEND_BUFFER_SIZE)
writeData.Write(SendBuffer, 0, SendSize)
writeData.Flush()
Loop Until ImgData.Length = ImgData.Position
Catch e As Exception
''' MsgBox(e.ToString)
End Try
End Sub Private Sub StreamReceiver(ByVal ar As IAsyncResult)
Dim BytesRead As Integer
Dim strMessage As String Try
SyncLock client.GetStream
BytesRead = client.GetStream.EndRead(ar)
End SyncLock strMessage = Encoding.ASCII.GetString(ReadBuffer, 0, BytesRead - 1)
RaiseEvent LineReceived(Me, strMessage) SyncLock client.GetStream
client.GetStream.BeginRead(ReadBuffer, 0, READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing)
End SyncLock
Catch e As Exception
End Try
End Sub
End Class
随便说几个可能的思路,仅供参考:)1. 最简单的办法
在程序中嵌入Windows Remote Desktop,每个客户端让他登录域中,服务器使用域管理员帐号登录,网络连接都省了:)2. 复杂点的办法
普通socket传递,普通算法压缩(7z)3. 再复杂点的
和上次的图片比较,压缩已经变化的部分4. 再再复杂点的
监测弹出的窗口,确定其前景、背景、字体、文字,只传递,需要传背景图的再传图,毕竟Windows里的所有窗口都是继承Win的看看你喜欢哪个?呵呵顺便宣传一下我的网站◆源码搜搜◆
1. 最简单的办法
在程序中嵌入Windows Remote Desktop,每个客户端让他登录域中,服务器使用域 管理员帐号登录,网络连接都省了:)
2. 复杂点的办法
普通socket传递,普通算法压缩(7z) 3. 再复杂点的
和上次的图片比较,压缩已经变化的部分 4. 再再复杂点的
监测弹出的窗口,确定其前景、背景、字体、文字,只传递,需要传背景图的再传 图,毕竟Windows里的所有窗口都是继承Win的 --------------------------------------------
能不能再详细点呢
暂时也不宣传我的网站了,直接去CODEPROJECT上看吧,呵呵A simple Windows forms properties spy等等,看看C++的也有不少啊