c# - Show only small part of picture -


i have write program consists server , client. server makes screenshot desktop, , client receives screenshot , shows on form. there problem in data transfer, see small strip. checked before sending image on server ok. however, client receives wrong image. wrong? xaml:

<window x:class="testtcp.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     title="testtcp" height="350" width="525"> <grid>     <image x:name="img1" margin="0"/>  </grid> </window> 

code c#:

  public partial class mainwindow : window   {     public mainwindow()     {         initializecomponent();          task rec = new task(waitclientquery);         rec.start();          task tk = new task(send);         tk.start();     }      private void send()//server part. take screen , send image     {         bitmap temp = capturescreen(true);         memorystream ms3 = new memorystream();         bitmapimage image = new bitmapimage();         byte[] img_byte;         try         {             ((system.drawing.bitmap)temp).save(ms3, system.drawing.imaging.imageformat.bmp);             image.begininit();             ms3.seek(0, seekorigin.begin);             image.streamsource = ms3;             image.endinit();              jpegbitmapencoder encoder = new jpegbitmapencoder();             encoder.frames.add(bitmapframe.create(image));             using (memorystream ms = new memorystream())             {                 encoder.save(ms);                 img_byte = ms.toarray();             }              try             {                  tcpclient client = new tcpclient("192.168.1.64", 4444);                 networkstream netstream = client.getstream();                 netstream.write(img_byte, 0, img_byte.length);                 netstream.close();                 client.close();             }             catch (exception)             {              }         }         catch (exception ee)         {          }     }      [structlayout(layoutkind.sequential)]     struct cursorinfo     {         public int32 cbsize;         public int32 flags;         public intptr hcursor;         public pointapi ptscreenpos;     }      [structlayout(layoutkind.sequential)]     struct pointapi     {         public int x;         public int y;     }      [dllimport("user32.dll")]     static extern bool getcursorinfo(out cursorinfo pci);      [dllimport("user32.dll")]     static extern bool drawicon(intptr hdc, int x, int y, intptr hicon);      const int32 cursor_showing = 0x00000001;      public static bitmap capturescreen(bool capturemouse)     {         bitmap result = new bitmap(screen.primaryscreen.bounds.width, screen.primaryscreen.bounds.height);        try         {             using (graphics g = graphics.fromimage(result))             {                 g.copyfromscreen(0, 0, 0, 0, screen.primaryscreen.bounds.size, copypixeloperation.sourcecopy);                  if (capturemouse)                 {                     cursorinfo pci;                     pci.cbsize = system.runtime.interopservices.marshal.sizeof(typeof(cursorinfo));                      if (getcursorinfo(out pci))                     {                         if (pci.flags == cursor_showing)                         {                             drawicon(g.gethdc(), pci.ptscreenpos.x, pci.ptscreenpos.y, pci.hcursor);                             g.releasehdc();                         }                     }                 }             }         }         catch         {             result = null;         }          return result;     }      void waitclientquery()//receive data(client)     {         try         {             ipaddress ip ;             ipaddress.tryparse("192.168.1.64", out ip);             tcplistener listener = new tcplistener(ip, 4444);             listener.start();              while (true)             {                 tcpclient client = listener.accepttcpclient();                 thread thread = new thread(new parameterizedthreadstart(readmessage));                 thread.isbackground = true;                 thread.start(client);             }         }         catch (exception ex)         {         }     }      void readmessage(object obj)     {         try         {             tcpclient client = (tcpclient)obj;             networkstream netstream = client.getstream();             byte[] arr = new byte[client.receivebuffersize ];             int len = netstream.read(arr, 0, client.receivebuffersize);             if (len > 0)             {                 try                 {                      action act = delegate                                      {                                             memorystream strmimg = new memorystream(arr);                                             bitmapimage mybitmapimage = new bitmapimage();                                             mybitmapimage.begininit();                                             mybitmapimage.streamsource = strmimg;                                             mybitmapimage.endinit();                                             img1.source = mybitmapimage;                                      };                     this.dispatcher.begininvoke(act);                  }                 catch (exception ex)                 {                     string message = ex.message;                 }              }             netstream.close();             client.close();          }         catch (exception ex)         {         }     }  } 

p.s. result, screen program after compile: click view

the main root cause client.receivebuffersize using default value 8192 byte client not receive enough image has been transferred server. in code, see @ client receive correctly size of image use binarywriter transfer length of image. client side have used binaryreader read number of bytes , size of image save bandwidth. hope help

at send method:

tcpclient client = new tcpclient("192.168.1.64", 4444);  using (networkstream netstream = client.getstream())  {      using (binarywriter bw = new binarywriter(netstream))      {         bw.write(img_byte.length);         bw.write(img_byte, 0, img_byte.length);      }       client.close();  } 

at readmessage(object obj)

private void readmessage(object obj)     {         try         {             byte[] arr = null;             tcpclient client = (tcpclient) obj;             using (networkstream netstream = client.getstream())             {                 using (binaryreader br = new binaryreader(netstream))                 {                     var arrlen = new byte[4];                     br.read(arrlen, 0, 4);                     int len = bitconverter.toint32(arrlen, 0);                     if (len > 0)                     {                         arr = new byte[len];                          int read = 0;                         while (read != len)                         {                             read += br.read(arr, read, arr.length - read);                         }                     }                 }             }              if (arr != null && arr.length > 0)             {                 try                 {                      action act = delegate                         {                             memorystream strmimg = new memorystream(arr);                             bitmapimage mybitmapimage = new bitmapimage();                             mybitmapimage.begininit();                             mybitmapimage.streamsource = strmimg;                             mybitmapimage.endinit();                             img1.source = mybitmapimage;                         };                     this.dispatcher.begininvoke(act);                  }                 catch (exception ex)                 {                     string message = ex.message;                 }              }              client.close();         }         catch (exception ex)         {         }     } 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -