public void ReceiveImage() { int GetImageLength; int GetImageArea; int RealImageLength; MemoryStream ms = new MemoryStream(); IPBindPoint_ForImage = new IPEndPoint(ServerIP, sPort + 1); IPBindPoint_ForImage = new IPEndPoint(((IPEndPoint)Client[0].RemoteEndPoint).Address, sPort + 1); senderp = (IPEndPoint)IPBindPoint_ForImage; Bitmap GetImageBit; //Thread.Sleep(10000); try { ImageServer.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 500); while (Client[0].Connected) { //Get Image Size //ImageServer.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 0); Client[0].Receive(GetImageSize, 0, 4, SocketFlags.None); RealImageLength = BitConverter.ToInt32(GetImageSize, 0); Client[0].Receive(GetImageSize, 0, 4, SocketFlags.None); GetImageLength = BitConverter.ToInt32(GetImageSize, 0); if (GetImageLength == 0) { continue; } //Get Image Area /*ImageClient.Receive(GetImageAreaByte, 0, 4, SocketFlags.None); * GetImageArea = BitConverter.ToInt32(GetImageAreaByte, 0);*/ GetImageBytes = new byte[GetImageLength]; //ImageServer.Receive(GetImageBytes, 0, GetImageLength, SocketFlags.None); //ImageServer.ReceiveFrom(GetImageBytes, 0, GetImageLength, SocketFlags.None, ref senderp); int count = 0; int icount = 0; sw = false; do { try { GetByte = new byte[SizeForOne + 4]; for (int i = icount; i < GetImageLength / SizeForOne; i++) { //ChatBoxAdd(ImageServer.ReceiveFrom(GetByte, 0, SizeForOne + 4, SocketFlags.None, ref senderp).ToString()); ImageServer.BeginReceiveFrom(GetByte, 0, SizeForOne + 4, SocketFlags.None, ref senderp, new AsyncCallback(MessageCallBack), Tuple.Create(GetByte, 0, GetImageLength)); } Client[0].Send(new byte[4]); Client[0].Receive(new byte[4]); if (GetImageLength % SizeForOne != 0) { ImageServer.ReceiveFrom(GetImageBytes, GetImageLength - (GetImageLength % SizeForOne), GetImageLength % SizeForOne, SocketFlags.None, ref senderp); } sw = true; Client[0].Send(BitConverter.GetBytes(-1)); } catch (Exception Ex3) { ChatBoxAdd(Ex3.ToString()); ChatBoxAdd("UDP 데이터 소실 발생."); sw = false; icount = 0; Client[0].Send(BitConverter.GetBytes(-1)); break; Client[0].Send(BitConverter.GetBytes(icount)); } } while (!sw); try { if (sw == false) { throw new Exception(); } ms = new MemoryStream(); ms.Write(GetImageBytes, 0, (int)GetImageBytes.Length); ms.Position = 0; GetImageBytes = new byte[RealImageLength]; //var DecompressStream = new DeflateStream(ms, CompressionMode.Decompress,true); var DecompressStream = new GZipStream(ms, CompressionMode.Decompress, true); var DecompressedStream = new MemoryStream(); DecompressStream.CopyTo(DecompressedStream); DecompressStream.Close(); DecompressedStream.Position = 0; Bitmap Image = new Bitmap(DecompressedStream); //Bitmap Image = new Bitmap(ms); ChatBoxAdd("Success!"); MainPicture.MainPictureChange(Image); //pictureBox1Change(Image); Orginal_Image = Image; ms.Close(); DecompressedStream.Close(); ImageServer.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 0); Client[0].Send(new byte[4]); sw = true; } catch (Exception ex) { ChatBoxAdd(ex.ToString()); ImageServer.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 0); Client[0].Send(new byte[4]); sw = true; } GetImageBytes = null; //Thread.Sleep(100); GetImageBit = null; GC.Collect(0); GC.Collect(1); } } catch (Exception Ex2) { ChatBoxAdd(Ex2.ToString()); } }
private void toolStripButton1_Click(object sender, EventArgs e) { if (goodInput && !loading) { loading = true; toolStripButton1.Enabled = false; if (iterations >= 100000) { DialogResult option = MessageBox.Show("Running " + iterations + " simulations will " + "take approximately: " + getTime() + "\nDo you wish to continue?", "Alert", MessageBoxButtons.OKCancel); if (option == DialogResult.Cancel) { goto Done; } } try { if (pictureBox1.Image != null) { pictureBox1.Image.Dispose(); } } catch (Exception Ex0) { var nullErrorMessageBox = MessageBox.Show ("ERROR disposing old image file! \nOutput: " + Ex0.ToString(), "Alert", MessageBoxButtons.OK); } RscriptWriter(); userInputScript = rPathDest; Console.WriteLine("userInputScript: " + userInputScript); outputLocation = RpathFinder(); Console.WriteLine("outputLocation: " + outputLocation); try { RunFromCmd(userInputScript, userInputCSV, outputLocation, iterations.ToString(), distributionType); } catch (Exception Ex1) { var nullErrorMessageBox = MessageBox.Show ("ERROR on CMD process! \nOutput: " + Ex1.ToString(), "Alert", MessageBoxButtons.OK); } try { Cursor.Current = Cursors.WaitCursor; toolStripButton1.BackColor = Color.GreenYellow; if (iterations >= 10000000) { Thread.Sleep(Convert.ToInt32(iterations * 0.10)); } else if (iterations >= 100000 && iterations < 10000000) { Thread.Sleep(Convert.ToInt32(iterations * 0.30)); } else if (iterations >= 10000 && iterations < 100000) { Thread.Sleep(Convert.ToInt32(iterations * 0.50)); } else { Thread.Sleep(2500); } } catch (Exception Ex2) { var nullErrorMessageBox = MessageBox.Show ("ERROR waiting for process to complete! \nOuput: " + Ex2.ToString(), "Alert", MessageBoxButtons.OK); } try { pictureBox1.Image = Image.FromFile(RpathFinder() + @"\output.png"); } catch (Exception Ex3) { var nullErrorMessageBox = MessageBox.Show ("ERROR loading the image file! \nOuput: " + Ex3.ToString(), "Alert", MessageBoxButtons.OK); } Done: try { if (!string.IsNullOrWhiteSpace(userInputScript)) { DeleteTempFile(userInputScript); } //if (!string.IsNullOrWhiteSpace(userInputScript)) //DeleteTempFile(RpathFinder() + @"\output.png"); loading = false; maxWarning = false; minWarning = false; } catch (Exception Ex4) { var nullErrorMessageBox = MessageBox.Show ("ERROR on cleanup process! \nOutput: " + Ex4.ToString(), "Alert", MessageBoxButtons.OK); } } }