111. PictureBox와 OpenFileDialog를 이용하여 간단한 이미지뷰어(ImageViewer) 만들기
.NET프로그래밍/WinForm 2009. 8. 28. 15:37 |
* '도구상자'의 '모든 Windows Forms'에서 'HScrollBar'와 'VScrollBar'를 폼에 드래그 & 드롭하여 스크롤 기능 추가함. 폼에 하나 추가한 'OpenFileDialog'의 "Filter"속성을 " 비트맵|*.tmp|JGP|*.jpg|JPEG|*.jpeg|PNG|*.png"로 하여 파일을 열 때 원하는 이미지 파일확장자명 만 나오게 된다.
==> Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace MyWinForms
{
static class Program
{
/// <summary>
/// 해당 응용 프로그램의 주 진입점입니다.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Sample.FrmImageViewer());
}
}
}
==============================================================================================
==> MainForm.cs
// 이미지뷰어 관련(PictureBox와 OpenFileDialog 사용)
private void miImageViewer_Click(object sender, EventArgs e)
{
(new FrmImageViewer()).Show();
}
==============================================================================================
==> FrmImageViewer.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyWinForms.Sample
{
public partial class FrmImageViewer : Form
{
public FrmImageViewer()
{
InitializeComponent();
}
private void btnOpen_Click(object sender, EventArgs e)
{
if (ofdImage.ShowDialog() != DialogResult.Cancel)
{
this.picImage.ImageLocation = ofdImage.FileName;
}
}
private void FrmImageViewer_Load(object sender, EventArgs e)
{
// 픽쳐박스의 기본 크기는 50*50
p.X = hScrollBar1.Value;
p.Y = vScrollBar1.Value;
this.picImage.Size = new Size(p);
}
Point p; // 포인터형 변수 : X, Y값을 보관
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
p.X = hScrollBar1.Value * 2;
this.picImage.Size = new Size(p);
}
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
p.Y = vScrollBar1.Value * 2;
this.picImage.Size = new Size(p);
}
}
}
< 실행결과 >
--> '도구상자'의 '모든 Windows Forms'에서 'HScrollBar'와 'VScrollBar'를 폼에 드래그 & 드롭하여 스크롤 기능 추가함.
--> '사진 열기'버튼을 누른 후 열린 "열기"창(OpenFileDialog)에서 컴퓨터에 저장해놨던 JPG파일을 선택함.
--> 선택한 JPG파일을 열었을 때 화면. 'HScrollBar'와 'VScrollBar'를 움직여 이미지 크기 조작가능.
'.NET프로그래밍 > WinForm' 카테고리의 다른 글
113. 트리뷰(TreeView) (0) | 2009.08.28 |
---|---|
112. 리스트뷰(ListView) (0) | 2009.08.28 |
110. 체크리스트박스(CheckedListBox) (0) | 2009.08.28 |
109. 창 ( 창 닫기 / 계단식 정렬 / 수평 바둑판 정렬 / 수직 바둑판 정렬 ) (0) | 2009.08.27 |
108. 초간단 웹브라우저 만들기 (0) | 2009.08.27 |