龙空技术网

C#用Socket发送图片实例

逍遥总遥 1842

前言:

现时大家对“net获取上传图片字节数”大概比较关怀,大家都想要知道一些“net获取上传图片字节数”的相关知识。那么小编同时在网上网罗了一些对于“net获取上传图片字节数””的相关资讯,希望我们能喜欢,看官们一起来了解一下吧!

用socket同步发送,异步发送字符串,前几天已经搞定,今天给大家说一个服务端向客户端发送图片的例子。

效果如下:

1、 新建一个Student做为发送端,放一个picturebox显示图片,界面如图

代码如下:

using System;using System.Drawing;using System.IO;using System.Net;using System.Net.Sockets;using System.Text;using System.Windows.Forms;namespace Student{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        //建立全局对象,因为要在不同的地方用到。        IPEndPoint TheServerOBJ = null;        Socket socket = null;        private void button1_Click(object sender, EventArgs e)        {            //要连接的服务器IP与端口            IPEndPoint TheServerOBJ = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7878);            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            //尝试连接服务器            socket.BeginConnect(TheServerOBJ, new AsyncCallback(Sending), socket);        }        private void Sending(IAsyncResult rec_socket)        {            Socket socket = (Socket)rec_socket.AsyncState;            try            {                if (socket.Connected)                {                    //如果成功连接了,就把图片内容整到内存流中                    MemoryStream memoryStream = new MemoryStream();                    pictureBox1.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);                    //把这个图片读到byte中                    byte[] thebytes = memoryStream.ToArray();                    //转成base64string字符                    string imgstr = Convert.ToBase64String(thebytes);                    //再将字符转成byte发送                    byte[] bytes = Encoding.UTF8.GetBytes(imgstr);                    socket.Send(bytes);                }                else                {                    //如果没连接成功,再次尝试连接                    socket.BeginConnect(TheServerOBJ, new AsyncCallback(Sending), socket);                }            }            catch(Exception Ex)            {                //出错了,不管,再次尝试连接                socket.BeginConnect(TheServerOBJ, new AsyncCallback(Sending), socket);            }        }    }}
2、建立一个Teacher项目,做为接收端,也放一个picturebox上去,用来显示接收到的图片,界面:

代码如下:

using System;using System.Drawing;using System.IO;using System.Net;using System.Net.Sockets;using System.Runtime.Remoting.Contexts;using System.Runtime.Remoting.Messaging;using System.Text;using System.Windows.Forms;namespace Teacher{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void pictureBox1_Click(object sender, EventArgs e)        {            //点击图片让图片没有,为了调试方便            pictureBox1.Image = null;        }        private void Form1_Load(object sender, EventArgs e)        {            //做为socket服务器的信息,IP及端口            IPEndPoint IamServer = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7878);            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            socket.Bind(IamServer);            //这个数字据称是客户端数量            //我确实没有理解这是个啥,反正我都是写1000也没出过问题            socket.Listen(1000);            //开始接收客户端信息            socket.BeginAccept(new AsyncCallback(RecVing), socket);        }        //回调函数        private void RecVing(IAsyncResult rec_socket)        {            Socket socket = (Socket)rec_socket.AsyncState;            //继续准备下次的接收            socket.BeginAccept(new AsyncCallback(RecVing), socket);            //-----------------处理本次信息            Socket socketOld = socket.EndAccept(rec_socket);            string cmdstr = "";            byte[] recvBytes = new byte[1024];            int i = 0;            //完整接收收到的信息            do            {                i = socketOld.Receive(recvBytes, recvBytes.Length, 0);                cmdstr += Encoding.UTF8.GetString(recvBytes, 0, i);            }            while (i == 1024);            //处理图片字符            this.Invoke(new Action(() =>            {                showstrs(cmdstr);            }            ));        }        //具体操作        private void showstrs(string imgstrs)        {            int imgstrlen = imgstrs.Length;            if (imgstrlen > 4 & imgstrlen % 4 == 0)            {                //把base64string的字符弄到byte中                byte[] bytesimg = Convert.FromBase64String(imgstrs.Replace(" ", "+"));                MemoryStream memoryStream = new MemoryStream();                memoryStream.Write(bytesimg, 0, bytesimg.Length);                Image bitmap = Image.FromStream(memoryStream);                //把图片弄上去                pictureBox1.Image = bitmap;            }            else            {                MessageBox.Show("图片的 Base64String 有错误,转换失败。");            }        }    }}

结语:imgstrs.Replace(" ", "+")这一句其实并不需要,我怀疑是我这台机器的问题或者是编码问题,我得到的字符串中居然有空格,我对比了一下,发现应该是“+”,所以我替换了一下,不过写上也是绝无问题的。

标签: #net获取上传图片字节数