출처 : http://ryunad.tistory.com/tag/C%23%20%EC%9D%B4%EB%AF%B8%EC%A7%80%20%EC%8D%B8%EB%84%A4%EC%9D%BC
간단하게 이미지를 하나 선택하면 선택한 이미지의 Thumbnail을 만드는 로직입니다.
윈 폼으로 만들었지만 로직부분만 재활용하면 ASP.NET이든 WPF든 어디서든 사용이 가능합니다.
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;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace Ex11
{
public partial class MainForm : Form
{
private Person _person = new Person();
public MainForm()
{
InitializeComponent();
}
private void btnLoadPicture_Click(object sender, EventArgs e)
{
//1. 파일 불러오기
OpenFileDialog dlg = new OpenFileDialog();
if (DialogResult.OK == dlg.ShowDialog())
{
string path = Application.StartupPath;
string fileName =
dlg.FileName.Substring(
dlg.FileName.LastIndexOf("\\") + 1);
string filePath =
Path.Combine(path, fileName);
int dotPosition = fileName.LastIndexOf(".");
string thumbnailName =
fileName.Substring(0, dotPosition) +
"_small" +
fileName.Substring(dotPosition);
string thumbnailPath =
Path.Combine(path, thumbnailName);
if (File.Exists(filePath))
File.Delete(filePath);
//2. 원본이미지 복사 및 thumbnail 이미지 생성
File.Copy(dlg.FileName, filePath);
Image image =
Image.FromFile(filePath);
Image thumbnail =
image.GetThumbnailImage(150, 150, null, IntPtr.Zero);
//picture 이미지 해제
if (pbPicture.Image != null)
{
pbPicture.Image.Dispose();
}
if (File.Exists(thumbnailPath))
File.Delete(thumbnailPath);
thumbnail.Save(thumbnailPath);
//이미지 객체의 해제
image.Dispose();
thumbnail.Dispose();
//3. 이미지 표시
pbPicture.Image =
Image.FromFile(thumbnailPath);
_person.ImagePath = fileName;
}
}
private void miSave_Click(object sender, EventArgs e)
{
_person.Name = txtName.Text;
_person.ResidentNumbers = txtResidentNumber.Text;
_person.BirthDate = DateTime.Parse(txtBirthDate.Text);
_person.Height = int.Parse(txtHeight.Text);
_person.Weight = int.Parse(txtWeight.Text);
_person.Phone = txtPhone.Text;
_person.Email = txtEmail.Text;
string path = Path.Combine(Application.StartupPath, "person.dat"); ;
FileStream stream = new FileStream(path, FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, _person);
stream.Close();
MessageBox.Show("저장되었습니다.");
}
private void miOpen_Click(object sender, EventArgs e)
{
}
private void MainForm_Load(object sender, EventArgs e)
{
string path = Path.Combine(Application.StartupPath,"person.dat");
if (File.Exists(path))
{
FileStream stream = new FileStream(path, FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
_person = (Person)formatter.Deserialize(stream);
txtName.Text = _person.Name;
txtResidentNumber.Text = _person.ResidentNumbers;
txtBirthDate.Text = _person.BirthDate.ToString("yyyy-MM-dd");
txtHeight.Text = _person.Height.ToString();
txtWeight.Text = _person.Weight.ToString();
txtPhone.Text = _person.Phone;
txtEmail.Text = _person.Email;
string fileName = _person.ImagePath;
fileName = fileName.Insert(fileName.LastIndexOf("."), "_small");
string imagePath = Path.Combine(Application.StartupPath, fileName);
if (File.Exists(imagePath))
{
pbPicture.Image = Image.FromFile(imagePath);
}
}
}
}
}
[출처] C# 이미지 섬네일|작성자 괴무리
'PP > ASP.NET' 카테고리의 다른 글
[asp.net] ashx 이미지 섬네일 (0) | 2012.01.06 |
---|---|
[asp.net] 이미지 섬네일 (0) | 2012.01.06 |
[.ASHX 파일]jQuery 제너릭 처리기(ashx)로 DB/데이터배이스 받기/사용 핸들러/handler (0) | 2012.01.06 |
GENERIC (0) | 2012.01.04 |
Generic (0) | 2012.01.04 |