C#使用Socket上传并保存图片的方法
本文实例讲述了C#使用Socket上传并保存图片的方法。分享给大家供大家参考。具分析如下:
使用stringfilename=openFile.FileName;即返回带全路径的文件名Path.GetFileNameWithoutExtension(filename)即可获得不带路径、后缀名的文件名。上传图片使用二进制tcp协议上传的
客户端代码:
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.Net; usingSystem.Net.Sockets; usingSystem.Threading; usingSystem.IO; namespacesocketClient { publicpartialclassForm1:Form { SocketclientSocket; privatestaticbyte[]result=newbyte[1024]; publicForm1() { InitializeComponent(); } privatevoidbuttonBegin_Click(objectsender,EventArgse) { //设定服务器IP地址 IPAddressip=IPAddress.Parse("127.0.0.1"); clientSocket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); try { clientSocket.Connect(newIPEndPoint(ip,8000)); //配置服务器IP与端口 } catch { MessageBox.Show("连接服务器失败"); return; } } privatevoidbuttonClose_Click(objectsender,EventArgse) { this.Close(); } privatevoidbuttonSelect_Click(objectsender,EventArgse) { OpenFileDialogopenFile=newOpenFileDialog(); openFile.Filter="图像文件(*.bmp;*.gif;*.jpg;*.jpeg;*.png)|*.bmp;*.gif;*.jpg;*.jpeg;*.png"; openFile.Multiselect=false; if(openFile.ShowDialog()==DialogResult.OK) { textBox2.Text=openFile.FileName; } //stringfilename=openFile.FileName; //即返回带全路径的文件名 //Path.GetFileNameWithoutExtension(filename) //即可获得不带路径、后缀名的文件名。 byte[]msg=Encoding.Default.GetBytes(Path.GetFileNameWithoutExtension(openFile.FileName)); clientSocket.Send(msg); try { //开始使用socket发送文件 FileStreamfs=newFileStream(openFile.FileName,FileMode.OpenOrCreate,FileAccess.Read); byte[]fssize=newbyte[fs.Length]; BinaryReaderstrread=newBinaryReader(fs); strread.Read(fssize,0,fssize.Length-1); clientSocket.Send(fssize); fs.Close(); clientSocket.Shutdown(System.Net.Sockets.SocketShutdown.Send); clientSocket.Close(); } catch(Exceptionex) { strings=ex.ToString(); return; } } } }
服务器端代码:
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.Net.Sockets; usingSystem.Net; usingSystem.Threading; usingSystem.IO; usingSystem.Drawing.Imaging; namespacesocketServer { publicpartialclassForm1:Form { privatestaticbyte[]result=newbyte[1024]; SocketserverSocket; publicForm1() { InitializeComponent(); } privatevoidbutton1_Click(objectsender,EventArgse) { try { IPAddressip=IPAddress.Parse("127.0.0.1"); IPEndPointiep=newIPEndPoint(ip,8000); serverSocket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); serverSocket.Bind(iep); serverSocket.Listen(10); byte[]recvBytes=newbyte[1024]; intbytes=newSocket.Receive(recvBytes,recvBytes.Length,SocketFlags.None);//从客户端接受信息 stringname=Encoding.ASCII.GetString(recvBytes,0,bytes); textBox1.Text=name+".jpg"; //设置接收数据缓冲区的大小 byte[]b=newbyte[1024*4]; MemoryStreamfs=newMemoryStream(); intgot=0; intdatalength=0; while(true) { got=newSocket.Receive(b); fs.Write(b,0,got); if(got>0) datalength=datalength+got; else break; } BitmapImg=newBitmap(fs); pictureBox1.Image=Img; stringfilename=name+".jpg"; Img.Save(@"D:images"+filename,ImageFormat.Jpeg); //关闭写文件流 fs.Close(); //关闭接收数据的Socket newSocket.Shutdown(System.Net.Sockets.SocketShutdown.Receive); newSocket.Close(); } catch(Exceptionse) { serverSocket.Close(); MessageBox.Show("连接错误"+se.ToString()); return; } } } }
希望本文所述对大家的C#程序设计有所帮助。