728x90
반응형
728x170
▶ MainForm.cs
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
namespace TestProject
{
/// <summary>
/// 메인 폼
/// </summary>
public partial class MainForm : Form
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 원본 비트맵
/// </summary>
private Bitmap originalBitmap = null;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
this.openMenuItem.Click += openMenuItem_Click;
this.saveMenuItem.Click += saveMenuItem_Click;
this.scaleTextBox.TextChanged += scaleTextBox_TextChanged;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region Open 메뉴 항목 클릭시 처리하기 - openMenuItem_Click(sender, e)
/// <summary>
/// Open 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void openMenuItem_Click(object sender, EventArgs e)
{
if(this.openFileDialog.ShowDialog() == DialogResult.OK)
{
this.originalBitmap = LoadBitmapUnlocked(this.openFileDialog.FileName);
ShowImage();
}
}
#endregion
#region Save 메뉴 항목 클릭시 처리하기 - saveMenuItem_Click(sender, e)
/// <summary>
/// Save 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void saveMenuItem_Click(object sender, EventArgs e)
{
if(this.saveFileDialog.ShowDialog() == DialogResult.OK)
{
if(!this.saveFileDialog.FileName.ToLower().EndsWith(".png"))
{
DialogResult result = MessageBox.Show
(
"Transparency will be lost unless you save with the PNG format. Do you want to save Anyway?",
"Save Anyway?",
MessageBoxButtons.YesNo
);
if(result == DialogResult.No)
{
return;
}
}
SaveImage(this.pictureBox.Image, this.saveFileDialog.FileName);
}
}
#endregion
#region Scale 텍스트 박스 텍스트 변경시 처리하기 - scaleTextBox_TextChanged(sender, e)
/// <summary>
/// Scale 텍스트 박스 텍스트 변경시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void scaleTextBox_TextChanged(object sender, EventArgs e)
{
ShowImage();
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 잠금없이 비트맵 로드하기 - LoadBitmapUnlocked(filePame)
/// <summary>
/// 잠금없이 비트맵 로드하기
/// </summary>
/// <param name="filePame">파일 경로</param>
/// <returns>비트맵</returns>
private Bitmap LoadBitmapUnlocked(string filePame)
{
using(Bitmap bitmap = new Bitmap(filePame))
{
return new Bitmap(bitmap);
}
}
#endregion
#region 이미지 표시하기 - ShowImage()
/// <summary>
/// 이미지 표시하기
/// </summary>
private void ShowImage()
{
this.sizeLabel.Text = "Size : ";
this.pictureBox.Visible = false;
if(this.originalBitmap == null)
{
return;
}
float scale;
if(!float.TryParse(this.scaleTextBox.Text, out scale))
{
return;
}
if(scale < 0.0001)
{
return;
}
int width = (int)(this.originalBitmap.Width * scale);
int height = (int)(this.originalBitmap.Height * scale);
this.sizeLabel.Text = string.Format("Size : {0} x {1}", width, height);
Bitmap bitmap = new Bitmap(width, height);
using(Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle targetRectangle = new Rectangle(0, 0, width, height);
Rectangle sourceRectangle = new Rectangle(0, 0, this.originalBitmap.Width, this.originalBitmap.Height);
graphics.DrawImage(this.originalBitmap, targetRectangle, sourceRectangle, GraphicsUnit.Pixel);
}
this.pictureBox.Image = bitmap;
this.pictureBox.Visible = true;
}
#endregion
#region 이미지 저장하기 - SaveImage(image, filePath)
/// <summary>
/// 이미지 저장하기
/// </summary>
/// <param name="image">이미지</param>
/// <param name="filePath">파일 경로</param>
public void SaveImage(Image image, string filePath)
{
string fileExtension = Path.GetExtension(filePath);
switch(fileExtension.ToLower())
{
case ".bmp" : image.Save(filePath, ImageFormat.Bmp ); break;
case ".exif" : image.Save(filePath, ImageFormat.Exif); break;
case ".gif" : image.Save(filePath, ImageFormat.Gif ); break;
case ".jpg" :
case ".jpeg" : image.Save(filePath, ImageFormat.Jpeg); break;
case ".png" : image.Save(filePath, ImageFormat.Png ); break;
case ".tif" :
case ".tiff" : image.Save(filePath, ImageFormat.Tiff); break;
default : throw new NotSupportedException("Unknown file extension " + fileExtension);
}
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] 위쪽 및 아래쪽 절반의 색상으로 반전된 텍스트 그리기 (0) | 2020.07.20 |
---|---|
[C#/WINFORM] 개선된 투명 배경 이미지 구하기 (0) | 2020.07.20 |
[C#/WINFORM] 둥근 모서리 이미지 구하기 (0) | 2020.07.20 |
[C#/WINFORM] 워터마크 추가하기 (0) | 2020.07.19 |
[C#/WINFORM] 직선 방향의 가운데 정렬 텍스트 그리기 (0) | 2020.07.19 |
[C#/WINFORM] ColorMatrix 클래스 : 이미지 불투명도 조정하기 (0) | 2020.07.13 |
[C#/WINFORM] 호(arc) 그리기/이동하기/수정하기 (0) | 2020.07.12 |
[C#/WINFORM] 호(arc) 위의 마우스 위치 여부 구하기 (0) | 2020.07.12 |
[C#/WINFORM] 안티-알리아싱으로 그리는 경우 투명도 사용하기 (0) | 2020.07.11 |
[C#/WINFORM] 이미지 나선 그리기 (0) | 2020.07.10 |
댓글을 달아 주세요