当前位置:编程学习 > C#/ASP.NET >>

C# 用socket,TCP,NetworkStream做的一个Image对象的传送,出错,请高手帮忙看一下那里的问题

我的代码:
		public void Listen()
{
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);

StreamReader sr=new StreamReader(ns);

this.picBoxRemoteVideo.Image=Image.FromStream(ns);
}

public void sentMsg(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);

//把图片写入流ns
myPic.Save(ns,ImageFormat.Jpeg);

ns.Flush();
SendSocket.Close();
}

出错:
GDI+ 中发生一般性错误。发生在myPic.Save(ns,ImageFormat.Jpeg);这句
使用了无用的参数:this.picBoxRemoteVideo.Image=Image.FromStream(ns); --------------------编程问答-------------------- 你把ns的数据存储到 byte[] 里面,然后再存储照片;
--------------------编程问答-------------------- 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;
}
}
}
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,