问题 如何在异步套接字中福彩12选5走势图完整的屏幕截图/文本?

索伦国王

新成员
已加入
2019年6月25日
留言内容
1
编程经验
1-3
我有一个Java android代码,它将数据(图像或文本)发送到C#应用程序,以福彩12选5走势图这些数据。'm using 异步 插座。但是存在一个相对于 BeginReceive() 发送图像时,功能未福彩12选5走势图到完整的数据。那我该如何做"loop"福彩12选5走势图全部数据并在显示图像后 图片框 (for example)?

我总是出错 无效的参数 在这条线

C#:
var bitmap = new Bitmap(msInner);

在这里使用压缩也是如此:

C#:
z.CopyTo(msInner);

IvalidDataException

ServerReceivedImage() 方法分别。

使用这个:

C#:
File.WriteAllBytes(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "image.png"), newImage);

我注意到只福彩12选5走势图15KB(不使用压缩的文件大小)。

PS: android代码还可以。我的问题仅在于C#代码。

Form:
private Listener listener;
private Thread startListen;
private Bitmap _buffer;


    public frmMain()

    {

      InitializeComponent();

    }

    private void serverReceivedImage(Client client, byte[] image)

    {

        try

        {

            byte[] newImage = new byte[image.Length - 6];

   

            Array.Copy(image, 6, newImage, 0, newImage.Length);

   

                using (var stream = new MemoryStream(newImage))

                {

                    using (var msInner = new MemoryStream())

                    {

   

                      stream.Seek(2, SeekOrigin.Begin);

   

                      using (DeflateStream z = new DeflateStream(stream, CompressionMode.Decompress))

                      {

                        z.CopyTo(msInner);

                      }

   

                      msInner.Seek(0, SeekOrigin.Begin);

   

                      var bitmap = new Bitmap(msInner);

                      Invoke(new frmMain.ImageCompleteDelegate(ImageComplete), new object[] { bitmap });

                    }

                }

        }

         catch (Exception)

         {

            System.Diagnostics.Process.GetCurrentProcess().Kill();

         }

    }

   

    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 pictureBox1_Paint(object sender, PaintEventArgs e)

    {

      if (_buffer == null) return;

          e.Graphics.DrawImage(_buffer, 0, 0);

    }

   

    private void startToolStripMenuItem_Click(object sender, EventArgs e)

    {

      startListen = new Thread(listen);

      startListen.Start();

    }

   

    private void listen()

    {

      listener = new Listener();

      listener.BeginListen(101);

      listener.receivedImage += new Listener.ReceivedImageEventHandler(serverReceivedImage);

   

      startToolStripMenuItem.Enabled = false;

    }

Listener:
class Listener
{
   
        private Socket s;
        public List<Client> clients;
       
        public delegate void ReceivedImageEventHandler(Client client, byte[] image);
        public event ReceivedImageEventHandler receivedImage;
   
        private bool listening = false;
   
        public Listener()
        {
            clients = new List<Client>();
            s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }
   
        public bool Running
        {
            get { return listening; }
        }
   
        public void BeginListen(int port)
        {
            s.Bind(new IPEndPoint(IPAddress.Any, port));
            s.Listen(100);
            s.BeginAccept(new AsyncCallback(AcceptCallback), s);
            listening = true;
        }
   
        public void StopListen()
        {
            if (listening == true)
            {
                s.Close();
                listening = false;
            }
        }
   
        void AcceptCallback(IAsyncResult ar)
        {
            Socket handler = (Socket)ar.AsyncState;
            Socket sock = handler.EndAccept(ar);
            Client client = new Client(sock);
            clients.Add(client);
   
            sock.BeginReceive(client.buffer, 0, client.buffer.Length, SocketFlags.None, new AsyncCallback(ReadCallback), client);
           
            client.Send("REQUEST_PRINT" + Environment.NewLine);
           
            handler.BeginAccept(new AsyncCallback(AcceptCallback), handler);
        }
   
        void ReadCallback(IAsyncResult ar)
        {
   
            Client client = (Client)ar.AsyncState;
            try
            {
                int rec = client.sock.EndReceive(ar);
                if (rec != 0)
                {
                    string data = Encoding.UTF8.GetString(client.buffer, 0, rec);
   
                    if (data.Contains("SCREEN"))
                    {
                byte[] bytes = Encoding.UTF8.GetBytes(data);
                        receivedImage(client, bytes);
                    }
                    else // not is a image, is a text
                    {
                        // prepare text to show in TextBox
                    }
                }
                else
                {
                    return;
                }
   
                client.sock.BeginReceive(client.buffer, 0, client.buffer.Length, SocketFlags.None, new AsyncCallback(ReadCallback), client);
            }
            catch
            {
                client.sock.Close();
            }
        }
   
}

Client:
class Client
{
        public Socket sock;
        public byte[] buffer = new byte[8192];
   
        public Client(Socket sock)
        {
            this.sock = sock;
        }
   
        public void Send(string data)
        {
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            sock.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback((ar) =>
            {
                sock.EndSend(ar);
            }), buffer);
        }
}

Android code:
private byte[] compress(byte[] data) {
   
        Deflater deflater = new Deflater();
        deflater.setInput(data);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
        deflater.finish();
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
        byte[] output = outputStream.toByteArray();
   
        return output;
}
   
    public static DataOutputStream dos;
    public static byte[] array;
   
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
    array = compress(bos.toByteArray());
   
    //...
   
    dos = new DataOutputStream(SocketBackgroundService.clientSocket.getOutputStream());
    byte[] header = ("SCREEN").getBytes(StandardCharsets.UTF_8);
    byte[] dataToSend = new byte[header.length + array.length];
    System.arraycopy(header, 0, dataToSend, 0, header.length);
    System.arraycopy(array, 0, dataToSend, header.length, array.length);
    dos.writeInt(dataToSend.length);
    dos.write(dataToSend, 0, dataToSend.length);
   
    dos.flush();
 
最佳 底部