티스토리 뷰

프로그래밍/C#

C# 드래그 앤 드롭 픽쳐 이미지

뽀로로친구에디 2019. 2. 28. 17:09



using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;


namespace 드래그드롭픽쳐

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }


        private void pictureBox1_DragDrop(object sender, DragEventArgs e)

        {

            var data = e.Data.GetData(DataFormats.FileDrop);


            if(data != null)

            {

                var fileNames = data as string[];

                if (fileNames.Length > 0)

                    pictureBox1.Image = Image.FromFile(fileNames[0]);

            }

        }


        private void Form1_Load(object sender, EventArgs e)

        {

            pictureBox1.AllowDrop = true;

            pictureBox2.AllowDrop = true;


            }


        private void pictureBox1_DragEnter(object sender, DragEventArgs e)

        {

            e.Effect = DragDropEffects.Copy;

        }


        private void pictureBox2_DragEnter(object sender, DragEventArgs e)

        {

            if (e.Data.GetDataPresent(DataFormats.Bitmap) && (e.AllowedEffect & DragDropEffects.Copy) != 0)

                e.Effect = DragDropEffects.Copy;

            else

                e.Effect = DragDropEffects.None;

        }


        private void pictureBox2_DragDrop(object sender, DragEventArgs e)

        {

            pictureBox2.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap, true);

        }


        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)

        {

            if(e.Button == MouseButtons.Left)

            {

                pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);

            }

        }

    }

}



댓글
최근에 달린 댓글
글 보관함
«   2024/05   »
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
Total
Today
Yesterday
    뽀로로친구에디
    최근에 올라온 글