如何将图片转换为二进制流发送出去在客户端里的RICHTEXTBOX显示?
最近做了一个聊天软件希望把图片通过二进制流的形式发送给客户端实现客户端能看到我发送的图片如何显示,我做的聊天软件本身作为服务器也是客户端的:下面是我的代码private Thread td;
private TcpListener tcpListener;
private static string message = "";
private void rtbContent_TextChanged(object sender, EventArgs e)
{
}
private void F_Chat_Load(object sender, EventArgs e)
{
td = new Thread(new ThreadStart(this.StartListen));
td.Start();
timer1.Start();
this.toolStripStatusLabel3.Text = "系统当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
this.timer1.Interval = 1000;
this.timer1.Start();
}
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
try
{
IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());
string strmsg = " " + txtName.Text + "(" + ip[0].ToString() + ") " + DateTime.Now.ToLongTimeString() + "\n" + " " + this.rtbSend.Text + "\n";
TcpClient client = new TcpClient(txtIP.Text, 888);
NetworkStream netstream = client.GetStream();
StreamWriter wstream = new StreamWriter(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 (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void StartListen()
{
message = "";
tcpListener = new TcpListener(888);
tcpListener.Start();
while (true)
{
TcpClient tclient = tcpListener.AcceptTcpClient(); //接受连接请求
NetworkStream nstream = tclient.GetStream(); //获取数据流
byte[] mbyte = new byte[1024]; //建立缓存
int i = nstream.Read(mbyte, 0, mbyte.Length); //将数据流写入缓存
message = Encoding.Default.GetString(mbyte, 0, i);
}
}
private void button1_Click(object sender, EventArgs e)
{
rtbContent.Clear();
}
private void F_Chat_FormClosed(object sender, FormClosedEventArgs e)
{
if (this.tcpListener != null)
{
tcpListener.Stop();
}
if (td != null)
{
if (td.ThreadState == ThreadState.Running)
{
td.Abort();
}
}
}
这一段是我的实现在RICHTEXXBOX里显示图片
private void 插入图片ToolStripMenuItem_Click(object sender, EventArgs e)
{
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页面
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(ShowImage(@photo));//@photo服务器绝对路径如D:/ABC/A.jpg
}
private string ShowImage(string filePath)
{
string[] Files = System.IO.Directory.GetFiles(filePath);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
String p = "";
int i = 0;
foreach (string file in Files)
{
System.IO.FileInfo fi = new System.IO.FileInfo(file);
p += "<li><a href='#' target='_blank'><img id='ManImage' src='Image.aspx?path=" + fi.FullName + "' runat='server' /></a></li>";
i++;
}
return p.ToString();
}
Image画面
protected void Page_Load(object sender, EventArgs e)
{
Response.BinaryWrite(this.GetImage(Request.QueryString["path"]));
}
private byte[] GetImage(string path)
{
//文件异步读取(文件路径,打开文件,控制对文件的读取方式)
using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
//将基元数据类型读作二进制值
using (System.IO.BinaryReader reader = new System.IO.BinaryReader(fs))
{
return reader.ReadBytes((int)fs.Length);
reader.Close();//关闭流
}
fs.Close();
}
}
这样就OK 了 --------------------编程问答-------------------- 楼上的可以试试 --------------------编程问答-------------------- byte[] data = (byte[])ds.Tables[0].Rows[0]["IMAGE_DATA"];
Stream ms = new MemoryStream(data);
Image image= System.Drawing.Image.FromStream(ms); --------------------编程问答-------------------- protected string ImgToBase64String(string Imagefilename)
{
try
{
Bitmap bmp = new Bitmap(Imagefilename);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
return Convert.ToBase64String(arr);
}
catch (Exception ex)
{
return null;
}
}
//base64编码的字符串转为图片
protected Bitmap Base64StringToImage(string strbase64)
{
try
{
byte[] arr = Convert.FromBase64String(strbase64);
MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(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();
return bmp;
}
catch (Exception ex)
{
return null;
}
}
--------------------编程问答-------------------- //将RichTextBox的内容直接写入数据库:
private void button1_Click(object sender, EventArgs e)
...{
System.IO.MemoryStream mstream = new System.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("insert into XXX (XX) values (@XX)", pram);
}
//将数据库中的RTF读出并填充到RichTextBox
private void button2_Click(object sender, EventArgs e)
...{
//从数据库中读出数据
DataTable dt=sqlHelper.GetDataTable("select XX from XXX where .....");
byte[] bWrite = (byte[])dt.Rows[0][0];
//将数组转换成stream
System.IO.MemoryStream mstream = new System.IO.MemoryStream(bWrite, false);
//将stream填充到RichTextBox
this.richTextBox1.LoadFile(mstream, RichTextBoxStreamType.RichText);
}
--------------------编程问答--------------------
//将RichTextBox的内容直接写入数据库:
private void button1_Click(object sender, EventArgs e)
...{
System.IO.MemoryStream mstream = new System.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("insert into XXX (XX) values (@XX)", pram);
}
//将数据库中的RTF读出并填充到RichTextBox
private void button2_Click(object sender, EventArgs e)
...{
//从数据库中读出数据
DataTable dt=sqlHelper.GetDataTable("select XX from XXX where .....");
byte[] bWrite = (byte[])dt.Rows[0][0];
//将数组转换成stream
System.IO.MemoryStream mstream = new System.IO.MemoryStream(bWrite, false);
//将stream填充到RichTextBox
this.richTextBox1.LoadFile(mstream, RichTextBoxStreamType.RichText);
}
来自http://blog.csdn.net/leomaya/article/details/1608701,以方便大家,我找了两个小时才找到.
补充:.NET技术 , C#