달력

52024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
반응형
서버 쪽
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 이미지 읽어오기(하드)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dia = new OpenFileDialog();
            dia.Multiselect = false;
            dia.Filter = "jpg files|*.jpg";

            if (dia.ShowDialog() == DialogResult.OK)
            {
                this.pictureBox1.ImageLocation = dia.FileName;
            }
        }

        /// <summary>
        /// 이미지를 바이트로
        /// https://www.codeproject.com/Articles/15460/C-Image-to-Byte-Array-and-Byte-Array-to-Image-Conv
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public byte[] ImageToByteArray(System.Drawing.Image image)
        {
            MemoryStream ms = new MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }

        /// <summary>
        /// 서버 대기
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            this.button3.Enabled = false;

            Socket sListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 8081);

            sListener.Bind(ipEndPoint);
            sListener.Listen(20);

            Console.WriteLine("클라이언트 연결을 대기합니다.");

            Socket sClient = sListener.Accept();
            IPEndPoint ip = (IPEndPoint)sClient.RemoteEndPoint;
            Console.WriteLine("주소 {0} 에서 접속", ip.Address);

            Byte[] _data = ImageToByteArray(this.pictureBox1.Image);
            sClient.Send(BitConverter.GetBytes(_data.Length));
            sClient.Send(_data);

            sListener.Close();
        }
    }
}

 

 

클라이언트 쪽

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 소켓으로 읽어오기
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            Socket sClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8081);
            sClient.Connect(ipEndPoint);
            Byte[] _data = new byte[256];
            sClient.Receive(_data);
            int iLength = BitConverter.ToInt32(_data, 0);

            Byte[] _data2 = new byte[iLength];
            sClient.Receive(_data2);

            this.pictureBox1.Image = byteArrayToImage(_data2);

            sClient.Close();
        }

        /// <summary>
        /// 바이트를 image로
        /// https://www.codeproject.com/Articles/15460/C-Image-to-Byte-Array-and-Byte-Array-to-Image-Conv
        /// </summary>
        /// <param name="byteArrayIn"></param>
        /// <returns></returns>
        public Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }
    }
}

  

 

서버 가동

   이미지 읽어오기

   소켓서버 대기

 

클라이언트

   소켓연결 후 이미지 가져오기

 

순으로 동작시켜보면 됨

 

실행화면

 

'프로그래밍 > C#' 카테고리의 다른 글

[C#]PictureBox안에 있는 이미지 드래그로 이동시키기  (7) 2017.04.28
[C#] 화면스크린 갭쳐  (0) 2017.04.25
[C#]as is  (0) 2017.04.22
[C#]마지막 일자 구하기  (0) 2017.04.20
[C#]엑셀 컬럼 숫자 문자로 변환  (0) 2017.04.18
Posted by 유령회사
|