本文介绍了插座不工作,因为它应该帮助!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了发言,屏幕捕捉,并通过插座被转移到所需的听者,但我得到一个黑掉把图像的接收端或客户端图像那里服务器,但不能显示在客户端和服务器的基于套接字GUI应用程序它在我的客户端应用程序,它只是显示一个黑色的图片



代码:服务器

 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;使用System.Windows.Forms的
;
使用System.Drawing.Imaging;使用的System.Net.Sockets
;
:使用System.IO;使用System.Net
;

命名空间LanMonitoring
{
公共部分Form1类:表格
{
私有静态位图bmpScreenshot;
布尔开始= FALSE;
私有静态图形gfxScreenshot;

公共Form1中()
{
的InitializeComponent();
button2.Enabled = FALSE;
}

私人无效的button1_Click(对象发件人,EventArgs五)
{
button1.Enabled = FALSE;
button2.Enabled = TRUE;
启动= TRUE;

fillpic();
}
公共无效fillpic()
{
bmpScreenshot =新位图(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,PixelFormat.Format32bppArgb);
gfxScreenshot = Graphics.FromImage(bmpScreenshot);
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,Screen.PrimaryScreen.Bounds.Y,0,0,Screen.PrimaryScreen.Bounds.Size,CopyPixelOperation.SourceCopy);
pictureBox1.Image = bmpScreenshot;
sendbmp(bmpScreenshot);
}


私人无效button2_Click(对象发件人,EventArgs五)
{

button1.Enabled = TRUE;
button2.Enabled = FALSE;
开始= FALSE;
}
公共无效sendbmp(BMP位图)
{
插槽毫米=新的Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint remoteEP =新IPEndPoint(IPAddress.Parse(textBox1.Text),5002);

{
mm.Connect(remoteEP);
}
赶上(例外)
{
sendbmp(bmpScreenshot);
}
图像TEMP = BMP;
的byte [] buf中= imageToByteArray(TEMP);
mm.Send(BUF);
mm.Close();
}
公共字节[] imageToByteArray(System.Drawing.Image对象imageIn)
{
的MemoryStream毫秒​​=新的MemoryStream();
imageIn.Save(MS,System.Drawing.Imaging.ImageFormat.Gif);
返回ms.ToArray();
}


公众形象byteArrayToImage(字节[] byteArrayIn)
{
的MemoryStream毫秒​​=新的MemoryStream(byteArrayIn);
图像returnImage = Image.FromStream(毫秒);
返回returnImage;
}

}
}



客户机

 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;使用System.Windows.Forms的
;使用的System.Net.Sockets
;
:使用System.IO;使用System.Net
;
命名空间LanReciver
{
公共部分类客户:表
{

的byte [] buf中=新的字节[5000];
公共客户端()
{
的InitializeComponent();

}

私人无效的button1_Click(对象发件人,EventArgs五)
{
backgroundWorker1.RunWorkerAsync();
}
公共无效的()调用
{
插槽毫米=新的Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
mm.Bind(新IPEndPoint(0,5002));
mm.Listen(100);
插槽ACC = mm.Accept();
BUF =新的字节[acc.SendBufferSize]
INT byteread = acc.Receive(BUF);
的byte []转=新的字节[byteread]
的for(int i = 0; I< byteread;我++)
{
转由[i] = BUF [I]
}
byteArrayToImage(REV);
mm.Close();
acc.Close();
()调用;
}

公众形象byteArrayToImage(字节[] byteArrayIn)
{
的MemoryStream毫秒​​=新的MemoryStream(byteArrayIn);
图像returnImage = Image.FromStream(毫秒);
返回returnImage;
}

私人无效backgroundWorker1_DoWork(对象发件人,DoWorkEventArgs E)
{
()调用;
}

私人无效backgroundWorker1_RunWorkerCompleted(对象发件人,RunWorkerCompletedEventArgs E)
{
MessageBox.Show(收到);
}


}
}


解决方案

下面是一个相当完整的(虽然快速和肮脏的)解决方案:



服务器:

 使用系统; 
使用System.Drawing中;
使用System.Drawing.Imaging;
:使用System.IO;使用System.Net
;使用的System.Net.Sockets
;
使用的System.Threading;使用System.Windows.Forms的
;

命名空间ImageServer
{
使用(VAR插座静态类节目
{
静态无效的主要()
{
=新的Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp))
{
socket.Bind(新IPEndPoint(IPAddress.Any,23456));
socket.Listen(100);而(真)
{
使用(VAR的客户= socket.Accept())
{
VAR边界= Screen.PrimaryScreen.Bounds
;
VAR位=新位图(bounds.Width,bounds.Height);同时使用(真)
{


{
(VAR显卡= Graphics.FromImage(位图))
{
图形。 CopyFromScreen(bounds.X,0,bounds.Y,0,bounds.Size);
}
字节[]为imageData;使用
(VAR流=新的MemoryStream())
{
bitmap.Save(流ImageFormat.Png);
为imageData = stream.ToArray();
}
VAR lengthData = BitConverter.GetBytes(imageData.Length);
如果(client.Send(lengthData)LT; lengthData.Length)破;
如果(client.Send(为imageData)LT; imageData.Length)破;
Thread.sleep代码(1000);
}
}

{
中断;
}
}
}
}
}
}
}

客户端(带有一个按钮的一种形式:Button1的和一个图片框:pictureBox1):

 使用系统; 
使用System.Drawing中;
:使用System.IO;使用System.Net
;使用的System.Net.Sockets
;
使用的System.Threading;使用System.Windows.Forms的
;

命名空间ImageClient
{
公共部分Form1类:表格
{
私人位图_buffer;

公共Form1中()
{
的InitializeComponent();
}

私人无效Button1Click(对象发件人,EventArgs五)
{
button1.Enabled = FALSE;
ThreadPool.QueueUserWorkItem(GetSnapshots);
}

私人无效GetSnapshots(对象状态)
{
使用(VAR插座=新的Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp))
{
socket.Connect(新IPEndPoint(IPAddress.Loopback,23456));
,而(真)
{
变种lengthData =新的字节[4];
变种lengthBytesRead = 0;
,而(lengthBytesRead< lengthData.Length)
{
无功读= socket.Receive(lengthData,lengthBytesRead,lengthData.Length - lengthBytesRead,SocketFlags.None);
如果(阅读== 0)返回;
lengthBytesRead + =读;
}
变种长度= BitConverter.ToInt32(lengthData,0);
变种为imageData =新的字节[长度]
变种imageBytesRead = 0;
,而(imageBytesRead< imageData.Length)
{
无功读= socket.Receive(为imageData,imageBytesRead,imageData.Length - imageBytesRead,SocketFlags.None);
如果(阅读== 0)返回;
imageBytesRead + =读;使用
}
(VAR流=新的MemoryStream(为imageData))
{
VAR位=新位图(流);
调用(新ImageCompleteDelegate(的imageComplete),新的对象[] {}位图)
}
}
}
}

私人委托无效ImageCompleteDelegate(位图位图);
私人无效的imageComplete(位图位图)
{
如果(_buffer!= NULL)
{
_buffer.Dispose();
}
_buffer =新位图(位图);
pictureBox1.Size = _buffer.Size;
pictureBox1.Invalidate();
}

私人无效PictureBox1Paint(对象发件人,PaintEventArgs的E)
{
如果(_buffer == NULL)回报;
e.Graphics.DrawImage(_buffer,0,0);
}
}
}


i made a client and server socket based gui app in which screen is captured and via socket it is transfered to desired listner but i am getting a black out put image a receiver end or client the image is there at server but cannot show it in my client app it just show a black pic?

code as : server

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Net.Sockets;
using System.IO;
using System.Net;

namespace LanMonitoring
{
    public partial class Form1 : Form
    {
      private static Bitmap bmpScreenshot;
      bool start = false;
      private static Graphics gfxScreenshot;

      public Form1()
      {
        InitializeComponent();
        button2.Enabled = false;
      }

      private void button1_Click(object sender, EventArgs e)
      {
        button1.Enabled = false;
        button2.Enabled = true;
        start = true;

        fillpic();
      }
      public void fillpic()
      {
        bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
        gfxScreenshot = Graphics.FromImage(bmpScreenshot);
        gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
        pictureBox1.Image = bmpScreenshot;
        sendbmp(bmpScreenshot);
      }


      private void button2_Click(object sender, EventArgs e)
      {

        button1.Enabled = true;
        button2.Enabled = false;
        start = false;
      }
      public void sendbmp(Bitmap bmp)
      {
        Socket mm = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(textBox1.Text), 5002);
        try
        {
          mm.Connect(remoteEP);
        }
        catch (Exception)
        {
          sendbmp(bmpScreenshot);
        }
        Image temp = bmp;
        byte[] buf = imageToByteArray(temp);
        mm.Send(buf);
        mm.Close();
     }
      public byte[] imageToByteArray(System.Drawing.Image imageIn)
      {
        MemoryStream ms = new MemoryStream();
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        return ms.ToArray();
      }


      public Image byteArrayToImage(byte[] byteArrayIn)
      {
        MemoryStream ms = new MemoryStream(byteArrayIn);
        Image returnImage = Image.FromStream(ms);
        return returnImage;
      }

    }
}

client as:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Net;
namespace LanReciver
{
    public partial class Client : Form
    {

      byte[] buf = new byte[5000];
      public Client()
      {
        InitializeComponent();

      }

      private void button1_Click(object sender, EventArgs e)
      {
        backgroundWorker1.RunWorkerAsync();
      }
      public void call()
      {
        Socket mm = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        mm.Bind(new IPEndPoint(0, 5002));
        mm.Listen(100);
        Socket acc = mm.Accept();
        buf = new byte[acc.SendBufferSize];
        int byteread = acc.Receive(buf);
        byte[] rev = new byte[byteread];
        for (int i = 0; i < byteread; i++)
        {
          rev[i] = buf[i];
        }
        byteArrayToImage(rev);
        mm.Close();
        acc.Close();
        call();
      }

      public Image byteArrayToImage(byte[] byteArrayIn)
      {
        MemoryStream ms = new MemoryStream(byteArrayIn);
        Image returnImage = Image.FromStream(ms);
        return returnImage;
      }

      private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
      {
        call();
      }

      private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
        MessageBox.Show("Recieved");
      }


    }
}
解决方案

Here's a reasonably complete (albeit quick and dirty) solution:

Server:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;

namespace ImageServer
{
    static class Program
    {
        static void Main()
        {
            using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                socket.Bind(new IPEndPoint(IPAddress.Any, 23456));
                socket.Listen(100);
                while (true)
                {
                    using (var client = socket.Accept())
                    {
                        var bounds = Screen.PrimaryScreen.Bounds;
                        var bitmap = new Bitmap(bounds.Width, bounds.Height);
                        try
                        {
                            while (true)
                            {
                                using (var graphics = Graphics.FromImage(bitmap))
                                {
                                    graphics.CopyFromScreen(bounds.X, 0, bounds.Y, 0, bounds.Size);
                                }
                                byte[] imageData;
                                using (var stream = new MemoryStream())
                                {
                                    bitmap.Save(stream, ImageFormat.Png);
                                    imageData = stream.ToArray();
                                }
                                var lengthData = BitConverter.GetBytes(imageData.Length);
                                if (client.Send(lengthData) < lengthData.Length) break;
                                if (client.Send(imageData) < imageData.Length) break;
                                Thread.Sleep(1000);
                            }
                        }
                        catch
                        {
                            break;
                        }
                    }
                }
            }
        }
    }
}

Client (a form with a single button: "button1" and a single pictureBox: "pictureBox1"):

using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;

namespace ImageClient
{
    public partial class Form1 : Form
    {
        private Bitmap _buffer;

        public Form1()
        {
            InitializeComponent();
        }

        private void Button1Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            ThreadPool.QueueUserWorkItem(GetSnapshots);
        }

        private void GetSnapshots(object state)
        {
            using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                socket.Connect(new IPEndPoint(IPAddress.Loopback, 23456));
                while (true)
                {
                    var lengthData = new byte[4];
                    var lengthBytesRead = 0;
                    while (lengthBytesRead < lengthData.Length)
                    {
                        var read = socket.Receive(lengthData, lengthBytesRead, lengthData.Length - lengthBytesRead, SocketFlags.None);
                        if (read == 0) return;
                        lengthBytesRead += read;
                    }
                    var length = BitConverter.ToInt32(lengthData, 0);
                    var imageData = new byte[length];
                    var imageBytesRead = 0;
                    while (imageBytesRead < imageData.Length)
                    {
                        var read = socket.Receive(imageData, imageBytesRead, imageData.Length - imageBytesRead, SocketFlags.None);
                        if (read == 0) return;
                        imageBytesRead += read;
                    }
                    using (var stream = new MemoryStream(imageData))
                    {
                        var bitmap = new Bitmap(stream);
                        Invoke(new ImageCompleteDelegate(ImageComplete), new object[] { bitmap });
                    }
                }
            }
        }

        private delegate void ImageCompleteDelegate(Bitmap bitmap);
        private void ImageComplete(Bitmap bitmap)
        {
            if (_buffer != null)
            {
                _buffer.Dispose();
            }
            _buffer = new Bitmap(bitmap);
            pictureBox1.Size = _buffer.Size;
            pictureBox1.Invalidate();
        }

        private void PictureBox1Paint(object sender, PaintEventArgs e)
        {
            if (_buffer == null) return;
            e.Graphics.DrawImage(_buffer, 0, 0);
        }
    }
}

这篇关于插座不工作,因为它应该帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:19