http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2076|回复: 1

[其他] BinaryReader参数错误

[复制链接]
发表于 2013-10-11 17:40:54 | 显示全部楼层 |阅读模式
服务端发送图片
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;

namespace 发图片
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Socket FuWuSocket;
        Socket KeHuSocket;
        Byte[] photo;
        private void Form1_Load(object sender, EventArgs e)
        {
            IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 6050);
            FuWuSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            FuWuSocket.Bind(ipe);
            FuWuSocket.Listen(10);
            KeHuSocket = FuWuSocket.Accept();
            Form1 from = new Form1();
            from.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread1=new Thread(new ThreadStart(fasong));
            thread1.Start();
        }
        private void fasong()
        {
            for (int i = 0; i <5;i++ )
            {
                Image img = new Bitmap(@"D:\tupian\" + i.ToString() + ".jpg");
                System.IO.MemoryStream mem = new MemoryStream();    //内存流
                img.Save(mem, ImageFormat.Bmp);                      //将图片保存到指定的流
                BinaryReader br = new BinaryReader(mem);
                photo = mem.ToArray();
                mem.Close();
                KeHuSocket.Send(photo);
                if(i==4)
                {
                    i = 0;
                }
                Thread.Sleep(50);
            }

        }
    }
}
客户端接收图片
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;

namespace 接图片
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Socket KeHuSocket;
        Byte[] byte1;
        private void Form1_Load(object sender, EventArgs e)
        {
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            IPEndPoint ipe = new IPEndPoint(ip,6050);
            KeHuSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            KeHuSocket.Connect(ipe);
            if (KeHuSocket.Connected)
            {
                MessageBox.Show("连接成功");
                ThreadStart ts = new ThreadStart(jieshou);
                Thread thread1 = new Thread(ts);
                thread1.Start();

            }
        }
        private void button1_Click(object sender, EventArgs e)
        {

        }
        private void jieshou()
        {
            while (true)
            {

                byte1 = new Byte[5000000];
                KeHuSocket.Receive(byte1);
                Byte[] photo = byte1;
                if (photo!=null)
                {
                    System.IO.MemoryStream mem = new MemoryStream(photo);
                    mem.Position=100;
                    Image img = Image.FromStream(mem);
                    mem.Close();
                    pictureBox1.Image = img;
                    Thread.Sleep(50);
                }


            }
        }
    }
}
当客户端图片接收多少后, Image img = Image.FromStream(mem); 提示mem参数无效



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-10-11 17:49:05 | 显示全部楼层
KeHuSocket.Receive(byte1); 这个是一下子接受完吗?
如果不是,就等接受完了,再用下面的方法转图片,方法看着都没有问题就是这样点上
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-29 11:06

© 2014-2021

快速回复 返回顶部 返回列表