[C# 문법] PictureBox 컨트롤 이용하여 이미지(Image) 넣는 방법


안녕하세요.

 

오늘은 C#에서 PictureBox 컨트롤에 이미지를 넣는 방법에 대해서 알려드리려고 합니다.

 

매우 간단한 작업이기 때문에, 별 다른 설명 없이 예제 코드를 통해서 어떻게 PictureBox 컨트롤에 이미지를 넣는지 보여드리도록 하겠습니다.

 

먼저, 윈폼 빈 프로젝트를 생성해 주시고 아래와 같이 PictureBox 컨트롤, Button 컨트롤을 배치시켜 주시기 바랍니다.


 

윈폼 프로젝트 생성 및 컨트롤 배치




이제 위의 Show Image 버튼 컨트롤을 클릭하였을 때, 이미지가 표시 되게 끔 소스코드를 작성해보고 프로그램도 실행해 보도록 하겠습니다.


예제 코드


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

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 PictureBoxTest

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

 

            uiBtn_ShowImage.Click += uiBtn_ShowImage_Click;

        }

 

        /// <summary>

        /// Show Image 버튼 클릭 이벤트 핸들러

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        public void uiBtn_ShowImage_Click(object sender, EventArgs e)

        {

            //PictureBox 컨트롤에 넣을 이미지 경로\이미지이름 추가

            Image img = Image.FromFile(@"C:\Users\winforsys\Desktop\test.png");

 

            //PictureBox 컨트롤에 이미지 넣기

            uiPb_Image.BackColor = Color.DimGray;

            uiPb_Image.SizeMode = PictureBoxSizeMode.Zoom;

            uiPb_Image.Image = img.GetThumbnailImage(150150nullIntPtr.Zero);

        }

    }

}

 

Colored by Color Scripter

cs


실행 결과



 

위와 같이 이미지를 알맞게 가져오는 것을 확인하실 수 있습니다.

 

이로써, 간단히 PictureBox 컨트롤에 이미지를 넣는 방법에 대해서 알아보았습니다.

 

감사합니다.


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY