我试图在我的C#Winforms聊天项目上通过TCP流发送图片。问题是当它到达时你只能看到一张空白的照片。
这是我的代码:
客户端:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.IO;
namespace CLIENT
{
public partial class client : Form
{
Socket sock;
public client()
{
InitializeComponent();
sock = socket();
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem);
}
Socket socket()
{
return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
//
// Draw the background of the ListBox control for each item.
// Create a new Brush and initialize to a Black colored brush
// by default.
//
e.DrawBackground();
Brush myBrush = Brushes.Black;
//
// Determine the color of the brush to draw each item based on
// the index of the item to draw.
//
switch ((String)listBox1.Items[e.Index])
{
case "other:":
myBrush = Brushes.Green;
break;
case "you:":
myBrush = Brushes.Red;
break;
}
//
// Draw the current item text based on the current
// Font and the custom brush settings.
//
e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(),
e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
//
// If the ListBox has focus, draw a focus rectangle
// around the selected item.
//
e.DrawFocusRectangle();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms.ToArray();
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
// Bitmap bmp;
// using (var ms = new MemoryStream(byteArrayIn))
//{
// bmp = new Bitmap(ms);
//}
//return bmp;
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
private void connectionButton_Click(object sender, EventArgs e)
{
try
{
sock.Connect(new IPEndPoint(IPAddress.Parse(textBox2.Text), 3));
new Thread( () =>
{
read();
}).Start();
}
catch
{
MessageBox.Show("CONNECTION FAILED");
}
}
void read()
{
while(true)
{
try
{
byte[] buffer =new Byte[255];
int rec = sock.Receive(buffer,0, buffer.Length, 0);
if (rec <= 0)
{
throw new SocketException();
}
Array.Resize(ref buffer, rec);
Invoke((MethodInvoker)delegate
{
if (listBox1.Items.Count > 1)
{
if (listBox1.Items[listBox1.Items.Count - 1].Equals("hii"))
{
Image i=byteArrayToImage(buffer);
pictureBox1.Image =i ;
}
}
listBox1.Items.Add("other:");
listBox1.Items.Add(Encoding.Default.GetString(buffer));
});
}
catch
{
MessageBox.Show("DISCONNECTION!");
Application.Exit();
}
}
}
private void sendButton_Click(object sender, EventArgs e)
{
listBox1.Items.Add("you:");
listBox1.Items.Add(textBox1.Text);
byte[] data = Encoding.Default.GetBytes(textBox1.Text);
sock.Send(data, 0, data.Length, 0);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
服务器
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace server
{
public partial class server : Form
{
Socket sock;
Socket acc;
public server()
{
InitializeComponent();
//this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
//this.ShowInTaskbar = false;
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem);
}
Socket socket()
{
return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms.ToArray();
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
private void sendButton_Click(object sender, EventArgs e)
{
listBox1.Items.Add("you:");
listBox1.Items.Add(textBox1.Text);
byte[] data = Encoding.Default.GetBytes(textBox1.Text);
acc.Send(data, 0, data.Length, 0);
}
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
//
// Draw the background of the ListBox control for each item.
// Create a new Brush and initialize to a Black colored brush
// by default.
//
e.DrawBackground();
Brush myBrush = Brushes.Black;
//
// Determine the color of the brush to draw each item based on
// the index of the item to draw.
//
switch ((String)listBox1.Items[e.Index])
{
case "other:":
myBrush = Brushes.Green;
break;
case "you:":
myBrush = Brushes.Red;
break;
}
//
// Draw the current item text based on the current
// Font and the custom brush settings.
//
e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(),
e.Font, myBrush,e.Bounds,StringFormat.GenericDefault);
//
// If the ListBox has focus, draw a focus rectangle
// around the selected item.
//
e.DrawFocusRectangle();
}
private void listenButton_Click(object sender, EventArgs e)
{
sock = socket();
sock.Bind(new IPEndPoint(0, 3));
sock.Listen(0);
new Thread(() =>
{
acc = sock.Accept();
MessageBox.Show("CONNECTED ACCEPTED!");
sock.Close();
while(true)
{
try
{
byte[] buffer =new byte[255];
int rec =acc.Receive(buffer, 0, buffer.Length, 0);
if (rec<=0)
{
throw new SocketException();
}
Array.Resize(ref buffer, rec);
Invoke((MethodInvoker)delegate
{
listBox1.Items.Add("other:");
listBox1.Items.Add(Encoding.Default.GetString(buffer));
});
}
catch{
MessageBox.Show("DISCONNECTION!");
Application.Exit();
}
}
}).Start();
}
private void server_Load(object sender, EventArgs e)
{
//this.Size = new Size(0, 0);
}
private void button1_Click(object sender, EventArgs e)
{
byte[] data = imageToByteArray(Image.FromFile("c:\\ss\\gg.png"));
acc.Send(data, 0, data.Length, 0);
}
}
}
请帮忙!