c# - Sending jpg using tcp socket -


I have a problem sending JPG data via socket.

From: Socket Customer_s = New Socket (AddressedInternetwork, SocketType Stream, Protocol Type. TCP); Ipdpoint serverpe = new ipadpoint (ipadress loopback, 4567); Client_s.Connect (serverEP); Network Stream Stream = New Network Stream (Client_s); Bmp.Save (stream, system, drawing. Imaging.mage format.jpg);

To the receiver:

  clientSocket = listenSocket.Accept (); NetworkStream S = new networkstream (client socket); Bitmap slice_bmp = new bitmap (s); Graphics G = this.CreateGraphics (); G.DrawImage (slice_bmp, 0,0);   

When I execute the above, bitmap slice_bmp = new bitmap (s); The sender's socket is blocked until it is closed. After the socket closure of the sender, an image is normally prepared. I want to know why the operation is blocked?

In addition to this, is this example of networkstream right in the form of the argument of the creator of Bitmap? (My code is based on heritage)

First of all, I'll give you the flush () method to networkstream to ensure that all the data has been sent

  socket customer_s = new socket (addressfind.Internetwork, socket type. Stream, protocol type. TCP); Ipdpoint serverpe = new ipadpoint (ipadress loopback, 4567); Client_s.Connect (serverEP); Network Stream Stream = New Network Stream (Client_s); Bmp.Save (stream, system, drawing. Imaging.mage format.jpg); Stream.Flush (); // flush everything   

toward the receiver, you should first read all the bytes of the image in the byte array. From that stream you can create from that stream with MemoryStream and Bitmap .

  clientSocket = listenSocket.Accept (); NetworkStream S = new networkstream (client socket); Int BitesRad = 0; Int howMany = 0; Byte [] bytebuffer = new byte [100000]; Do {how to = s.Read (byte buffer, bytes read, 10000); BytesRed + = How many; If (bytes read> gt; byte buffer; lang) bitbuffer = resizebyte (bytebuffer); } While (how I & gt; 0 & amp; amp; S. data is available); Memorystream ms = new memorystream (bytebuffer); Bitmap slice_bmp = new bitmap (ms); Graphics G = this.CreateGraphics (); G.DrawImage (slice_bmp, 0, 0); G.Dispose ();   

If the image is larger than the capacity of the generated byte array, then you need to resize it. You can use this helpful method:

  private byte [] ResizeByteArray (byte [] arr) {byte] newArr = new byte [arr.Length * 2]; Array.Copy (ARM, Newer, AR Lamp); Make a new comeback; }   

and as a side note, always dispose on the graphics object that you create manually (this case like). I checked the code and it works.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -