问题描述
最近做了一个聊天软件希望把图片通过二进制流的形式发送给客户端实现客户端能看到我发送的图片如何显示,我做的聊天软件本身作为服务器也是客户端的:下面是我的代码privateThreadtd;privateTcpListenertcpListener;privatestaticstringmessage="";privatevoidrtbContent_TextChanged(objectsender,EventArgse){}privatevoidF_Chat_Load(objectsender,EventArgse){td=newThread(newThreadStart(this.StartListen));td.Start();timer1.Start();this.toolStripStatusLabel3.Text="系统当前时间:"+DateTime.Now.ToString("yyyy-MM-ddhh:mm:ss");this.timer1.Interval=1000;this.timer1.Start();}privatevoidbutton3_Click(objectsender,EventArgse){Application.Exit();}privatevoidbutton2_Click(objectsender,EventArgse){try{IPAddress[]ip=Dns.GetHostAddresses(Dns.GetHostName());stringstrmsg=""+txtName.Text+"("+ip[0].ToString()+")"+DateTime.Now.ToLongTimeString()+"n"+""+this.rtbSend.Text+"n";TcpClientclient=newTcpClient(txtIP.Text,888);NetworkStreamnetstream=client.GetStream();StreamWriterwstream=newStreamWriter(netstream,Encoding.Default);wstream.Write(strmsg);wstream.Flush();wstream.Close();client.Close();rtbContent.SelectionColor=colorDialog1.Color;rtbContent.SelectionFont=fontDialog1.Font;rtbContent.AppendText(strmsg);rtbContent.ScrollToCaret();rtbSend.Clear();}catch(Exceptionex){MessageBox.Show(ex.Message);}}privatevoidStartListen(){message="";tcpListener=newTcpListener(888);tcpListener.Start();while(true){TcpClienttclient=tcpListener.AcceptTcpClient();//接受连接请求NetworkStreamnstream=tclient.GetStream();//获取数据流byte[]mbyte=newbyte[1024];//建立缓存inti=nstream.Read(mbyte,0,mbyte.Length);//将数据流写入缓存message=Encoding.Default.GetString(mbyte,0,i);}}privatevoidbutton1_Click(objectsender,EventArgse){rtbContent.Clear();}privatevoidF_Chat_FormClosed(objectsender,FormClosedEventArgse){if(this.tcpListener!=null){tcpListener.Stop();}if(td!=null){if(td.ThreadState==ThreadState.Running){td.Abort();}}}这一段是我的实现在RICHTEXXBOX里显示图片privatevoid插入图片ToolStripMenuItem_Click(objectsender,EventArgse){openFileDialog1.Filter="图片文件|*.jpg|所有文件|*.*";if(openFileDialog1.ShowDialog()==DialogResult.OK){Clipboard.SetDataObject(Image.FromFile(openFileDialog1.FileName),false);rtbSend.Paste();}}我想请教各位大虾如何把我打开的图片转换为二进制流发送到客户端的RICHTEXTBOX然后再解出来显示图片呢
解决方案
解决方案二:
客户端将接收到的二进制数据写成image文件,调用Clipboard.SetDataObject(Image.FromFile(openFileDialog1.FileName),false);rtbSend.Paste();
解决方案三:
关键我还没有把我的图片转换为二进制流,大虾如何把我打开的图片转换为二进制流的?
解决方案四:
Image的Save方法就可以存到流中啊,看帮助
解决方案五:
我自己项目中用到得方法A页面protectedvoidPage_Load(objectsender,EventArgse){Response.Write(ShowImage(@photo));//@photo服务器绝对路径如D:/ABC/A.jpg}privatestringShowImage(stringfilePath){string[]Files=System.IO.Directory.GetFiles(filePath);System.Text.StringBuildersb=newSystem.Text.StringBuilder();Stringp="";inti=0;foreach(stringfileinFiles){System.IO.FileInfofi=newSystem.IO.FileInfo(file);p+="<li><ahref='#'target='_blank'><imgid='ManImage'src='Image.aspx?path="+fi.FullName+"'runat='server'/></a></li>";i++;}returnp.ToString();}
Image画面protectedvoidPage_Load(objectsender,EventArgse){Response.BinaryWrite(this.GetImage(Request.QueryString["path"]));}privatebyte[]GetImage(stringpath){//文件异步读取(文件路径,打开文件,控制对文件的读取方式)using(System.IO.FileStreamfs=newSystem.IO.FileStream(path,System.IO.FileMode.Open,System.IO.FileAccess.Read)){//将基元数据类型读作二进制值using(System.IO.BinaryReaderreader=newSystem.IO.BinaryReader(fs)){returnreader.ReadBytes((int)fs.Length);reader.Close();//关闭流}fs.Close();}}
这样就OK了
解决方案六:
楼上的可以试试
解决方案七:
byte[]data=(byte[])ds.Tables[0].Rows[0]["IMAGE_DATA"];Streamms=newMemoryStream(data);Imageimage=System.Drawing.Image.FromStream(ms);
解决方案八:
protectedstringImgToBase64String(stringImagefilename){try{Bitmapbmp=newBitmap(Imagefilename);MemoryStreamms=newMemoryStream();bmp.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);byte[]arr=newbyte[ms.Length];ms.Position=0;ms.Read(arr,0,(int)ms.Length);ms.Close();returnConvert.ToBase64String(arr);}catch(Exceptionex){returnnull;}}//base64编码的字符串转为图片protectedBitmapBase64StringToImage(stringstrbase64){try{byte[]arr=Convert.FromBase64String(strbase64);MemoryStreamms=newMemoryStream(arr);Bitmapbmp=newBitmap(ms);//bmp.Save("test.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);//bmp.Save("test.bmp",ImageFormat.Bmp);//bmp.Save("test.gif",ImageFormat.Gif);//bmp.Save("test.png",ImageFormat.Png);ms.Close();returnbmp;}catch(Exceptionex){returnnull;}}
解决方案九:
//将RichTextBox的内容直接写入数据库:privatevoidbutton1_Click(objectsender,EventArgse)...{System.IO.MemoryStreammstream=newSystem.IO.MemoryStream();this.richTextBox1.SaveFile(mstream,RichTextBoxStreamType.RichText);//将流转换成数组byte[]bWrite=mstream.ToArray();//将数组写入数据库System.Data.SqlClient.SqlParameter[]pram=...{sqlHelper.MakeInParam("@XX",System.Data.SqlDbType.Image)};pram[0].Value=bWrite;sqlHelper.RunSql("insertintoXXX(XX)values(@XX)",pram);}//将数据库中的RTF读出并填充到RichTextBoxprivatevoidbutton2_Click(objectsender,EventArgse)...{//从数据库中读出数据DataTabledt=sqlHelper.GetDataTable("selectXXfromXXXwhere.....");byte[]bWrite=(byte[])dt.Rows[0][0];//将数组转换成streamSystem.IO.MemoryStreammstream=newSystem.IO.MemoryStream(bWrite,false);//将stream填充到RichTextBoxthis.richTextBox1.LoadFile(mstream,RichTextBoxStreamType.RichText);}
解决方案十:
//将RichTextBox的内容直接写入数据库:privatevoidbutton1_Click(objectsender,EventArgse)...{System.IO.MemoryStreammstream=newSystem.IO.MemoryStream();this.richTextBox1.SaveFile(mstream,RichTextBoxStreamType.RichText);//将流转换成数组byte[]bWrite=mstream.ToArray();//将数组写入数据库System.Data.SqlClient.SqlParameter[]pram=...{sqlHelper.MakeInParam("@XX",System.Data.SqlDbType.Image)};pram[0].Value=bWrite;sqlHelper.RunSql("insertintoXXX(XX)values(@XX)",pram);}//将数据库中的RTF读出并填充到RichTextBoxprivatevoidbutton2_Click(objectsender,EventArgse)...{//从数据库中读出数据DataTabledt=sqlHelper.GetDataTable("selectXXfromXXXwhere.....");byte[]bWrite=(byte[])dt.Rows[0][0];//将数组转换成streamSystem.IO.MemoryStreammstream=newSystem.IO.MemoryStream(bWrite,false);//将stream填充到RichTextBoxthis.richTextBox1.LoadFile(mstream,RichTextBoxStreamType.RichText);}
来自http://blog.csdn.net/leomaya/article/details/1608701,以方便大家,我找了两个小时才找到.