C# Drag & Drop - 드래그 & 드롭 2
C# 2022. 3. 31. 14:25 |반응형
    
    
    
  이미지 파일을 Drag & Drop으로 열어보자.

| 
 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 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
 | 
 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 WindowsFormsApp1 
{ 
    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; 
            pictureBox1.AllowDrop = true; 
            // 인텔리센스에는 pictureBox1.AllowDrop가 표시되지 않는다. 
            // 하지만 사용은 가능하며 인텔리센스를 사용하고 싶다면 아래처럼 
            // Control로 형변환해서 해도 된다. 
            // ((Control)pictureBox1).AllowDrop = true; 
        } 
        private void pictureBox1_DragOver(object sender, DragEventArgs e) 
        { 
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
            { 
                e.Effect = DragDropEffects.Copy; 
            } 
        } 
        private void pictureBox1_DragDrop(object sender, DragEventArgs e) 
        { 
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
            { 
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); 
                // 드롭된 파일들의 전체 경로명을 반환. 
                foreach (string file in files) 
                { 
                    if (file.EndsWith("jpg") || file.EndsWith("JPG")) 
                    { 
                        // JPG 파일만 디스플레이 
                        pictureBox1.Image = System.Drawing.Image.FromFile(file); 
                    } 
                } 
            } 
        } 
    } 
} 
 | 
소스를 입력하고 빌드한다.


반응형
    
    
    
  'C#' 카테고리의 다른 글
| C# Joystick(Gamepad) Input Check - 조이스틱(게임패드) 입력 확인 1 (0) | 2022.04.05 | 
|---|---|
| C# Console Key Input Check - 콘솔 키 입력 확인 (0) | 2022.04.05 | 
| C# Drag & Drop - 드래그 & 드롭 1 (0) | 2022.03.31 | 
| C# DataTable, DataSet, DataGridView, and XML 2 (0) | 2022.02.20 | 
| C# DataTable, DataSet, DataGridView, and XML 1 (0) | 2022.02.20 |