使用socket从客户端传递截屏下来的图片到服务器端.出现错误提示:通常每个套接字地址(协议/网络地址/端口)只允许使用一次。帖出代码,大家帮忙看下是哪里出了问题.
下面是代码:
客户端截屏并且socket传递到服务器端:(C/S)using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Example023_屏幕捕获程序
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, //目标设备的句柄
int nXDest, // 目标对象的左上角的X坐标
int nYDest, // 目标对象的左上角的X坐标
int nWidth, // 目标对象的矩形的宽度
int nHeight, // 目标对象的矩形的长度
IntPtr hdcSrc, // 源设备的句柄
int nXSrc, // 源对象的左上角的X坐标
int nYSrc, // 源对象的左上角的X坐标
System.Int32 dwRop // 光栅的操作值
); [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtr CreateDC(
string lpszDriver, // 驱动名称
string lpszDevice, // 设备名称
string lpszOutput, // 无用,可以设定位"NULL"
IntPtr lpInitData // 任意的打印机数据
);
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
IntPtr dc1 = CreateDC("DISPLAY", null, null, (IntPtr)null);
//创建显示器的DC
Graphics g1 = Graphics.FromHdc(dc1);
//由一个指定设备的句柄创建一个新的Graphics对象
Bitmap MyImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, g1);
//根据屏幕大小创建一个与之相同大小的Bitmap对象
Graphics g2 = Graphics.FromImage(MyImage);
//获得屏幕的句柄
IntPtr dc3 = g1.GetHdc();
//获得位图的句柄
IntPtr dc2 = g2.GetHdc();
//把当前屏幕捕获到位图对象中
BitBlt(dc2, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, dc3, 0, 0, 13369376);
//把当前屏幕拷贝到位图中
g1.ReleaseHdc(dc3);
//释放屏幕句柄
g2.ReleaseHdc(dc2);
//释放位图句柄
MyImage.Save("d:\\MyJpeg.jpg", ImageFormat.Jpeg); //当然你也可以根据自己的需要,把屏幕以其他图片的格式来保存,如果你想把图片保存为位图文件,可以把"ImageFormat.Jpeg"改换成"ImageFormat.Bmp";想把图片保存为Gif文件,就把"ImageFormat.Jpeg"改换成"ImageFormat.Gif"。你可以保存的文件类型大概有十多种,这里就不一一介绍了,当然你也要相应改变保存文件的后缀。
pictureBox1.Image = MyImage;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; //本程序所用控件为1个按狃,一个panel,一个picturebox,其中panel的属性Autoscroll =true; picturebox的属性SizeMode =AutoSize; 然后只要改变pictureBox的大小,图象就会跟着改变大小,如果把这句去掉的话,就可以实现图象按panel滚动条移动而查看全图。 // MessageBox.Show("已经把当前屏幕保存到D:\\MyJpeg.jpg文件中!");
this.Show();
//***************************************************************************************开始使用socket发送文件*********************************************************//
System.Net.Sockets.Socket sendsocket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
//实例化socket
System.Net.IPEndPoint ipendpiont = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("192.168.1.1"), 8080); //服务器的IP和端口//因为客户端只是用来向特定的服务器发送信息,所以不需要绑定本机的IP和端口。不需要监听。
//建立终结点
System.IO.MemoryStream Stream = new System.IO.MemoryStream();
pictureBox1.Image.Save(Stream, System.Drawing.Imaging.ImageFormat.Bmp);//存储为bmp图象
// pictureBox1.Image.Save(Stream, System.Drawing.Imaging.ImageFormat.Jpeg); //不想存储为bmp图象,想存储为jpeg图象
byte[] b = Stream.ToArray(); sendsocket.Connect(ipendpiont);
//连接远程计算机
sendsocket.Send(b);
//发送 Stream.Close();
sendsocket.Shutdown(System.Net.Sockets.SocketShutdown.Send);
//关闭发送连接
sendsocket.Close(); //关闭本
}
}
}
服务器端接收图片并显示:(B/S)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;public partial class Receive : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//如何确定该数组大小
System.IO.MemoryStream fs = new System.IO.MemoryStream();
System.Net.Sockets.Socket receivesocket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
System.Net.IPEndPoint hostipendpiont = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("192.168.1.1"), 8080); //服务器端IP和端口号
receivesocket.Bind(hostipendpiont);
//建立远程计算机的的socket
receivesocket.Listen(2);
//设置接收数据缓冲区的大小
byte[] b = new byte[1765107];
//监听socket
System.Net.Sockets.Socket hostsocket = receivesocket.Accept(); //同意和发送端计算机建立连接
hostsocket.Receive(b);
fs.Write(b, 0, b.Length);
Bitmap Img = new Bitmap(fs);
//PictureBox1.Image = Img;
Img.Save("e:\\MyJpeg.jpg", ImageFormat.Jpeg);
this.Image1.ImageUrl = @"e:\MyJpeg.jpg";
fs.Close();
hostsocket.Shutdown(System.Net.Sockets.SocketShutdown.Receive);
hostsocket.Close();
}
}
解决方案 »
- C#中怎么调用三角函数呢?
- dataGridview 里面 添加 一个 编辑列 怎么操作
- 如何给自定义Dll文件生成元数据文件并查看
- 在程序中进行数据库的创建
- SQL连接问题!!
- 通过OpenFileDialog和FolderBrowserDialog浏览网上邻居时,输入的账户和密码,程序中能不能捕获?
- 请问,如何能让.net中C#开发的程序独立运行,不需要环境。
- .net有必要开个开源版块,支持的up,同时散分
- 呵呵,又换号了,再给200分,问两个关于DataGridView的具体问题。再次感谢刚才帮助和帮顶的朋友
- 有没有办法从hotmail里读取email?
- C#的winform中如何实现缩略图
- [50分]如何清除Grahpics所绘图形?
http://www.netcsharp.cn/topictag-24.aspxPS:有用的话,点一下广告支持下,谢谢咯^_^
发表于:2008-11-21 18:40:583楼 得分:0
你是不是在一个以上的窗体里面都初始化了同一个IP一个端口号~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
是啊,我在客户端给服务器传递图片,发送端填的是服务器端的IP和端口号.
接收端也是服务器的IP和端口号
是关不掉。
我在网上找到了解决方法。
贴出来给大家看看:C#:程序关闭了,为什么在后台还有进程存在?
以前也看到有朋友存在这个问题,当时没有在意。现在自己的项目中也遇到这种情况,略作研究,给大家分享一下。最终发现我的项目里面出现这种情况的原因是线程的创建和终结问题。解决办法有两种:A:在建立线程的时候指定这个线程是后台线程(.IsBackground属性为true).那么主线程关闭的时候它也会关闭.B:如果线程不是设置为后台线程,需要手动关闭,但是也有可能是abort不干净或者线程在等待什么的时候是不会关闭的.
楼
╭︿︿︿╮
{/ o o /}
( (oo) )
︶ ︶︶木有jj