C# 用socket,TCP,NetworkStream做的一个Image对象的传送,出错,请高手帮忙看一下那里的问题 你把ns的数据存储到 byte[] 里面,然后再存储照片; 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 to:iyori为什么要这么做啊? 先把ns存到byte[]里面,ns里面没有数据也存吗? 有另一种方法,来实现把你这个图片用Graphics对象缩小,然后再发送出去就可以了,一些视频就是这样做的。代码如下:using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Net;using System.Net.Sockets;using System.Threading;using System.IO;using System.Drawing.Imaging;using System.Drawing.Drawing2D;... //远端客户机 private EndPoint remotePoint=new IPEndPoint(IPAddress.Any,0); private Thread myThread; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); myThread=new Thread(new ThreadStart(ListenUdp)); myThread.Start(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if(myThread!=null){myThread.Abort();} if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }... public webcam wcam=null; private void Form1_Load(object sender, System.EventArgs e) { wcam=new webcam(panelLocalVideo.Handle,0,0,this.panelLocalVideo.Width,this.panelLocalVideo.Height); wcam.Start(); } private void btnSend_Click(object sender, System.EventArgs e) { this.timer1.Enabled=true; this.timer1.Interval=100; } public void ListenTcp() { IPEndPoint ipep=new IPEndPoint(IPAddress.Any,9050); Socket ListenSock=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); ListenSock.Bind(ipep); ListenSock.Listen(10); Socket client=ListenSock.Accept(); IPEndPoint clientipep=(IPEndPoint)client.RemoteEndPoint; NetworkStream ns=new NetworkStream(client); while(true) { this.picBoxRemoteVideo.Image=Image.FromStream(ns); } } public void sentMsgTcp(Image myPic) { IPEndPoint ipep=new IPEndPoint(IPAddress.Parse(this.txtBRemoteIP.Text),9050); Socket SendSocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); SendSocket.Connect(ipep); NetworkStream ns=new NetworkStream(SendSocket); //把图片以Bmp格式写入流ns myPic.Save(ns,ImageFormat.Jpeg); ns.Flush(); SendSocket.Close(); } public void ListenUdp() { byte[] data; int recv; IPEndPoint ipep=new IPEndPoint(IPAddress.Any,9050); Socket ListenSock=new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp); ListenSock.Bind(ipep); while(true) { data=new byte[20000000]; recv=ListenSock.ReceiveFrom(data,ref remotePoint); //反序列化 object msgObj=FormatterHelper.Deserialize(data); Bitmap mybm=(Bitmap)msgObj; //把图片放大充满容器,并给容器 this.picBoxRemoteVideo.Image=ChangImage(mybm,this.picBoxRemoteVideo.Width,this.picBoxRemoteVideo.Height); } } public void sentMsgUdp(Image myPic) { byte[] sendData=new byte[20000000]; IPEndPoint ipep=new IPEndPoint(IPAddress.Parse(this.txtBRemoteIP.Text),9050); Socket SendSocket=new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp); sendData = FormatterHelper.Serialize(myPic); SendSocket.SendTo(sendData,sendData.Length,SocketFlags.None,ipep); SendSocket.Close(); } private void timer1_Tick(object sender, System.EventArgs e) { IDataObject data; Image bmap; // Copy image to clipboard webcam.SendMessage(wcam.hWndC,webcam.WM_CAP_EDIT_COPY, 0, 0); // Get image from clipboard and convert it to a bitmap data = Clipboard.GetDataObject(); if (data.GetDataPresent(typeof(System.Drawing.Bitmap))) { bmap = ((Image)(data.GetData(typeof(System.Drawing.Bitmap)))); //把图片变小,供传送 bmap=ChangImage(bmap,150,150); sentMsgUdp(bmap); } } //把原Image转成小的Image public Image ChangImage(Image imageIn, int towidth,int toheight){ //新建一个bmp图片 Image bitmap = new Bitmap(towidth, toheight); //新建一个画板 Graphics g = Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(imageIn, new Rectangle(0, 0, towidth, toheight),new Rectangle(0, 0, imageIn.Width,imageIn.Height),GraphicsUnit.Pixel); return bitmap; } 这是序列化的代码:using System;using System.IO;using System.Runtime.Serialization.Formatters.Binary;namespace VideoTransmission{ /// <summary> /// FormatterHelper 序列化,反序列化消息的帮助类 /// </summary> public class FormatterHelper { public static byte[] Serialize(object obj) { BinaryFormatter binaryF = new BinaryFormatter(); MemoryStream ms = new MemoryStream(1024*10); binaryF.Serialize(ms, obj); ms.Seek(0, SeekOrigin.Begin); byte[] buffer = new byte[(int)ms.Length]; ms.Read(buffer, 0, buffer.Length); ms.Close(); return buffer; } public static object Deserialize(byte[] buffer) { BinaryFormatter binaryF = new BinaryFormatter(); MemoryStream ms = new MemoryStream(buffer, 0, buffer.Length, false); object obj = binaryF.Deserialize(ms); ms.Close(); return obj; } }} 未能映射路径“/inc/re.config”。 局域网winforms登陆验证失败问题 c#中的DataGrid数据导出excel中,如何加上excel标题?在线 日期格式转换出错,GMT,超出范围。。。 C#代码如何实现域名解析? 最好能给个源代码.C#(HTTP+POST+XML) 如何得到一个有水晶效果的图像? c#( winform-vs.net2008)在dataGirdView输入数据(添加和修改)后,点击button,实现access数据库的更新(非绑定) 谁来教教我封装(DLL),我是菜鸟:) 菜鸟问题,在线 关于C#窗体的问题 c# 颜色的问题
把你这个图片用Graphics对象缩小,然后再发送出去就可以了,一些视频就是这样做的。代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
...
//远端客户机
private EndPoint remotePoint=new IPEndPoint(IPAddress.Any,0);
private Thread myThread;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
myThread=new Thread(new ThreadStart(ListenUdp));
myThread.Start(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if(myThread!=null){myThread.Abort();}
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
...
public webcam wcam=null;
private void Form1_Load(object sender, System.EventArgs e)
{
wcam=new webcam(panelLocalVideo.Handle,0,0,this.panelLocalVideo.Width,this.panelLocalVideo.Height);
wcam.Start();
} private void btnSend_Click(object sender, System.EventArgs e)
{
this.timer1.Enabled=true;
this.timer1.Interval=100;
} public void ListenTcp()
{
IPEndPoint ipep=new IPEndPoint(IPAddress.Any,9050);
Socket ListenSock=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
ListenSock.Bind(ipep);
ListenSock.Listen(10);
Socket client=ListenSock.Accept(); IPEndPoint clientipep=(IPEndPoint)client.RemoteEndPoint;
NetworkStream ns=new NetworkStream(client);
while(true)
{
this.picBoxRemoteVideo.Image=Image.FromStream(ns);
}
} public void sentMsgTcp(Image myPic)
{
IPEndPoint ipep=new IPEndPoint(IPAddress.Parse(this.txtBRemoteIP.Text),9050);
Socket SendSocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
SendSocket.Connect(ipep); NetworkStream ns=new NetworkStream(SendSocket); //把图片以Bmp格式写入流ns
myPic.Save(ns,ImageFormat.Jpeg); ns.Flush();
SendSocket.Close();
} public void ListenUdp()
{
byte[] data;
int recv;
IPEndPoint ipep=new IPEndPoint(IPAddress.Any,9050);
Socket ListenSock=new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
ListenSock.Bind(ipep);
while(true)
{
data=new byte[20000000];
recv=ListenSock.ReceiveFrom(data,ref remotePoint);
//反序列化
object msgObj=FormatterHelper.Deserialize(data);
Bitmap mybm=(Bitmap)msgObj;
//把图片放大充满容器,并给容器
this.picBoxRemoteVideo.Image=ChangImage(mybm,this.picBoxRemoteVideo.Width,this.picBoxRemoteVideo.Height);
}
} public void sentMsgUdp(Image myPic)
{
byte[] sendData=new byte[20000000];
IPEndPoint ipep=new IPEndPoint(IPAddress.Parse(this.txtBRemoteIP.Text),9050);
Socket SendSocket=new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
sendData = FormatterHelper.Serialize(myPic);
SendSocket.SendTo(sendData,sendData.Length,SocketFlags.None,ipep);
SendSocket.Close();
} private void timer1_Tick(object sender, System.EventArgs e)
{
IDataObject data;
Image bmap; // Copy image to clipboard
webcam.SendMessage(wcam.hWndC,webcam.WM_CAP_EDIT_COPY, 0, 0); // Get image from clipboard and convert it to a bitmap
data = Clipboard.GetDataObject(); if (data.GetDataPresent(typeof(System.Drawing.Bitmap)))
{
bmap = ((Image)(data.GetData(typeof(System.Drawing.Bitmap)))); //把图片变小,供传送
bmap=ChangImage(bmap,150,150);
sentMsgUdp(bmap);
}
} //把原Image转成小的Image
public Image ChangImage(Image imageIn, int towidth,int toheight){
//新建一个bmp图片
Image bitmap = new Bitmap(towidth, toheight); //新建一个画板
Graphics g = Graphics.FromImage(bitmap); //设置高质量插值法
g.InterpolationMode = InterpolationMode.High; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(imageIn, new Rectangle(0, 0, towidth, toheight),new Rectangle(0, 0, imageIn.Width,imageIn.Height),GraphicsUnit.Pixel); return bitmap;
}
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;namespace VideoTransmission
{
/// <summary>
/// FormatterHelper 序列化,反序列化消息的帮助类
/// </summary>
public class FormatterHelper
{
public static byte[] Serialize(object obj)
{
BinaryFormatter binaryF = new BinaryFormatter();
MemoryStream ms = new MemoryStream(1024*10);
binaryF.Serialize(ms, obj);
ms.Seek(0, SeekOrigin.Begin);
byte[] buffer = new byte[(int)ms.Length];
ms.Read(buffer, 0, buffer.Length);
ms.Close();
return buffer;
} public static object Deserialize(byte[] buffer)
{
BinaryFormatter binaryF = new BinaryFormatter();
MemoryStream ms = new MemoryStream(buffer, 0, buffer.Length, false);
object obj = binaryF.Deserialize(ms);
ms.Close();
return obj;
}
}
}