Ver3.0 ๐จ C# ์ด๋ฏธ์ง ์ฒ๋ฆฌ์ System.Drawing์ ํ์ฉํ ๊ทธ๋ํฝ ํ๋ก๊ทธ๋๋ฐ ์๋ฒฝ ๊ฐ์ด๋

๐จ C# ์ด๋ฏธ์ง ์ฒ๋ฆฌ์ System.Drawing์ ํ์ฉํ ๊ทธ๋ํฝ ํ๋ก๊ทธ๋๋ฐ ์๋ฒฝ ๊ฐ์ด๋
์น๊ตฌ์ฒ๋ผ ์ฝ๊ฒ ๋ฐฐ์ฐ๋ C# ๊ทธ๋ํฝ ํ๋ก๊ทธ๋๋ฐ์ ๋ชจ๋ ๊ฒ
๐ ์๋
! ์ค๋์ C#์ผ๋ก ์ด๋ฏธ์ง๋ฅผ ๋ค๋ฃจ๋ ๋ฐฉ๋ฒ์ ๋ํด ์ด์ผ๊ธฐํด๋ณผ๊น ํด. ํน์ ํ๋ก๊ทธ๋จ์์ ์ด๋ฏธ์ง๋ฅผ ๋ถ๋ฌ์ค๊ณ , ํฌ๊ธฐ๋ฅผ ์กฐ์ ํ๊ณ , ํํฐ๋ฅผ ์ ์ฉํ๊ณ , ์ฌ์ง์ด ๊ทธ๋ฆผ์ ๊ทธ๋ฆฌ๋ ๊ธฐ๋ฅ์ ๋ง๋ค์ด๋ณด๊ณ ์ถ์๋ ์ ์์ด? ๊ทธ๋ ๋ค๋ฉด ์ ๋๋ก ์ฐพ์์์ด!
System.Drawing ๋ค์์คํ์ด์ค๋ C#์์ ๊ทธ๋ํฝ ์์
์ ํ ์ ์๊ฒ ํด์ฃผ๋ ๊ฐ๋ ฅํ ๋๊ตฌ์ผ. GDI+(Graphics Device Interface Plus)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ๊ณ ์์ด์, Windows ํ๊ฒฝ์์ 2D ๊ทธ๋ํฝ์ ๋ค๋ฃจ๋ ๋ฐ ์ต์ ํ๋์ด ์์ง. ๋ฌผ๋ก ์ต๊ทผ์๋ ํฌ๋ก์ค ํ๋ซํผ ํ๊ฒฝ์ ์ํด ๋ค๋ฅธ ๋์๋ค๋ ๋์ค๊ณ ์์ง๋ง, System.Drawing์ ์ฌ์ ํ ๋ง์ ๊ฐ๋ฐ์๋ค์ด ์ฌ์ฉํ๋ ๊ฒ์ฆ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ผ.
๐ฏ ์ด ๊ธ์์๋ ๊ธฐ์ด๋ถํฐ ์ค์ ํ์ฉ๊น์ง, ์น๊ตฌ์๊ฒ ์ค๋ช
ํ๋ฏ์ด ์ฝ๊ณ ์ฌ๋ฏธ์๊ฒ ์๋ ค์ค๊ฒ. ์ฝ๋ ์์ ๋ ํ๋ถํ๊ฒ ์ค๋นํ์ผ๋๊น, ๋ฐ๋ผํ๋ค ๋ณด๋ฉด ์ด๋์ ์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ ๋ฌธ๊ฐ๊ฐ ๋์ด ์์ ๊ฑฐ์ผ!
๐ System.Drawing ๋ค์์คํ์ด์ค ์ดํดํ๊ธฐ
๋จผ์ System.Drawing์ด ๋ญ์ง๋ถํฐ ์์๋ณด์. ์ด๊ฑด .NET Framework์ ํฌํจ๋ ๋ค์์คํ์ด์ค๋ก, ๊ทธ๋ํฝ ๊ด๋ จ ์์
์ ์ฒ๋ฆฌํ ์ ์๋ ํด๋์ค๋ค์ ์งํฉ์ด์ผ.
๐ ์ฃผ์ ๊ตฌ์ฑ ์์๋ฅผ ์ดํด๋ณด๋ฉด:
System.Drawing ์ฃผ์ ํด๋์ค๋ค:
Graphics - ์ค์ ๋ก ๊ทธ๋ฆผ์ ๊ทธ๋ฆฌ๋ ์บ๋ฒ์ค ์ญํ ์ ํ๋ ํต์ฌ ํด๋์ค์ผ. ์ , ๋ํ, ํ
์คํธ, ์ด๋ฏธ์ง ๋ฑ์ ๊ทธ๋ฆด ์ ์์ด.
Bitmap - ํฝ์
๊ธฐ๋ฐ์ ์ด๋ฏธ์ง๋ฅผ ๋ค๋ฃจ๋ ํด๋์ค. ์ด๋ฏธ์ง ํ์ผ์ ๋ถ๋ฌ์ค๊ฑฐ๋ ์๋ก์ด ์ด๋ฏธ์ง๋ฅผ ๋ง๋ค ๋ ์ฌ์ฉํด.
Image - Bitmap์ ๋ถ๋ชจ ํด๋์ค๋ก, ๋ ์ถ์์ ์ธ ์ด๋ฏธ์ง ๊ฐ๋
์ ๋ค๋ค.
Pen - ์ ์ ๊ทธ๋ฆด ๋ ์ฌ์ฉํ๋ ๋๊ตฌ. ์์, ๋๊ป, ์คํ์ผ์ ์ง์ ํ ์ ์์ด.
Brush - ๋ํ์ ์ฑ์ธ ๋ ์ฌ์ฉํ๋ ๋๊ตฌ. ๋จ์, ๊ทธ๋ผ๋์ธํธ, ํจํด ๋ฑ ๋ค์ํ ์คํ์ผ์ด ์์ด.
Color - ์์์ ํํํ๋ ๊ตฌ์กฐ์ฒด. RGB, ARGB ๊ฐ์ผ๋ก ์์ ์ ์ํด.
Font - ํ
์คํธ๋ฅผ ๊ทธ๋ฆด ๋ ์ฌ์ฉํ๋ ํฐํธ ์ ๋ณด๋ฅผ ๋ด๊ณ ์์ด.
๐ก ํ: System.Drawing์ ์ฌ์ฉํ๋ ค๋ฉด ํ๋ก์ ํธ์ System.Drawing.dll ์ฐธ์กฐ๋ฅผ ์ถ๊ฐํด์ผ ํด. .NET Framework ํ๋ก์ ํธ์์๋ ๊ธฐ๋ณธ์ ์ผ๋ก ํฌํจ๋์ด ์์ง๋ง, .NET Core๋ .NET 5+ ํ๊ฒฝ์์๋ System.Drawing.Common NuGet ํจํค์ง๋ฅผ ์ค์นํด์ผ ํด!
๐ผ๏ธ ์ด๋ฏธ์ง ๋ถ๋ฌ์ค๊ธฐ์ ๊ธฐ๋ณธ ์กฐ์
์, ์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํด๋ณด์! ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ์์
์ธ ์ด๋ฏธ์ง ๋ถ๋ฌ์ค๊ธฐ๋ถํฐ ์์ํ ๊ฒ.
๐ ์ด๋ฏธ์ง๋ฅผ ๋ถ๋ฌ์ค๋ ๋ฐฉ๋ฒ์ ์ฌ๋ฌ ๊ฐ์ง๊ฐ ์์ด. ๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ๋ถํฐ ์ดํด๋ณด์:
using System;
using System.Drawing;
using System.Drawing.Imaging;
// ๋ฐฉ๋ฒ 1: ํ์ผ ๊ฒฝ๋ก๋ก ์ง์ ๋ถ๋ฌ์ค๊ธฐ
Bitmap image = new Bitmap(@"C:\Images\sample.jpg");
// ๋ฐฉ๋ฒ 2: Image.FromFile ์ฌ์ฉ (๋ ์์ ํ ๋ฐฉ๋ฒ)
Image img = Image.FromFile(@"C:\Images\sample.jpg");
Bitmap bitmap = new Bitmap(img);
// ๋ฐฉ๋ฒ 3: ์คํธ๋ฆผ์ ํตํด ๋ถ๋ฌ์ค๊ธฐ
using (FileStream fs = new FileStream(@"C:\Images\sample.jpg", FileMode.Open))
{
Bitmap streamImage = new Bitmap(fs);
// ์์
์ํ
}
// ์ด๋ฏธ์ง ์ ๋ณด ํ์ธ
Console.WriteLine($"๋๋น: {image.Width}px");
Console.WriteLine($"๋์ด: {image.Height}px");
Console.WriteLine($"ํฌ๋งท: {image.RawFormat}");
Console.WriteLine($"ํด์๋: {image.HorizontalResolution} x {image.VerticalResolution} DPI");
// ์ฌ์ฉ ํ ๋ฐ๋์ ๋ฉ๋ชจ๋ฆฌ ํด์ !
image.Dispose();
โ ๏ธ ์ฃผ์์ฌํญ: Bitmap ๊ฐ์ฒด๋ ๊ด๋ฆฌ๋์ง ์๋ ๋ฆฌ์์ค๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์, ์ฌ์ฉ์ด ๋๋๋ฉด ๋ฐ๋์ Dispose() ๋ฉ์๋๋ฅผ ํธ์ถํ๊ฑฐ๋ using ๋ฌธ์ ์ฌ์ฉํด์ผ ํด. ๊ทธ๋ ์ง ์์ผ๋ฉด ๋ฉ๋ชจ๋ฆฌ ๋์๊ฐ ๋ฐ์ํ ์ ์์ด!
๐ ์ด๋ฏธ์ง ํฌ๊ธฐ ์กฐ์ ํ๊ธฐ
์ด๋ฏธ์ง ํฌ๊ธฐ๋ฅผ ์กฐ์ ํ๋ ๊ฑด ์ ๋ง ์์ฃผ ์ฌ์ฉํ๋ ๊ธฐ๋ฅ์ด์ผ. ์ธ๋ค์ผ์ ๋ง๋ค๊ฑฐ๋, ์น์ ์ต์ ํ๋ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ ๋ ํ์์ ์ด์ง:
public Bitmap ResizeImage(Image image, int width, int height)
{
// ์๋ก์ด ํฌ๊ธฐ์ Bitmap ์์ฑ
Bitmap resizedImage = new Bitmap(width, height);
// Graphics ๊ฐ์ฒด ์์ฑ
using (Graphics graphics = Graphics.FromImage(resizedImage))
{
// ๊ณ ํ์ง ๋ ๋๋ง ์ค์
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
// ์ด๋ฏธ์ง ๊ทธ๋ฆฌ๊ธฐ
graphics.DrawImage(image, 0, 0, width, height);
}
return resizedImage;
}
// ์ฌ์ฉ ์์
using (Image original = Image.FromFile(@"C:\Images\large.jpg"))
{
Bitmap thumbnail = ResizeImage(original, 200, 150);
thumbnail.Save(@"C:\Images\thumbnail.jpg", ImageFormat.Jpeg);
thumbnail.Dispose();
}
๐ก ํ๋ก ํ: InterpolationMode๋ฅผ ์ค์ ํ๋ฉด ์ด๋ฏธ์ง ํ์ง์ ํฌ๊ฒ ํฅ์์ํฌ ์ ์์ด. HighQualityBicubic์ ๊ฐ์ฅ ์ข์ ํ์ง์ ์ ๊ณตํ์ง๋ง ์ฒ๋ฆฌ ์๋๊ฐ ๋๋ ค. ์๋๊ฐ ์ค์ํ๋ค๋ฉด Bilinear๋ Low๋ฅผ ์ฌ์ฉํด๋ด!
๐ ๋น์จ ์ ์งํ๋ฉฐ ํฌ๊ธฐ ์กฐ์ ํ๊ธฐ
์๋ณธ ์ด๋ฏธ์ง์ ๊ฐ๋ก์ธ๋ก ๋น์จ์ ์ ์งํ๋ฉด์ ํฌ๊ธฐ๋ฅผ ์กฐ์ ํ๋ ๊ฒ๋ ์ค์ํด. ์ด๋ฏธ์ง๊ฐ ์ฐ๊ทธ๋ฌ์ง๋ ๊ฑธ ๋ฐฉ์งํ ์ ์๊ฑฐ๋ :
public Bitmap ResizeImageKeepAspectRatio(Image image, int maxWidth, int maxHeight)
{
// ์๋ณธ ๋น์จ ๊ณ์ฐ
double ratioX = (double)maxWidth / image.Width;
double ratioY = (double)maxHeight / image.Height;
double ratio = Math.Min(ratioX, ratioY);
// ์๋ก์ด ํฌ๊ธฐ ๊ณ์ฐ
int newWidth = (int)(image.Width * ratio);
int newHeight = (int)(image.Height * ratio);
// ํฌ๊ธฐ ์กฐ์
Bitmap resizedImage = new Bitmap(newWidth, newHeight);
using (Graphics graphics = Graphics.FromImage(resizedImage))
{
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
}
return resizedImage;
}
// ์ฌ์ฉ ์์
using (Image original = Image.FromFile(@"C:\Images\photo.jpg"))
{
// ์ต๋ 800x600 ํฌ๊ธฐ๋ก ์กฐ์ (๋น์จ ์ ์ง)
Bitmap resized = ResizeImageKeepAspectRatio(original, 800, 600);
resized.Save(@"C:\Images\resized.jpg", ImageFormat.Jpeg);
resized.Dispose();
}
๐จ Graphics ํด๋์ค๋ก ๊ทธ๋ฆผ ๊ทธ๋ฆฌ๊ธฐ
์ด์ ์ง์ง ์ฌ๋ฏธ์๋ ๋ถ๋ถ์ด์ผ! Graphics ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ฉด ํ๋ก๊ทธ๋๋ฐ์ผ๋ก ๊ทธ๋ฆผ์ ๊ทธ๋ฆด ์ ์์ด. ๋ง์น ๋์งํธ ์บ๋ฒ์ค์ ๋ถ์ผ๋ก ๊ทธ๋ฆผ์ ๊ทธ๋ฆฌ๋ ๊ฒ์ฒ๋ผ ๋ง์ด์ผ.
โ๏ธ ์ ๊ณผ ๋ํ ๊ทธ๋ฆฌ๊ธฐ
// ์๋ก์ด Bitmap ์์ฑ (800x600 ํฌ๊ธฐ)
Bitmap canvas = new Bitmap(800, 600);
using (Graphics g = Graphics.FromImage(canvas))
{
// ๋ฐฐ๊ฒฝ์ ํฐ์์ผ๋ก ์ฑ์ฐ๊ธฐ
g.Clear(Color.White);
// ์ํฐ์จ๋ฆฌ์ด์ฑ ํ์ฑํ (๋ถ๋๋ฌ์ด ์ )
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// ๋นจ๊ฐ์ ํ ์์ฑ (๋๊ป 3px)
using (Pen redPen = new Pen(Color.Red, 3))
{
// ์ ๊ทธ๋ฆฌ๊ธฐ
g.DrawLine(redPen, 50, 50, 200, 200);
// ์ฌ๊ฐํ ๊ทธ๋ฆฌ๊ธฐ (ํ
๋๋ฆฌ๋ง)
g.DrawRectangle(redPen, 250, 50, 150, 100);
// ์ ๊ทธ๋ฆฌ๊ธฐ
g.DrawEllipse(redPen, 450, 50, 100, 100);
}
// ํ๋์ ๋ธ๋ฌ์๋ก ์ฑ์์ง ๋ํ ๊ทธ๋ฆฌ๊ธฐ
using (SolidBrush blueBrush = new SolidBrush(Color.Blue))
{
// ์ฑ์์ง ์ฌ๊ฐํ
g.FillRectangle(blueBrush, 50, 250, 150, 100);
// ์ฑ์์ง ์
g.FillEllipse(blueBrush, 250, 250, 100, 100);
}
// ๊ทธ๋ผ๋์ธํธ ๋ธ๋ฌ์ ์ฌ์ฉ
using (System.Drawing.Drawing2D.LinearGradientBrush gradBrush =
new System.Drawing.Drawing2D.LinearGradientBrush(
new Rectangle(400, 250, 200, 150),
Color.Yellow,
Color.Orange,
45f))
{
g.FillRectangle(gradBrush, 400, 250, 200, 150);
}
}
// ์ ์ฅ
canvas.Save(@"C:\Images\drawing.png", ImageFormat.Png);
canvas.Dispose();
์ฃผ์ ๊ทธ๋ฆฌ๊ธฐ ๋ฉ์๋๋ค:
DrawLine() - ์ ๊ทธ๋ฆฌ๊ธฐ
DrawRectangle() - ์ฌ๊ฐํ ํ
๋๋ฆฌ ๊ทธ๋ฆฌ๊ธฐ
FillRectangle() - ์ฑ์์ง ์ฌ๊ฐํ ๊ทธ๋ฆฌ๊ธฐ
DrawEllipse() - ์/ํ์ ํ
๋๋ฆฌ ๊ทธ๋ฆฌ๊ธฐ
FillEllipse() - ์ฑ์์ง ์/ํ์ ๊ทธ๋ฆฌ๊ธฐ
DrawPolygon() - ๋ค๊ฐํ ํ
๋๋ฆฌ ๊ทธ๋ฆฌ๊ธฐ
FillPolygon() - ์ฑ์์ง ๋ค๊ฐํ ๊ทธ๋ฆฌ๊ธฐ
DrawArc() - ํธ ๊ทธ๋ฆฌ๊ธฐ
DrawBezier() - ๋ฒ ์ง์ด ๊ณก์ ๊ทธ๋ฆฌ๊ธฐ
DrawCurve() - ๋ถ๋๋ฌ์ด ๊ณก์ ๊ทธ๋ฆฌ๊ธฐ
๐ ํ
์คํธ ๊ทธ๋ฆฌ๊ธฐ
์ด๋ฏธ์ง์ ํ
์คํธ๋ฅผ ์ถ๊ฐํ๋ ๊ฒ๋ ์ ๋ง ์ ์ฉํด. ์ํฐ๋งํฌ๋ฅผ ๋ฃ๊ฑฐ๋, ์ค๋ช
์ ์ถ๊ฐํ๊ฑฐ๋, ์ฐจํธ๋ฅผ ๋ง๋ค ๋ ํ์์ ์ด์ง:
using (Graphics g = Graphics.FromImage(canvas))
{
// ํ
์คํธ ๋ ๋๋ง ํ์ง ์ค์
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// ํฐํธ ์์ฑ
using (Font titleFont = new Font("๋ง์ ๊ณ ๋", 32, FontStyle.Bold))
using (Font normalFont = new Font("๋ง์ ๊ณ ๋", 16, FontStyle.Regular))
using (SolidBrush blackBrush = new SolidBrush(Color.Black))
using (SolidBrush whiteBrush = new SolidBrush(Color.White))
{
// ์ ๋ชฉ ๊ทธ๋ฆฌ๊ธฐ
g.DrawString("C# ๊ทธ๋ํฝ ํ๋ก๊ทธ๋๋ฐ", titleFont, blackBrush, 50, 50);
// ์ผ๋ฐ ํ
์คํธ
g.DrawString("System.Drawing์ ํ์ฉํ ์ด๋ฏธ์ง ์ฒ๋ฆฌ",
normalFont, blackBrush, 50, 100);
// ํ
์คํธ ํฌ๊ธฐ ์ธก์
SizeF textSize = g.MeasureString("์ธก์ ํ ํ
์คํธ", normalFont);
Console.WriteLine($"ํ
์คํธ ํฌ๊ธฐ: {textSize.Width} x {textSize.Height}");
// ์ค์ ์ ๋ ฌ ํ
์คํธ
string centerText = "์ค์ ์ ๋ ฌ";
SizeF size = g.MeasureString(centerText, normalFont);
float x = (canvas.Width - size.Width) / 2;
float y = (canvas.Height - size.Height) / 2;
g.DrawString(centerText, normalFont, blackBrush, x, y);
// ํ์ ๋ ํ
์คํธ
g.TranslateTransform(400, 300);
g.RotateTransform(45);
g.DrawString("ํ์ ๋ ํ
์คํธ", normalFont, blackBrush, 0, 0);
g.ResetTransform();
}
}
โจ ์ค์ ํ์ฉ: ์ฌ๋ฅ๋ท(https://www.jaenung.net)์์ ๋์์ธ ์ฌ๋ฅ์ ๊ฑฐ๋ํ๋ ๋ถ๋ค์ด ์๋์ผ๋ก ์ํฐ๋งํฌ๋ฅผ ๋ฃ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋ค ๋ ์ด๋ฐ ํ ์คํธ ๊ทธ๋ฆฌ๊ธฐ ๊ธฐ๋ฅ์ ๋ง์ด ํ์ฉํด. ํฌํธํด๋ฆฌ์ค ์ด๋ฏธ์ง์ ์๋์ผ๋ก ๋ก๊ณ ๋ ์ ์๊ถ ํ์๋ฅผ ๋ฃ์ ์ ์๊ฑฐ๋ !
๐ญ ์ด๋ฏธ์ง ํํฐ์ ํจ๊ณผ ์ ์ฉํ๊ธฐ
์ด์ ์ข ๋ ๊ณ ๊ธ ๊ธฐ๋ฅ์ผ๋ก ๋์ด๊ฐ๋ณผ๊น? ์ด๋ฏธ์ง์ ๋ค์ํ ํํฐ์ ํจ๊ณผ๋ฅผ ์ ์ฉํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์.
๐ ์์ ์กฐ์ํ๊ธฐ
ํฝ์
๋จ์๋ก ์์์ ์กฐ์ํ๋ฉด ์ ๋ง ๋ค์ํ ํจ๊ณผ๋ฅผ ๋ง๋ค ์ ์์ด:
// ๊ทธ๋ ์ด์ค์ผ์ผ ๋ณํ
public Bitmap ConvertToGrayscale(Bitmap original)
{
Bitmap grayscale = new Bitmap(original.Width, original.Height);
for (int y = 0; y < original.Height; y++)
{
for (int x = 0; x < original.Width; x++)
{
Color originalColor = original.GetPixel(x, y);
// ๊ทธ๋ ์ด์ค์ผ์ผ ๊ณต์ (์ธ๊ฐ์ ๋์ด ์์ ์ธ์ํ๋ ๋น์จ ๋ฐ์)
int grayValue = (int)(originalColor.R * 0.299 +
originalColor.G * 0.587 +
originalColor.B * 0.114);
Color grayColor = Color.FromArgb(originalColor.A,
grayValue,
grayValue,
grayValue);
grayscale.SetPixel(x, y, grayColor);
}
}
return grayscale;
}
// ๋ฐ๊ธฐ ์กฐ์
public Bitmap AdjustBrightness(Bitmap original, int brightness)
{
Bitmap adjusted = new Bitmap(original.Width, original.Height);
for (int y = 0; y < original.Height; y++)
{
for (int x = 0; x < original.Width; x++)
{
Color originalColor = original.GetPixel(x, y);
int r = Math.Min(255, Math.Max(0, originalColor.R + brightness));
int g = Math.Min(255, Math.Max(0, originalColor.G + brightness));
int b = Math.Min(255, Math.Max(0, originalColor.B + brightness));
adjusted.SetPixel(x, y, Color.FromArgb(originalColor.A, r, g, b));
}
}
return adjusted;
}
// ๋๋น ์กฐ์
public Bitmap AdjustContrast(Bitmap original, double contrast)
{
Bitmap adjusted = new Bitmap(original.Width, original.Height);
double factor = (259.0 * (contrast + 255.0)) / (255.0 * (259.0 - contrast));
for (int y = 0; y < original.Height; y++)
{
for (int x = 0; x < original.Width; x++)
{
Color originalColor = original.GetPixel(x, y);
int r = (int)Math.Min(255, Math.Max(0, factor * (originalColor.R - 128) + 128));
int g = (int)Math.Min(255, Math.Max(0, factor * (originalColor.G - 128) + 128));
int b = (int)Math.Min(255, Math.Max(0, factor * (originalColor.B - 128) + 128));
adjusted.SetPixel(x, y, Color.FromArgb(originalColor.A, r, g, b));
}
}
return adjusted;
}
โ ๏ธ ์ฑ๋ฅ ์ฃผ์: GetPixel()๊ณผ SetPixel()์ ํธ๋ฆฌํ์ง๋ง ๋งค์ฐ ๋๋ ค. ํฐ ์ด๋ฏธ์ง๋ฅผ ์ฒ๋ฆฌํ ๋๋ LockBits()๋ฅผ ์ฌ์ฉํด์ ์ง์ ๋ฉ๋ชจ๋ฆฌ์ ์ ๊ทผํ๋ ๊ฒ ํจ์ฌ ๋นจ๋ผ. ์๋์์ ์์ธํ ์ค๋ช ํ ๊ฒ!
โก ๊ณ ์ ํฝ์
์ฒ๋ฆฌ (LockBits ์ฌ์ฉ)
๋์ฉ๋ ์ด๋ฏธ์ง๋ฅผ ์ฒ๋ฆฌํ ๋๋ ์ฑ๋ฅ์ด ์ ๋ง ์ค์ํด. LockBits๋ฅผ ์ฌ์ฉํ๋ฉด GetPixel/SetPixel๋ณด๋ค ์์ญ ๋ฐฐ ๋น ๋ฅด๊ฒ ์ฒ๋ฆฌํ ์ ์์ด:
public unsafe Bitmap FastGrayscale(Bitmap original)
{
Bitmap grayscale = new Bitmap(original.Width, original.Height);
// ์ด๋ฏธ์ง ๋ฐ์ดํฐ ์ ๊ธ
Rectangle rect = new Rectangle(0, 0, original.Width, original.Height);
BitmapData originalData = original.LockBits(rect, ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
BitmapData grayscaleData = grayscale.LockBits(rect, ImageLockMode.WriteOnly,
PixelFormat.Format24bppRgb);
int bytes = Math.Abs(originalData.Stride) * original.Height;
byte[] rgbValues = new byte[bytes];
byte[] grayValues = new byte[bytes];
// ์๋ณธ ๋ฐ์ดํฐ ๋ณต์ฌ
System.Runtime.InteropServices.Marshal.Copy(originalData.Scan0, rgbValues, 0, bytes);
// ๊ทธ๋ ์ด์ค์ผ์ผ ๋ณํ
for (int i = 0; i < bytes; i += 3)
{
byte gray = (byte)(rgbValues[i] * 0.114 + // Blue
rgbValues[i + 1] * 0.587 + // Green
rgbValues[i + 2] * 0.299); // Red
grayValues[i] = gray;
grayValues[i + 1] = gray;
grayValues[i + 2] = gray;
}
// ๋ณํ๋ ๋ฐ์ดํฐ ๋ณต์ฌ
System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, grayscaleData.Scan0, bytes);
// ์ ๊ธ ํด์
original.UnlockBits(originalData);
grayscale.UnlockBits(grayscaleData);
return grayscale;
}
๐ก ์ฑ๋ฅ ๋น๊ต: 1920x1080 ์ด๋ฏธ์ง๋ฅผ ๊ทธ๋ ์ด์ค์ผ์ผ๋ก ๋ณํํ ๋, GetPixel/SetPixel์ ์ฝ 2~3์ด๊ฐ ๊ฑธ๋ฆฌ์ง๋ง, LockBits๋ฅผ ์ฌ์ฉํ๋ฉด 0.1์ด ์ด๋ด๋ก ์ฒ๋ฆฌํ ์ ์์ด. ์ฝ 20~30๋ฐฐ ๋น ๋ฅธ ๊ฑฐ์ง!
๐จ ColorMatrix๋ฅผ ์ฌ์ฉํ ์์ ๋ณํ
ColorMatrix๋ ๋ ๊ฐํธํ๊ณ ๊ฐ๋ ฅํ ์์ ๋ณํ ๋ฐฉ๋ฒ์ด์ผ. ํ๋ ฌ ์ฐ์ฐ์ ํตํด ๋ค์ํ ํจ๊ณผ๋ฅผ ์ฝ๊ฒ ์ ์ฉํ ์ ์์ด:
public Bitmap ApplyColorMatrix(Bitmap original, ColorMatrix colorMatrix)
{
Bitmap result = new Bitmap(original.Width, original.Height);
using (Graphics g = Graphics.FromImage(result))
{
// ImageAttributes ์์ฑ ๋ฐ ColorMatrix ์ค์
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
// ์ด๋ฏธ์ง ๊ทธ๋ฆฌ๊ธฐ
g.DrawImage(original,
new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height,
GraphicsUnit.Pixel,
attributes);
}
return result;
}
// ์ธํผ์ ํจ๊ณผ
public Bitmap ApplySepiaEffect(Bitmap original)
{
ColorMatrix sepiaMatrix = new ColorMatrix(new float[][]
{
new float[] {0.393f, 0.349f, 0.272f, 0, 0},
new float[] {0.769f, 0.686f, 0.534f, 0, 0},
new float[] {0.189f, 0.168f, 0.131f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
return ApplyColorMatrix(original, sepiaMatrix);
}
// ๋ค๊ฑฐํฐ๋ธ ํจ๊ณผ
public Bitmap ApplyNegativeEffect(Bitmap original)
{
ColorMatrix negativeMatrix = new ColorMatrix(new float[][]
{
new float[] {-1, 0, 0, 0, 0},
new float[] {0, -1, 0, 0, 0},
new float[] {0, 0, -1, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {1, 1, 1, 0, 1}
});
return ApplyColorMatrix(original, negativeMatrix);
}
// ํฌ๋ช
๋ ์กฐ์
public Bitmap AdjustOpacity(Bitmap original, float opacity)
{
ColorMatrix opacityMatrix = new ColorMatrix(new float[][]
{
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, opacity, 0},
new float[] {0, 0, 0, 0, 1}
});
return ApplyColorMatrix(original, opacityMatrix);
}
๐๏ธ ๊ณ ๊ธ ๊ทธ๋ํฝ ๊ธฐ๋ฒ
์ด์ ์ข ๋ ์ ๋ฌธ์ ์ธ ๊ธฐ๋ฒ๋ค์ ๋ฐฐ์๋ณด์. ์ด๋ฐ ๊ธฐ์ ๋ค์ ์ตํ๋ฉด ์ ๋ง ๋ฉ์ง ํ๋ก๊ทธ๋จ์ ๋ง๋ค ์ ์์ด!
๐ ์ด๋ฏธ์ง ํ์ ๊ณผ ๋ณํ
// ์ด๋ฏธ์ง ํ์
public Bitmap RotateImage(Bitmap original, float angle)
{
// ํ์ ํ ์ด๋ฏธ์ง๊ฐ ์๋ฆฌ์ง ์๋๋ก ์๋ก์ด ํฌ๊ธฐ ๊ณ์ฐ
double radians = angle * Math.PI / 180;
double cos = Math.Abs(Math.Cos(radians));
double sin = Math.Abs(Math.Sin(radians));
int newWidth = (int)(original.Width * cos + original.Height * sin);
int newHeight = (int)(original.Width * sin + original.Height * cos);
Bitmap rotated = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(rotated))
{
g.Clear(Color.Transparent);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// ์ค์ฌ์ ์ผ๋ก ์ด๋
g.TranslateTransform(newWidth / 2f, newHeight / 2f);
// ํ์
g.RotateTransform(angle);
// ์๋ณธ ์ด๋ฏธ์ง ์ค์ฌ์ผ๋ก ์ด๋
g.TranslateTransform(-original.Width / 2f, -original.Height / 2f);
// ์ด๋ฏธ์ง ๊ทธ๋ฆฌ๊ธฐ
g.DrawImage(original, new Point(0, 0));
}
return rotated;
}
// ์ด๋ฏธ์ง ๋ค์ง๊ธฐ
public Bitmap FlipImage(Bitmap original, bool horizontal, bool vertical)
{
Bitmap flipped = (Bitmap)original.Clone();
if (horizontal && vertical)
flipped.RotateFlip(RotateFlipType.RotateNoneFlipXY);
else if (horizontal)
flipped.RotateFlip(RotateFlipType.RotateNoneFlipX);
else if (vertical)
flipped.RotateFlip(RotateFlipType.RotateNoneFlipY);
return flipped;
}
// ์ด๋ฏธ์ง ์๋ฅด๊ธฐ
public Bitmap CropImage(Bitmap original, Rectangle cropArea)
{
Bitmap cropped = new Bitmap(cropArea.Width, cropArea.Height);
using (Graphics g = Graphics.FromImage(cropped))
{
g.DrawImage(original,
new Rectangle(0, 0, cropArea.Width, cropArea.Height),
cropArea,
GraphicsUnit.Pixel);
}
return cropped;
}
๐ญ ๋ธ๋ฌ(ํ๋ฆผ) ํจ๊ณผ
๋ธ๋ฌ ํจ๊ณผ๋ ์ด๋ฏธ์ง๋ฅผ ๋ถ๋๋ฝ๊ฒ ๋ง๋ค๊ฑฐ๋ ๋ฐฐ๊ฒฝ์ ํ๋ฆฌ๊ฒ ํ ๋ ์ฌ์ฉํด. ๊ฐ์ฐ์์ ๋ธ๋ฌ๊ฐ ๊ฐ์ฅ ์์ฐ์ค๋ฌ์ด ๊ฒฐ๊ณผ๋ฅผ ๋ง๋ค์ด:
// ๊ฐ๋จํ ๋ฐ์ค ๋ธ๋ฌ
public Bitmap ApplyBoxBlur(Bitmap original, int radius)
{
Bitmap blurred = new Bitmap(original.Width, original.Height);
for (int y = 0; y < original.Height; y++)
{
for (int x = 0; x < original.Width; x++)
{
int r = 0, g = 0, b = 0;
int count = 0;
// ์ฃผ๋ณ ํฝ์
์ ํ๊ท ๊ณ์ฐ
for (int dy = -radius; dy <= radius; dy++)
{
for (int dx = -radius; dx <= radius; dx++)
{
int nx = x + dx;
int ny = y + dy;
if (nx >= 0 && nx < original.Width &&
ny >= 0 && ny < original.Height)
{
Color pixel = original.GetPixel(nx, ny);
r += pixel.R;
g += pixel.G;
b += pixel.B;
count++;
}
}
}
Color avgColor = Color.FromArgb(r / count, g / count, b / count);
blurred.SetPixel(x, y, avgColor);
}
}
return blurred;
}
// ๊ฐ์ฐ์์ ๋ธ๋ฌ (๋ ์์ฐ์ค๋ฌ์ด ๊ฒฐ๊ณผ)
public Bitmap ApplyGaussianBlur(Bitmap original, double sigma)
{
int radius = (int)Math.Ceiling(sigma * 3);
int size = radius * 2 + 1;
// ๊ฐ์ฐ์์ ์ปค๋ ์์ฑ
double[,] kernel = new double[size, size];
double sum = 0;
for (int y = -radius; y <= radius; y++)
{
for (int x = -radius; x <= radius; x++)
{
double value = Math.Exp(-(x * x + y * y) / (2 * sigma * sigma));
kernel[y + radius, x + radius] = value;
sum += value;
}
}
// ์ ๊ทํ
for (int y = 0; y < size; y++)
for (int x = 0; x < size; x++)
kernel[y, x] /= sum;
// ๋ธ๋ฌ ์ ์ฉ
Bitmap blurred = new Bitmap(original.Width, original.Height);
for (int y = 0; y < original.Height; y++)
{
for (int x = 0; x < original.Width; x++)
{
double r = 0, g = 0, b = 0;
for (int ky = 0; ky < size; ky++)
{
for (int kx = 0; kx < size; kx++)
{
int px = x + kx - radius;
int py = y + ky - radius;
if (px >= 0 && px < original.Width &&
py >= 0 && py < original.Height)
{
Color pixel = original.GetPixel(px, py);
double weight = kernel[ky, kx];
r += pixel.R * weight;
g += pixel.G * weight;
b += pixel.B * weight;
}
}
}
Color blurredColor = Color.FromArgb(
Math.Min(255, Math.Max(0, (int)r)),
Math.Min(255, Math.Max(0, (int)g)),
Math.Min(255, Math.Max(0, (int)b))
);
blurred.SetPixel(x, y, blurredColor);
}
}
return blurred;
}
๐ก ์ต์ ํ ํ: ์ ์ฝ๋๋ ์ดํดํ๊ธฐ ์ฝ๊ฒ ์์ฑํ์ง๋ง, ์ค์ ํ๋ก๋์ ์์๋ LockBits๋ฅผ ์ฌ์ฉํ๊ณ ์ํ/์์ง ๋ถ๋ฆฌ ๋ธ๋ฌ๋ฅผ ์ ์ฉํ๋ฉด ํจ์ฌ ๋น ๋ฅด๊ฒ ์ฒ๋ฆฌํ ์ ์์ด. ๊ฐ์ฐ์์ ๋ธ๋ฌ๋ ๋ถ๋ฆฌ ๊ฐ๋ฅํ ํํฐ๋ผ์ 2D ์ฐ์ฐ์ 1D ์ฐ์ฐ ๋ ๋ฒ์ผ๋ก ๋๋ ์ ์๊ฑฐ๋ !
๐ ์คํ๋(์ ๋ช
๋ ํฅ์)
// ์ธ์คํ ๋ง์คํฌ (Unsharp Mask)
public Bitmap ApplySharpen(Bitmap original, float amount)
{
// ๋ธ๋ฌ๋ ์ด๋ฏธ์ง ์์ฑ
Bitmap blurred = ApplyBoxBlur(original, 1);
Bitmap sharpened = new Bitmap(original.Width, original.Height);
for (int y = 0; y < original.Height; y++)
{
for (int x = 0; x < original.Width; x++)
{
Color origColor = original.GetPixel(x, y);
Color blurColor = blurred.GetPixel(x, y);
// ์๋ณธ - ๋ธ๋ฌ = ๋ํ
์ผ
// ์๋ณธ + (๋ํ
์ผ * amount) = ์คํ๋
int r = Math.Min(255, Math.Max(0,
(int)(origColor.R + (origColor.R - blurColor.R) * amount)));
int g = Math.Min(255, Math.Max(0,
(int)(origColor.G + (origColor.G - blurColor.G) * amount)));
int b = Math.Min(255, Math.Max(0,
(int)(origColor.B + (origColor.B - blurColor.B) * amount)));
sharpened.SetPixel(x, y, Color.FromArgb(r, g, b));
}
}
blurred.Dispose();
return sharpened;
}
// ์ปจ๋ณผ๋ฃจ์
ํํฐ๋ฅผ ์ฌ์ฉํ ์คํ๋
public Bitmap ApplyConvolutionSharpen(Bitmap original)
{
// ์คํ๋ ์ปค๋
double[,] kernel = new double[,]
{
{ 0, -1, 0 },
{-1, 5, -1 },
{ 0, -1, 0 }
};
return ApplyConvolution(original, kernel);
}
// ๋ฒ์ฉ ์ปจ๋ณผ๋ฃจ์
ํํฐ
public Bitmap ApplyConvolution(Bitmap original, double[,] kernel)
{
int kernelSize = kernel.GetLength(0);
int radius = kernelSize / 2;
Bitmap result = new Bitmap(original.Width, original.Height);
for (int y = radius; y < original.Height - radius; y++)
{
for (int x = radius; x < original.Width - radius; x++)
{
double r = 0, g = 0, b = 0;
for (int ky = 0; ky < kernelSize; ky++)
{
for (int kx = 0; kx < kernelSize; kx++)
{
Color pixel = original.GetPixel(
x + kx - radius,
y + ky - radius
);
double weight = kernel[ky, kx];
r += pixel.R * weight;
g += pixel.G * weight;
b += pixel.B * weight;
}
}
Color newColor = Color.FromArgb(
Math.Min(255, Math.Max(0, (int)r)),
Math.Min(255, Math.Max(0, (int)g)),
Math.Min(255, Math.Max(0, (int)b))
);
result.SetPixel(x, y, newColor);
}
}
return result;
}
๐ฌ ์ค์ ํ๋ก์ ํธ: ์ด๋ฏธ์ง ํธ์ง๊ธฐ ๋ง๋ค๊ธฐ
์ด์ ๋ฐฐ์ด ๋ด์ฉ์ ์ข
ํฉํด์ ์ค์ ๋ก ์ฌ์ฉํ ์ ์๋ ๊ฐ๋จํ ์ด๋ฏธ์ง ํธ์ง๊ธฐ๋ฅผ ๋ง๋ค์ด๋ณด์!
๐ ๏ธ ImageProcessor ํด๋์ค ์ค๊ณ
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
public class ImageProcessor
{
private Bitmap currentImage;
private Stack<Bitmap> undoStack;
private Stack<Bitmap> redoStack;
public ImageProcessor()
{
undoStack = new Stack<Bitmap>();
redoStack = new Stack<Bitmap>();
}
// ์ด๋ฏธ์ง ๋ก๋
public void LoadImage(string filePath)
{
if (currentImage != null)
currentImage.Dispose();
currentImage = new Bitmap(filePath);
undoStack.Clear();
redoStack.Clear();
}
// ํ์ฌ ์ด๋ฏธ์ง ๊ฐ์ ธ์ค๊ธฐ
public Bitmap GetCurrentImage()
{
return (Bitmap)currentImage.Clone();
}
// ์์
์ ์ํ ์ ์ฅ
private void SaveState()
{
undoStack.Push((Bitmap)currentImage.Clone());
redoStack.Clear();
}
// ์คํ ์ทจ์
public void Undo()
{
if (undoStack.Count > 0)
{
redoStack.Push(currentImage);
currentImage = undoStack.Pop();
}
}
// ๋ค์ ์คํ
public void Redo()
{
if (redoStack.Count > 0)
{
undoStack.Push(currentImage);
currentImage = redoStack.Pop();
}
}
// ํฌ๊ธฐ ์กฐ์
public void Resize(int width, int height, bool keepAspectRatio = true)
{
SaveState();
if (keepAspectRatio)
{
double ratioX = (double)width / currentImage.Width;
double ratioY = (double)height / currentImage.Height;
double ratio = Math.Min(ratioX, ratioY);
width = (int)(currentImage.Width * ratio);
height = (int)(currentImage.Height * ratio);
}
Bitmap resized = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(resized))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(currentImage, 0, 0, width, height);
}
currentImage.Dispose();
currentImage = resized;
}
// ํ์
public void Rotate(float angle)
{
SaveState();
Bitmap rotated = RotateImage(currentImage, angle);
currentImage.Dispose();
currentImage = rotated;
}
// ์๋ฅด๊ธฐ
public void Crop(Rectangle area)
{
SaveState();
Bitmap cropped = new Bitmap(area.Width, area.Height);
using (Graphics g = Graphics.FromImage(cropped))
{
g.DrawImage(currentImage,
new Rectangle(0, 0, area.Width, area.Height),
area,
GraphicsUnit.Pixel);
}
currentImage.Dispose();
currentImage = cropped;
}
// ๋ฐ๊ธฐ ์กฐ์
public void AdjustBrightness(int value)
{
SaveState();
ColorMatrix matrix = new ColorMatrix(new float[][]
{
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {value/255f, value/255f, value/255f, 0, 1}
});
ApplyColorMatrix(matrix);
}
// ๋๋น ์กฐ์
public void AdjustContrast(float value)
{
SaveState();
float t = (1.0f - value) / 2.0f;
ColorMatrix matrix = new ColorMatrix(new float[][]
{
new float[] {value, 0, 0, 0, 0},
new float[] {0, value, 0, 0, 0},
new float[] {0, 0, value, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {t, t, t, 0, 1}
});
ApplyColorMatrix(matrix);
}
// ์ฑ๋ ์กฐ์
public void AdjustSaturation(float saturation)
{
SaveState();
float rWeight = 0.3086f;
float gWeight = 0.6094f;
float bWeight = 0.0820f;
float a = (1.0f - saturation) * rWeight + saturation;
float b = (1.0f - saturation) * rWeight;
float c = (1.0f - saturation) * rWeight;
float d = (1.0f - saturation) * gWeight;
float e = (1.0f - saturation) * gWeight + saturation;
float f = (1.0f - saturation) * gWeight;
float g = (1.0f - saturation) * bWeight;
float h = (1.0f - saturation) * bWeight;
float i = (1.0f - saturation) * bWeight + saturation;
ColorMatrix matrix = new ColorMatrix(new float[][]
{
new float[] {a, b, c, 0, 0},
new float[] {d, e, f, 0, 0},
new float[] {g, h, i, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
ApplyColorMatrix(matrix);
}
// ๊ทธ๋ ์ด์ค์ผ์ผ
public void ConvertToGrayscale()
{
SaveState();
ColorMatrix matrix = new ColorMatrix(new float[][]
{
new float[] {0.299f, 0.299f, 0.299f, 0, 0},
new float[] {0.587f, 0.587f, 0.587f, 0, 0},
new float[] {0.114f, 0.114f, 0.114f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
ApplyColorMatrix(matrix);
}
// ์ธํผ์ ํจ๊ณผ
public void ApplySepia()
{
SaveState();
ColorMatrix matrix = new ColorMatrix(new float[][]
{
new float[] {0.393f, 0.349f, 0.272f, 0, 0},
new float[] {0.769f, 0.686f, 0.534f, 0, 0},
new float[] {0.189f, 0.168f, 0.131f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
ApplyColorMatrix(matrix);
}
// ColorMatrix ์ ์ฉ ํฌํผ ๋ฉ์๋
private void ApplyColorMatrix(ColorMatrix colorMatrix)
{
Bitmap result = new Bitmap(currentImage.Width, currentImage.Height);
using (Graphics g = Graphics.FromImage(result))
{
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
g.DrawImage(currentImage,
new Rectangle(0, 0, currentImage.Width, currentImage.Height),
0, 0, currentImage.Width, currentImage.Height,
GraphicsUnit.Pixel,
attributes);
}
currentImage.Dispose();
currentImage = result;
}
// ํ
์คํธ ์ถ๊ฐ
public void AddText(string text, Font font, Color color, Point position)
{
SaveState();
using (Graphics g = Graphics.FromImage(currentImage))
{
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
using (SolidBrush brush = new SolidBrush(color))
{
g.DrawString(text, font, brush, position);
}
}
}
// ์ํฐ๋งํฌ ์ถ๊ฐ
public void AddWatermark(string text, Font font, Color color, float opacity = 0.5f)
{
SaveState();
using (Graphics g = Graphics.FromImage(currentImage))
{
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
SizeF textSize = g.MeasureString(text, font);
float x = (currentImage.Width - textSize.Width) / 2;
float y = (currentImage.Height - textSize.Height) / 2;
Color watermarkColor = Color.FromArgb((int)(255 * opacity), color);
using (SolidBrush brush = new SolidBrush(watermarkColor))
{
g.DrawString(text, font, brush, x, y);
}
}
}
// ์ ์ฅ
public void Save(string filePath, ImageFormat format = null)
{
if (format == null)
{
string extension = System.IO.Path.GetExtension(filePath).ToLower();
format = extension switch
{
".jpg" or ".jpeg" => ImageFormat.Jpeg,
".png" => ImageFormat.Png,
".bmp" => ImageFormat.Bmp,
".gif" => ImageFormat.Gif,
".tiff" => ImageFormat.Tiff,
_ => ImageFormat.Png
};
}
currentImage.Save(filePath, format);
}
// JPEG ํ์ง ์ง์ ์ ์ฅ
public void SaveJpeg(string filePath, long quality)
{
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = new EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, quality);
currentImage.Save(filePath, jpegCodec, encoderParams);
}
// ์ฝ๋ฑ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
private ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == mimeType)
return codec;
}
return null;
}
// ํ์ ํฌํผ ๋ฉ์๋
private Bitmap RotateImage(Bitmap original, float angle)
{
double radians = angle * Math.PI / 180;
double cos = Math.Abs(Math.Cos(radians));
double sin = Math.Abs(Math.Sin(radians));
int newWidth = (int)(original.Width * cos + original.Height * sin);
int newHeight = (int)(original.Width * sin + original.Height * cos);
Bitmap rotated = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(rotated))
{
g.Clear(Color.Transparent);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.TranslateTransform(newWidth / 2f, newHeight / 2f);
g.RotateTransform(angle);
g.TranslateTransform(-original.Width / 2f, -original.Height / 2f);
g.DrawImage(original, new Point(0, 0));
}
return rotated;
}
// ๋ฆฌ์์ค ์ ๋ฆฌ
public void Dispose()
{
currentImage?.Dispose();
while (undoStack.Count > 0)
undoStack.Pop().Dispose();
while (redoStack.Count > 0)
redoStack.Pop().Dispose();
}
}
โจ ์ฌ์ฉ ์์ :
// ImageProcessor ์ฌ์ฉํ๊ธฐ
ImageProcessor processor = new ImageProcessor();
try
{
// ์ด๋ฏธ์ง ๋ก๋
processor.LoadImage(@"C:\Images\photo.jpg");
// ํฌ๊ธฐ ์กฐ์ (๋น์จ ์ ์ง)
processor.Resize(800, 600, keepAspectRatio: true);
// ๋ฐ๊ธฐ ์ฆ๊ฐ
processor.AdjustBrightness(20);
// ๋๋น ์ฆ๊ฐ
processor.AdjustContrast(1.2f);
// ์ํฐ๋งํฌ ์ถ๊ฐ
using (Font font = new Font("Arial", 24, FontStyle.Bold))
{
processor.AddWatermark("ยฉ 2024 MyPhoto", font, Color.White, 0.3f);
}
// ์ ์ฅ (๊ณ ํ์ง JPEG)
processor.SaveJpeg(@"C:\Images\edited.jpg", 95);
Console.WriteLine("์ด๋ฏธ์ง ์ฒ๋ฆฌ ์๋ฃ!");
}
finally
{
processor.Dispose();
}
๐ฏ WinForms์ ํตํฉํ๊ธฐ
์ด์ ๋ง๋ ImageProcessor๋ฅผ ์ค์ Windows Forms ์ ํ๋ฆฌ์ผ์ด์
์ ํตํฉํด๋ณด์!
๐ฅ๏ธ ๊ฐ๋จํ ์ด๋ฏธ์ง ๋ทฐ์ด ๋ง๋ค๊ธฐ
using System;
using System.Drawing;
using System.Windows.Forms;
public class ImageViewerForm : Form
{
private PictureBox pictureBox;
private MenuStrip menuStrip;
private ImageProcessor processor;
private string currentFilePath;
public ImageViewerForm()
{
InitializeComponents();
processor = new ImageProcessor();
}
private void InitializeComponents()
{
// ํผ ์ค์
this.Text = "C# Image Viewer";
this.Size = new Size(1000, 700);
this.StartPosition = FormStartPosition.CenterScreen;
// ๋ฉ๋ด ์คํธ๋ฆฝ
menuStrip = new MenuStrip();
// ํ์ผ ๋ฉ๋ด
ToolStripMenuItem fileMenu = new ToolStripMenuItem("ํ์ผ(&F)");
fileMenu.DropDownItems.Add("์ด๊ธฐ(&O)", null, OnOpenClick);
fileMenu.DropDownItems.Add("์ ์ฅ(&S)", null, OnSaveClick);
fileMenu.DropDownItems.Add("๋ค๋ฅธ ์ด๋ฆ์ผ๋ก ์ ์ฅ(&A)", null, OnSaveAsClick);
fileMenu.DropDownItems.Add(new ToolStripSeparator());
fileMenu.DropDownItems.Add("์ข
๋ฃ(&X)", null, (s, e) => this.Close());
// ํธ์ง ๋ฉ๋ด
ToolStripMenuItem editMenu = new ToolStripMenuItem("ํธ์ง(&E)");
editMenu.DropDownItems.Add("์คํ ์ทจ์(&U)", null, OnUndoClick);
editMenu.DropDownItems.Add("๋ค์ ์คํ(&R)", null, OnRedoClick);
editMenu.DropDownItems.Add(new ToolStripSeparator());
editMenu.DropDownItems.Add("ํฌ๊ธฐ ์กฐ์ (&S)", null, OnResizeClick);
editMenu.DropDownItems.Add("ํ์ (&T)", null, OnRotateClick);
editMenu.DropDownItems.Add("์๋ฅด๊ธฐ(&C)", null, OnCropClick);
// ํจ๊ณผ ๋ฉ๋ด
ToolStripMenuItem effectMenu = new ToolStripMenuItem("ํจ๊ณผ(&X)");
effectMenu.DropDownItems.Add("๋ฐ๊ธฐ/๋๋น(&B)", null, OnBrightnessContrastClick);
effectMenu.DropDownItems.Add("์ฑ๋(&S)", null, OnSaturationClick);
effectMenu.DropDownItems.Add(new ToolStripSeparator());
effectMenu.DropDownItems.Add("๊ทธ๋ ์ด์ค์ผ์ผ(&G)", null, OnGrayscaleClick);
effectMenu.DropDownItems.Add("์ธํผ์(&P)", null, OnSepiaClick);
effectMenu.DropDownItems.Add(new ToolStripSeparator());
effectMenu.DropDownItems.Add("์ํฐ๋งํฌ ์ถ๊ฐ(&W)", null, OnWatermarkClick);
menuStrip.Items.Add(fileMenu);
menuStrip.Items.Add(editMenu);
menuStrip.Items.Add(effectMenu);
// PictureBox
pictureBox = new PictureBox
{
Dock = DockStyle.Fill,
SizeMode = PictureBoxSizeMode.Zoom,
BackColor = Color.DarkGray
};
// ์ปจํธ๋กค ์ถ๊ฐ
this.Controls.Add(pictureBox);
this.Controls.Add(menuStrip);
this.MainMenuStrip = menuStrip;
}
private void OnOpenClick(object sender, EventArgs e)
{
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Filter = "์ด๋ฏธ์ง ํ์ผ|*.jpg;*.jpeg;*.png;*.bmp;*.gif|๋ชจ๋ ํ์ผ|*.*";
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
currentFilePath = dialog.FileName;
processor.LoadImage(currentFilePath);
UpdateDisplay();
}
catch (Exception ex)
{
MessageBox.Show($"์ด๋ฏธ์ง๋ฅผ ์ด ์ ์์ต๋๋ค: {ex.Message}",
"์ค๋ฅ", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void OnSaveClick(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(currentFilePath))
{
OnSaveAsClick(sender, e);
return;
}
try
{
processor.Save(currentFilePath);
MessageBox.Show("์ ์ฅ๋์์ต๋๋ค!", "์๋ฃ",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"์ ์ฅ ์คํจ: {ex.Message}",
"์ค๋ฅ", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void OnSaveAsClick(object sender, EventArgs e)
{
using (SaveFileDialog dialog = new SaveFileDialog())
{
dialog.Filter = "JPEG|*.jpg|PNG|*.png|BMP|*.bmp|๋ชจ๋ ํ์ผ|*.*";
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
processor.Save(dialog.FileName);
currentFilePath = dialog.FileName;
MessageBox.Show("์ ์ฅ๋์์ต๋๋ค!", "์๋ฃ",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"์ ์ฅ ์คํจ: {ex.Message}",
"์ค๋ฅ", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void OnUndoClick(object sender, EventArgs e)
{
processor.Undo();
UpdateDisplay();
}
private void OnRedoClick(object sender, EventArgs e)
{
processor.Redo();
UpdateDisplay();
}
private void OnResizeClick(object sender, EventArgs e)
{
using (ResizeDialog dialog = new ResizeDialog())
{
if (dialog.ShowDialog() == DialogResult.OK)
{
processor.Resize(dialog.NewWidth, dialog.NewHeight, dialog.KeepAspectRatio);
UpdateDisplay();
}
}
}
private void OnRotateClick(object sender, EventArgs e)
{
string input = Microsoft.VisualBasic.Interaction.InputBox(
"ํ์ ๊ฐ๋๋ฅผ ์
๋ ฅํ์ธ์ (๋):", "ํ์ ", "90");
if (float.TryParse(input, out float angle))
{
processor.Rotate(angle);
UpdateDisplay();
}
}
private void OnCropClick(object sender, EventArgs e)
{
MessageBox.Show("์๋ฅด๊ธฐ ๊ธฐ๋ฅ์ ๋ง์ฐ์ค ๋๋๊ทธ๋ก ์์ญ์ ์ ํํ ํ ์ฌ์ฉํ์ธ์.",
"์๋ด", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void OnBrightnessContrastClick(object sender, EventArgs e)
{
using (BrightnessContrastDialog dialog = new BrightnessContrastDialog())
{
if (dialog.ShowDialog() == DialogResult.OK)
{
processor.AdjustBrightness(dialog.Brightness);
processor.AdjustContrast(dialog.Contrast);
UpdateDisplay();
}
}
}
private void OnSaturationClick(object sender, EventArgs e)
{
string input = Microsoft.VisualBasic.Interaction.InputBox(
"์ฑ๋ ๊ฐ์ ์
๋ ฅํ์ธ์ (0.0 ~ 2.0):", "์ฑ๋ ์กฐ์ ", "1.0");
if (float.TryParse(input, out float saturation))
{
processor.AdjustSaturation(saturation);
UpdateDisplay();
}
}
private void OnGrayscaleClick(object sender, EventArgs e)
{
processor.ConvertToGrayscale();
UpdateDisplay();
}
private void OnSepiaClick(object sender, EventArgs e)
{
processor.ApplySepia();
UpdateDisplay();
}
private void OnWatermarkClick(object sender, EventArgs e)
{
string text = Microsoft.VisualBasic.Interaction.InputBox(
"์ํฐ๋งํฌ ํ
์คํธ๋ฅผ ์
๋ ฅํ์ธ์:", "์ํฐ๋งํฌ", "ยฉ 2024");
if (!string.IsNullOrEmpty(text))
{
using (Font font = new Font("Arial", 24, FontStyle.Bold))
{
processor.AddWatermark(text, font, Color.White, 0.3f);
UpdateDisplay();
}
}
}
private void UpdateDisplay()
{
if (pictureBox.Image != null)
pictureBox.Image.Dispose();
pictureBox.Image = processor.GetCurrentImage();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
processor?.Dispose();
pictureBox.Image?.Dispose();
base.OnFormClosing(e);
}
}
// ํ๋ก๊ทธ๋จ ์ง์
์
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new ImageViewerForm());
}
}
๐ก ์ถ๊ฐ ๊ธฐ๋ฅ ์์ด๋์ด:
โข ๋ง์ฐ์ค ๋๋๊ทธ๋ก ์๋ฅด๊ธฐ ์์ญ ์ ํ
โข ์ค์๊ฐ ํํฐ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
โข ํ์คํ ๊ทธ๋จ ํ์
โข ๋ฐฐ์น ์ฒ๋ฆฌ (์ฌ๋ฌ ์ด๋ฏธ์ง ๋์ ํธ์ง)
โข ๋ ์ด์ด ๊ธฐ๋ฅ
โข ํ๋ฌ๊ทธ์ธ ์์คํ
โข ํด๋ฆฝ๋ณด๋ ์ง์ (๋ณต์ฌ/๋ถ์ฌ๋ฃ๊ธฐ)
โก ์ฑ๋ฅ ์ต์ ํ ํ
์ด๋ฏธ์ง ์ฒ๋ฆฌ๋ CPU๋ฅผ ๋ง์ด ์ฌ์ฉํ๋ ์์
์ด์ผ. ์ฑ๋ฅ์ ์ต์ ํํ๋ ๋ฐฉ๋ฒ๋ค์ ์์๋ณด์!
๐ 1. LockBits ์ฌ์ฉํ๊ธฐ
์์์๋ ์ธ๊ธํ์ง๋ง, GetPixel/SetPixel ๋์ LockBits๋ฅผ ์ฌ์ฉํ๋ฉด ์์ฒญ๋ ์ฑ๋ฅ ํฅ์์ ์ป์ ์ ์์ด:
์ฑ๋ฅ ๋น๊ต (1920x1080 ์ด๋ฏธ์ง ๊ธฐ์ค):
โข GetPixel/SetPixel: ์ฝ 2,500ms
โข LockBits: ์ฝ 80ms
โข ์ฑ๋ฅ ํฅ์: ์ฝ 31๋ฐฐ!
๐ง 2. ๋ณ๋ ฌ ์ฒ๋ฆฌ ํ์ฉ
using System.Threading.Tasks;
public Bitmap ParallelGrayscale(Bitmap original)
{
Bitmap grayscale = new Bitmap(original.Width, original.Height);
Rectangle rect = new Rectangle(0, 0, original.Width, original.Height);
BitmapData originalData = original.LockBits(rect, ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
BitmapData grayscaleData = grayscale.LockBits(rect, ImageLockMode.WriteOnly,
PixelFormat.Format24bppRgb);
int bytes = Math.Abs(originalData.Stride) * original.Height;
byte[] rgbValues = new byte[bytes];
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(originalData.Scan0, rgbValues, 0, bytes);
// ๋ณ๋ ฌ ์ฒ๋ฆฌ
Parallel.For(0, original.Height, y =>
{
int offset = y * Math.Abs(originalData.Stride);
for (int x = 0; x < original.Width; x++)
{
int index = offset + x * 3;
byte gray = (byte)(rgbValues[index] * 0.114 + // Blue
rgbValues[index + 1] * 0.587 + // Green
rgbValues[index + 2] * 0.299); // Red
grayValues[index] = gray;
grayValues[index + 1] = gray;
grayValues[index + 2] = gray;
}
});
System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, grayscaleData.Scan0, bytes);
original.UnlockBits(originalData);
grayscale.UnlockBits(grayscaleData);
return grayscale;
}
โจ ์ฑ๋ฅ ํฅ์: ๋ฉํฐ์ฝ์ด CPU์์ ๋ณ๋ ฌ ์ฒ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ฉด ์ถ๊ฐ๋ก 2~4๋ฐฐ ์ ๋ ์ฑ๋ฅ์ด ํฅ์๋ผ. 4์ฝ์ด CPU์์๋ ์ฝ 3๋ฐฐ, 8์ฝ์ด์์๋ ์ฝ 5๋ฐฐ๊น์ง ๋นจ๋ผ์ง ์ ์์ด!
๐พ 3. ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ
// ๋์ ์: ๋ฉ๋ชจ๋ฆฌ ๋์ ๋ฐ์
public void BadExample()
{
for (int i = 0; i < 100; i++)
{
Bitmap img = new Bitmap(1000, 1000);
// img.Dispose()๋ฅผ ํธ์ถํ์ง ์์!
}
}
// ์ข์ ์: ์ฌ๋ฐ๋ฅธ ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ
public void GoodExample()
{
for (int i = 0; i < 100; i++)
{
using (Bitmap img = new Bitmap(1000, 1000))
{
// ์์
์ํ
} // using ๋ธ๋ก์ ๋ฒ์ด๋๋ฉด ์๋์ผ๋ก Dispose ํธ์ถ
}
}
// ๋ ์ข์ ์: ๊ฐ์ฒด ์ฌ์ฌ์ฉ
public void BetterExample()
{
Bitmap img = new Bitmap(1000, 1000);
try
{
for (int i = 0; i < 100; i++)
{
using (Graphics g = Graphics.FromImage(img))
{
g.Clear(Color.White);
// ์์
์ํ
}
}
}
finally
{
img.Dispose();
}
}
๐ 4. ์ ์ ํ ์ด๋ฏธ์ง ํฌ๋งท ์ ํ
JPEG
๐ท
โข ์์ค ์์ถ
โข ์ฌ์ง์ ์ ํฉ
โข ์์ ํ์ผ ํฌ๊ธฐ
โข ํฌ๋ช
๋ ๋ฏธ์ง์
PNG
๐จ
โข ๋ฌด์์ค ์์ถ
โข ํฌ๋ช
๋ ์ง์
โข ๊ทธ๋ํฝ์ ์ ํฉ
โข ํฐ ํ์ผ ํฌ๊ธฐ
BMP
๐ผ๏ธ
โข ์์ถ ์์
โข ๋น ๋ฅธ ์ฒ๋ฆฌ
โข ๋งค์ฐ ํฐ ํฌ๊ธฐ
โข ์์ ํ์ผ์ฉ
GIF
๐ฌ
โข 256์ ์ ํ
โข ์ ๋๋ฉ์ด์
์ง์
โข ํฌ๋ช
๋ ์ง์
โข ๋จ์ ๊ทธ๋ํฝ์ฉ
โ๏ธ 5. Graphics ํ์ง ์ค์ ์ต์ ํ
// ๊ณ ํ์ง ์ค์ (๋๋ฆผ)
public void HighQualitySettings(Graphics g)
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
}
// ๊ท ํ์กํ ์ค์ (๊ถ์ฅ)
public void BalancedSettings(Graphics g)
{
g.InterpolationMode = InterpolationMode.Bilinear;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.PixelOffsetMode = PixelOffsetMode.Half;
g.CompositingQuality = CompositingQuality.AssumeLinear;
}
// ๊ณ ์ ์ค์ (๋น ๋ฆ)
public void FastSettings(Graphics g)
{
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.SmoothingMode = SmoothingMode.None;
g.PixelOffsetMode = PixelOffsetMode.Default;
g.CompositingQuality = CompositingQuality.HighSpeed;
}
๐ฎ .NET Core/.NET 5+ ํ๊ฒฝ์์์ System.Drawing
.NET Core์ .NET 5 ์ด์์์๋ System.Drawing์ ๋ช ๊ฐ์ง ๋ณํ๊ฐ ์์ด. ์์๋๋ฉด ์ข์ ๋ด์ฉ๋ค์ ์ ๋ฆฌํด๋ณผ๊ฒ!
โ ๏ธ System.Drawing.Common์ ๋ณํ
์ค์ํ ๋ณ๊ฒฝ์ฌํญ:
.NET 6๋ถํฐ System.Drawing.Common์ Windows์์๋ง ์ง์๋ผ. Linux์ macOS์์๋ ๋ฐํ์ ์์ธ๊ฐ ๋ฐ์ํ ์ ์์ด.
ํฌ๋ก์ค ํ๋ซํผ ๊ฐ๋ฐ์ ์ํด์๋ ๋ค์ ๋์๋ค์ ๊ณ ๋ คํด์ผ ํด:
โข ImageSharp - ์์ C#๋ก ์์ฑ๋ ํฌ๋ก์ค ํ๋ซํผ ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
โข SkiaSharp - Google์ Skia ๊ทธ๋ํฝ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ .NET ๋ฐ์ธ๋ฉ
โข Magick.NET - ImageMagick์ .NET ๋ํผ
๐ง System.Drawing.Common ์ฌ์ฉ ์ค์
// .csproj ํ์ผ์ ์ถ๊ฐ
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<!-- Windows์์๋ง System.Drawing.Common ์ฌ์ฉ ํ์ฉ -->
<EnableWindowsTargeting>true</EnableWindowsTargeting>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
</ItemGroup>
๐จ ImageSharp ์ฌ์ฉ ์์
ํฌ๋ก์ค ํ๋ซํผ์ด ํ์ํ๋ค๋ฉด ImageSharp์ ์ฌ์ฉํ๋ ๊ฒ๋ ์ข์ ์ ํ์ด์ผ:
// NuGet ํจํค์ง ์ค์น: SixLabors.ImageSharp
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.PixelFormats;
// ์ด๋ฏธ์ง ๋ก๋ ๋ฐ ์ฒ๋ฆฌ
using (Image<Rgba32> image = Image.Load<Rgba32>("input.jpg"))
{
// ํฌ๊ธฐ ์กฐ์
image.Mutate(x => x.Resize(800, 600));
// ๊ทธ๋ ์ด์ค์ผ์ผ
image.Mutate(x => x.Grayscale());
// ํ์
image.Mutate(x => x.Rotate(45));
// ๋ฐ๊ธฐ ์กฐ์
image.Mutate(x => x.Brightness(1.2f));
// ์ ์ฅ
image.Save("output.jpg");
}
// ํ
์คํธ ์ถ๊ฐ
using (Image<Rgba32> image = Image.Load<Rgba32>("input.jpg"))
{
image.Mutate(x => x.DrawText(
"Hello ImageSharp!",
SystemFonts.CreateFont("Arial", 48),
Color.White,
new PointF(100, 100)
));
image.Save("output.jpg");
}
๐ก ImageSharp vs System.Drawing:
ImageSharp ์ฅ์ :
โข ํฌ๋ก์ค ํ๋ซํผ ์ง์
โข ํ๋์ ์ธ API ๋์์ธ
โข ํ๋ฐํ ๊ฐ๋ฐ๊ณผ ์
๋ฐ์ดํธ
โข ๋ ๋์ ์ฑ๋ฅ (์ผ๋ถ ์์
์์)
System.Drawing ์ฅ์ :
โข Windows์์ ๊ฒ์ฆ๋ ์์ ์ฑ
โข ๋ฐฉ๋ํ ๋ ๊ฑฐ์ ์ฝ๋์ ์์
โข GDI+์ ๋ชจ๋ ๊ธฐ๋ฅ ์ ๊ทผ
โข ์ถ๊ฐ ํจํค์ง ์ค์น ๋ถํ์ (.NET Framework)
๐ ์ค์ ํ์ฉ ์ฌ๋ก
์ด์ ์ค์ ๋ก ์ด๋ป๊ฒ ํ์ฉํ ์ ์๋์ง ๋ช ๊ฐ์ง ์ฌ๋ก๋ฅผ ์ดํด๋ณด์!
๐ธ 1. ์ธ๋ค์ผ ์์ฑ๊ธฐ
public class ThumbnailGenerator
{
public void GenerateThumbnails(string sourceFolder, string targetFolder,
int maxWidth, int maxHeight)
{
if (!Directory.Exists(targetFolder))
Directory.CreateDirectory(targetFolder);
string[] imageFiles = Directory.GetFiles(sourceFolder, "*.*")
.Where(f => f.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ||
f.EndsWith(".png", StringComparison.OrdinalIgnoreCase) ||
f.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase))
.ToArray();
Parallel.ForEach(imageFiles, imageFile =>
{
try
{
using (Image original = Image.FromFile(imageFile))
{
// ๋น์จ ์ ์งํ๋ฉฐ ํฌ๊ธฐ ๊ณ์ฐ
double ratioX = (double)maxWidth / original.Width;
double ratioY = (double)maxHeight / original.Height;
double ratio = Math.Min(ratioX, ratioY);
int newWidth = (int)(original.Width * ratio);
int newHeight = (int)(original.Height * ratio);
using (Bitmap thumbnail = new Bitmap(newWidth, newHeight))
{
using (Graphics g = Graphics.FromImage(thumbnail))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(original, 0, 0, newWidth, newHeight);
}
string fileName = Path.GetFileName(imageFile);
string targetPath = Path.Combine(targetFolder,
$"thumb_{fileName}");
thumbnail.Save(targetPath, ImageFormat.Jpeg);
}
}
Console.WriteLine($"์์ฑ ์๋ฃ: {Path.GetFileName(imageFile)}");
}
catch (Exception ex)
{
Console.WriteLine($"์ค๋ฅ ({Path.GetFileName(imageFile)}): {ex.Message}");
}
});
}
}
// ์ฌ์ฉ ์์
ThumbnailGenerator generator = new ThumbnailGenerator();
generator.GenerateThumbnails(@"C:\Photos", @"C:\Thumbnails", 200, 200);
๐ผ๏ธ 2. ์ด๋ฏธ์ง ์ํฐ๋งํฌ ๋ฐฐ์น ์ฒ๋ฆฌ
public class WatermarkProcessor
{
private string watermarkText;
private Font watermarkFont;
private Color watermarkColor;
private float opacity;
public WatermarkProcessor(string text, Font font, Color color, float opacity = 0.3f)
{
this.watermarkText = text;
this.watermarkFont = font;
this.watermarkColor = color;
this.opacity = opacity;
}
public void ProcessFolder(string sourceFolder, string targetFolder)
{
if (!Directory.Exists(targetFolder))
Directory.CreateDirectory(targetFolder);
string[] imageFiles = Directory.GetFiles(sourceFolder, "*.jpg");
foreach (string imageFile in imageFiles)
{
try
{
using (Bitmap original = new Bitmap(imageFile))
using (Bitmap watermarked = AddWatermark(original))
{
string fileName = Path.GetFileName(imageFile);
string targetPath = Path.Combine(targetFolder, fileName);
// JPEG ํ์ง 90์ผ๋ก ์ ์ฅ
SaveJpeg(watermarked, targetPath, 90);
}
Console.WriteLine($"์ฒ๋ฆฌ ์๋ฃ: {Path.GetFileName(imageFile)}");
}
catch (Exception ex)
{
Console.WriteLine($"์ค๋ฅ: {ex.Message}");
}
}
}
private Bitmap AddWatermark(Bitmap original)
{
Bitmap result = (Bitmap)original.Clone();
using (Graphics g = Graphics.FromImage(result))
{
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// ํ
์คํธ ํฌ๊ธฐ ์ธก์
SizeF textSize = g.MeasureString(watermarkText, watermarkFont);
// ์ฐ์ธก ํ๋จ์ ๋ฐฐ์น
float x = result.Width - textSize.Width - 20;
float y = result.Height - textSize.Height - 20;
// ๋ฐํฌ๋ช
์์
Color color = Color.FromArgb((int)(255 * opacity), watermarkColor);
using (SolidBrush brush = new SolidBrush(color))
{
g.DrawString(watermarkText, watermarkFont, brush, x, y);
}
}
return result;
}
private void SaveJpeg(Bitmap image, string filePath, long quality)
{
ImageCodecInfo jpegCodec = ImageCodecInfo.GetImageEncoders()
.First(c => c.MimeType == "image/jpeg");
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = new EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, quality);
image.Save(filePath, jpegCodec, encoderParams);
}
}
// ์ฌ์ฉ ์์
using (Font font = new Font("Arial", 24, FontStyle.Bold))
{
WatermarkProcessor processor = new WatermarkProcessor(
"ยฉ 2024 MyCompany", font, Color.White, 0.5f);
processor.ProcessFolder(@"C:\OriginalPhotos", @"C:\WatermarkedPhotos");
}
๐ 3. ์ฐจํธ ์์ฑ๊ธฐ
public class SimpleChartGenerator
{
public Bitmap CreateBarChart(string title, Dictionary<string, double> data,
int width = 800, int height = 600)
{
Bitmap chart = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(chart))
{
g.Clear(Color.White);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// ์ฌ๋ฐฑ ์ค์
int marginLeft = 80;
int marginRight = 40;
int marginTop = 80;
int marginBottom = 80;
int chartWidth = width - marginLeft - marginRight;
int chartHeight = height - marginTop - marginBottom;
// ์ ๋ชฉ ๊ทธ๋ฆฌ๊ธฐ
using (Font titleFont = new Font("๋ง์ ๊ณ ๋", 24, FontStyle.Bold))
using (SolidBrush blackBrush = new SolidBrush(Color.Black))
{
SizeF titleSize = g.MeasureString(title, titleFont);
float titleX = (width - titleSize.Width) / 2;
g.DrawString(title, titleFont, blackBrush, titleX, 20);
}
// ์ต๋๊ฐ ์ฐพ๊ธฐ
double maxValue = data.Values.Max();
// ๋ง๋ ๊ทธ๋ฆฌ๊ธฐ
int barCount = data.Count;
int barWidth = chartWidth / barCount - 20;
int barSpacing = 20;
using (Font labelFont = new Font("๋ง์ ๊ณ ๋", 10))
using (SolidBrush blackBrush = new SolidBrush(Color.Black))
{
int index = 0;
foreach (var item in data)
{
// ๋ง๋ ๋์ด ๊ณ์ฐ
int barHeight = (int)(item.Value / maxValue * chartHeight);
// ๋ง๋ ์์น
int x = marginLeft + index * (barWidth + barSpacing);
int y = marginTop + chartHeight - barHeight;
// ๋ง๋ ์์ (๊ทธ๋ผ๋์ธํธ)
using (LinearGradientBrush barBrush = new LinearGradientBrush(
new Rectangle(x, y, barWidth, barHeight),
Color.FromArgb(100, 150, 255),
Color.FromArgb(50, 100, 200),
LinearGradientMode.Vertical))
{
g.FillRectangle(barBrush, x, y, barWidth, barHeight);
}
// ํ
๋๋ฆฌ
using (Pen borderPen = new Pen(Color.FromArgb(30, 80, 180), 2))
{
g.DrawRectangle(borderPen, x, y, barWidth, barHeight);
}
// ๊ฐ ํ์
string valueText = item.Value.ToString("F1");
SizeF valueSize = g.MeasureString(valueText, labelFont);
g.DrawString(valueText, labelFont, blackBrush,
x + (barWidth - valueSize.Width) / 2,
y - valueSize.Height - 5);
// ๋ ์ด๋ธ ํ์
SizeF labelSize = g.MeasureString(item.Key, labelFont);
g.DrawString(item.Key, labelFont, blackBrush,
x + (barWidth - labelSize.Width) / 2,
marginTop + chartHeight + 10);
index++;
}
}
// ์ถ ๊ทธ๋ฆฌ๊ธฐ
using (Pen axisPen = new Pen(Color.Black, 2))
{
// Y์ถ
g.DrawLine(axisPen, marginLeft, marginTop,
marginLeft, marginTop + chartHeight);
// X์ถ
g.DrawLine(axisPen, marginLeft, marginTop + chartHeight,
marginLeft + chartWidth, marginTop + chartHeight);
}
// Y์ถ ๋๊ธ
using (Font axisFont = new Font("๋ง์ ๊ณ ๋", 9))
using (SolidBrush blackBrush = new SolidBrush(Color.Black))
using (Pen gridPen = new Pen(Color.LightGray, 1))
{
gridPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
for (int i = 0; i <= 5; i++)
{
double value = maxValue * i / 5;
int y = marginTop + chartHeight - (int)(chartHeight * i / 5.0);
// ๋๊ธ ์
g.DrawLine(gridPen, marginLeft, y,
marginLeft + chartWidth, y);
// ๋๊ธ ๊ฐ
string label = value.ToString("F0");
SizeF labelSize = g.MeasureString(label, axisFont);
g.DrawString(label, axisFont, blackBrush,
marginLeft - labelSize.Width - 10,
y - labelSize.Height / 2);
}
}
}
return chart;
}
}
// ์ฌ์ฉ ์์
SimpleChartGenerator generator = new SimpleChartGenerator();
Dictionary<string, double> salesData = new Dictionary<string, double>
{
{ "1์", 150.5 },
{ "2์", 200.3 },
{ "3์", 180.7 },
{ "4์", 220.1 },
{ "5์", 250.8 },
{ "6์", 280.4 }
};
using (Bitmap chart = generator.CreateBarChart("์๋ณ ๋งค์ถ ํํฉ", salesData))
{
chart.Save(@"C:\Charts\sales_chart.png", ImageFormat.Png);
}
โจ ์ฌ๋ฅ๋ท(https://www.jaenung.net)์์ ๋ฐ์ดํฐ ์๊ฐํ๋ ๋ฆฌํฌํธ ์์ฑ ์ฌ๋ฅ์ ์ ๊ณตํ๋ ๋ถ๋ค์ด ์ด๋ฐ ์ฐจํธ ์์ฑ ๊ธฐ๋ฅ์ ๋ง์ด ํ์ฉํด. ์๋ํ๋ ๋ฆฌํฌํธ ์์คํ ์ ๋ง๋ค ๋ ์ ๋ง ์ ์ฉํ๊ฑฐ๋ !
๐ ์์ฃผ ๋ฐ์ํ๋ ๋ฌธ์ ์ ํด๊ฒฐ๋ฐฉ๋ฒ
์ด๋ฏธ์ง ์ฒ๋ฆฌ๋ฅผ ํ๋ค ๋ณด๋ฉด ์์ฃผ ๋ง์ฃผ์น๋ ๋ฌธ์ ๋ค์ด ์์ด. ๋ฏธ๋ฆฌ ์์๋๋ฉด ์๊ฐ์ ๋ง์ด ์ ์ฝํ ์ ์์ง!
โ ๋ฌธ์ 1: "GDI+ ์ผ๋ฐ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค"
์์ธ:
โข ํ์ผ์ด ๋ค๋ฅธ ํ๋ก์ธ์ค์์ ์ฌ์ฉ ์ค
โข ํ์ผ ๊ฒฝ๋ก์ ์ฐ๊ธฐ ๊ถํ ์์
โข ์ด๋ฏธ์ง ํฌ๋งท์ด ์ง์๋์ง ์์
โข ๋ฉ๋ชจ๋ฆฌ ๋ถ์กฑ
ํด๊ฒฐ๋ฐฉ๋ฒ:
// ๋์ ์
Bitmap img = new Bitmap("image.jpg");
img.Save("image.jpg"); // ๊ฐ์ ํ์ผ์ ์ ์ฅ ์๋ - ์ค๋ฅ!
// ์ข์ ์ 1: ๋ค๋ฅธ ํ์ผ๋ช
์ผ๋ก ์ ์ฅ
Bitmap img = new Bitmap("image.jpg");
img.Save("image_edited.jpg");
img.Dispose();
// ์ข์ ์ 2: ์คํธ๋ฆผ ์ฌ์ฉ
using (FileStream fs = new FileStream("image.jpg", FileMode.Open))
{
Bitmap img = new Bitmap(fs);
// ์์
์ํ
img.Save("output.jpg");
img.Dispose();
}
// ์ข์ ์ 3: ๋ณต์ฌ๋ณธ ๋ง๋ค๊ธฐ
using (Image original = Image.FromFile("image.jpg"))
{
Bitmap copy = new Bitmap(original);
copy.Save("image.jpg"); // ์๋ณธ ํ์ผ ๋ฎ์ด์ฐ๊ธฐ ๊ฐ๋ฅ
copy.Dispose();
}
โ ๋ฌธ์ 2: ๋ฉ๋ชจ๋ฆฌ ๋์
์ฆ์:
โข ํ๋ก๊ทธ๋จ์ด ์ ์ ๋๋ ค์ง
โข ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด ๊ณ์ ์ฆ๊ฐ
โข OutOfMemoryException ๋ฐ์
ํด๊ฒฐ๋ฐฉ๋ฒ:
// ๋์ ์
public void ProcessImages()
{
for (int i = 0; i < 1000; i++)
{
Bitmap img = new Bitmap(1000, 1000);
// Dispose ํธ์ถ ์ ํจ!
}
}
// ์ข์ ์
public void ProcessImages()
{
for (int i = 0; i < 1000; i++)
{
using (Bitmap img = new Bitmap(1000, 1000))
{
// ์์
์ํ
} // ์๋์ผ๋ก Dispose ํธ์ถ
}
}
// ๋ ์ข์ ์: ๋ช
์์ GC ํธ์ถ (๋๋ ์ฒ๋ฆฌ ์)
public void ProcessManyImages()
{
for (int i = 0; i < 1000; i++)
{
using (Bitmap img = new Bitmap(1000, 1000))
{
// ์์
์ํ
}
// 100๊ฐ๋ง๋ค ๊ฐ๋น์ง ์ปฌ๋ ์
if (i % 100 == 0)
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
}
}
โ ๋ฌธ์ 3: ์ด๋ฏธ์ง ํ์ง ์ ํ
์์ธ:
โข ์๋ชป๋ InterpolationMode ์ค์
โข JPEG ์์ถ ํ์ง์ด ๋๋ฌด ๋ฎ์
โข ์ฌ๋ฌ ๋ฒ ์ ์ฅ/๋ก๋ ๋ฐ๋ณต
ํด๊ฒฐ๋ฐฉ๋ฒ:
// ๊ณ ํ์ง ํฌ๊ธฐ ์กฐ์
public Bitmap HighQualityResize(Image original, int width, int height)
{
Bitmap resized = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(resized))
{
// ์ต๊ณ ํ์ง ์ค์
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.DrawImage(original, 0, 0, width, height);
}
return resized;
}
// ๊ณ ํ์ง JPEG ์ ์ฅ
public void SaveHighQualityJpeg(Bitmap image, string path)
{
ImageCodecInfo jpegCodec = ImageCodecInfo.GetImageEncoders()
.First(c => c.MimeType == "image/jpeg");
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = new EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, 95L); // 95% ํ์ง
image.Save(path, jpegCodec, encoderParams);
}
โ ๋ฌธ์ 4: ํฌ๋ช
๋ ์ฒ๋ฆฌ ๋ฌธ์
์ฆ์:
โข PNG ํฌ๋ช
๋ฐฐ๊ฒฝ์ด ๊ฒ์์์ผ๋ก ๋ณํจ
โข ์ํ ์ฑ๋์ด ์์ค๋จ
ํด๊ฒฐ๋ฐฉ๋ฒ:
// ํฌ๋ช
๋๋ฅผ ์ ์งํ๋ฉฐ ํฌ๊ธฐ ์กฐ์
public Bitmap ResizeWithTransparency(Image original, int width, int height)
{
// PixelFormat.Format32bppArgb ์ฌ์ฉ (์ํ ์ฑ๋ ํฌํจ)
Bitmap resized = new Bitmap(width, height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(resized))
{
// ํฌ๋ช
๋ฐฐ๊ฒฝ
g.Clear(Color.Transparent);
// CompositingMode ์ค์
g.CompositingMode = CompositingMode.SourceOver;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(original, 0, 0, width, height);
}
return resized;
}
// PNG๋ก ์ ์ฅ (ํฌ๋ช
๋ ์ ์ง)
resized.Save("output.png", ImageFormat.Png);
โ ๋ฌธ์ 5: ๋์ฉ๋ ์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ ์ฑ๋ฅ ์ ํ
ํด๊ฒฐ๋ฐฉ๋ฒ:
// 1. ์ด๋ฏธ์ง๋ฅผ ์์ ํ์ผ๋ก ๋๋์ด ์ฒ๋ฆฌ
public void ProcessLargeImageInTiles(Bitmap largeImage, int tileSize)
{
int tilesX = (int)Math.Ceiling((double)largeImage.Width / tileSize);
int tilesY = (int)Math.Ceiling((double)largeImage.Height / tileSize);
Parallel.For(0, tilesX * tilesY, tileIndex =>
{
int tileX = tileIndex % tilesX;
int tileY = tileIndex / tilesX;
int x = tileX * tileSize;
int y = tileY * tileSize;
int w = Math.Min(tileSize, largeImage.Width - x);
int h = Math.Min(tileSize, largeImage.Height - y);
Rectangle tileRect = new Rectangle(x, y, w, h);
// ํ์ผ ์ฒ๋ฆฌ
ProcessTile(largeImage, tileRect);
});
}
// 2. ์คํธ๋ฆฌ๋ฐ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌ
public void ProcessImageStreaming(string inputPath, string outputPath)
{
using (FileStream inputStream = new FileStream(inputPath, FileMode.Open))
using (Image image = Image.FromStream(inputStream, false, false))
{
// ๋ฉํ๋ฐ์ดํฐ๋ง ์ฝ๊ณ ํ์ํ ๋ถ๋ถ๋ง ๋ก๋
// ์์
์ํ
using (FileStream outputStream = new FileStream(outputPath, FileMode.Create))
{
image.Save(outputStream, ImageFormat.Jpeg);
}
}
}
๐ ์ถ๊ฐ ํ์ต ๋ฆฌ์์ค์ ํ
์ฌ๊ธฐ๊น์ง ์๋ค๋ฉด ์ด๋ฏธ C# ์ด๋ฏธ์ง ์ฒ๋ฆฌ์ ๊ธฐ์ด๋ ๋ง์คํฐํ ๊ฑฐ์ผ! ํ์ง๋ง ๋ฐฐ์์๋ ๋์ด ์์ง. ๋ ๊น์ด ๊ณต๋ถํ๊ณ ์ถ๋ค๋ฉด ์ด๋ฐ ๊ฒ๋ค์ ์ดํด๋ณด๋ฉด ์ข์:
๐ ์ถ์ฒ ํ์ต ์ฃผ์
๊ณ ๊ธ ํํฐ๋ง
๐จ
โข ๊ฐ์ฐ์์ ๋ธ๋ฌ
โข ์ฃ์ง ๊ฒ์ถ
โข ๋ชจ์
๋ธ๋ฌ
โข ๋
ธ์ด์ฆ ์ ๊ฑฐ
์ด๋ฏธ์ง ๋ถ์
๐
โข ํ์คํ ๊ทธ๋จ ๋ถ์
โข ์์ ๋ถํฌ
โข ๋ฐ๊ธฐ ๋ถ์
โข ํจํด ์ธ์
๊ณ ๊ธ ๋ณํ
๐
โข ์๊ทผ ๋ณํ
โข ์๊ณก ๋ณด์
โข ๋ชจํ
โข ํ๋
ธ๋ผ๋ง
์ต์ ํ ๊ธฐ๋ฒ
โก
โข SIMD ํ์ฉ
โข GPU ๊ฐ์
โข ๋ณ๋ ฌ ์ฒ๋ฆฌ
โข ์บ์ฑ ์ ๋ต
๐ก ์ค์ ํ๋ก์ ํธ ์์ด๋์ด
1. ์ฌ์ง ํธ์ง ์ฑ
ํํฐ, ํฌ๋กญ, ํ์ , ํ
์คํธ ์ถ๊ฐ ๋ฑ์ ๊ธฐ๋ฅ์ ๊ฐ์ง ์์ ํ ์ฌ์ง ํธ์ง ์ ํ๋ฆฌ์ผ์ด์
2. ๋ฐฐ์น ์ด๋ฏธ์ง ํ๋ก์ธ์
์ฌ๋ฌ ์ด๋ฏธ์ง๋ฅผ ํ ๋ฒ์ ์ฒ๋ฆฌํ๋ ๋๊ตฌ (ํฌ๊ธฐ ์กฐ์ , ์ํฐ๋งํฌ, ํฌ๋งท ๋ณํ)
3. ์คํฌ๋ฆฐ์ท ๋๊ตฌ
ํ๋ฉด ์บก์ฒ, ์ฃผ์ ์ถ๊ฐ, ์๋ ์
๋ก๋ ๊ธฐ๋ฅ
4. ์ด๋ฏธ์ง ๋น๊ต ๋๊ตฌ
๋ ์ด๋ฏธ์ง์ ์ฐจ์ด์ ์ ์ฐพ์๋ด๋ ํ๋ก๊ทธ๋จ
5. QR ์ฝ๋ ์์ฑ๊ธฐ
ํ
์คํธ๋ URL์ QR ์ฝ๋ ์ด๋ฏธ์ง๋ก ๋ณํ
6. ์ฝ๋ผ์ฃผ ๋ฉ์ด์ปค
์ฌ๋ฌ ์ฌ์ง์ ์กฐํฉํด์ ํ๋์ ์ฝ๋ผ์ฃผ ์ด๋ฏธ์ง ์์ฑ
7. ์ด๋ฏธ์ง ๋ฉํ๋ฐ์ดํฐ ํธ์ง๊ธฐ
EXIF ์ ๋ณด ์ฝ๊ธฐ/์ฐ๊ธฐ, GPS ์ ๋ณด ๊ด๋ฆฌ
8. ์๋ ๋ฆฌ์ฌ์ด์ง ์๋น์ค
์น ์๋ฒ์์ ์์ฒญ์ ๋ฐ๋ผ ์ด๋ฏธ์ง ํฌ๊ธฐ๋ฅผ ๋์ ์ผ๋ก ์กฐ์
๐ฏ ์ฑ๋ฅ ๋ฒค์น๋งํน ํ
using System.Diagnostics;
public class PerformanceTester
{
public void BenchmarkImageOperation(Action operation, string operationName)
{
// ์๋ฐ์
(JIT ์ปดํ์ผ)
operation();
// ์ค์ ์ธก์
Stopwatch sw = Stopwatch.StartNew();
operation();
sw.Stop();
Console.WriteLine($"{operationName}: {sw.ElapsedMilliseconds}ms");
}
public void ComparePerformance()
{
Bitmap testImage = new Bitmap(1920, 1080);
// GetPixel/SetPixel vs LockBits ๋น๊ต
BenchmarkImageOperation(() =>
{
ConvertToGrayscaleGetPixel(testImage);
}, "GetPixel/SetPixel");
BenchmarkImageOperation(() =>
{
ConvertToGrayscaleLockBits(testImage);
}, "LockBits");
BenchmarkImageOperation(() =>
{
ConvertToGrayscaleParallel(testImage);
}, "LockBits + Parallel");
testImage.Dispose();
}
}
๐ง ๋๋ฒ๊น
ํ
// ์ด๋ฏธ์ง ์ ๋ณด ์ถ๋ ฅ ํฌํผ
public void PrintImageInfo(Image image, string label = "Image")
{
Console.WriteLine($"=== {label} ===");
Console.WriteLine($"ํฌ๊ธฐ: {image.Width} x {image.Height}");
Console.WriteLine($"ํฝ์
ํฌ๋งท: {image.PixelFormat}");
Console.WriteLine($"ํด์๋: {image.HorizontalResolution} x {image.VerticalResolution} DPI");
Console.WriteLine($"์๋ณธ ํฌ๋งท: {image.RawFormat}");
Console.WriteLine($"ํ๋ ์ ์: {image.GetFrameCount(FrameDimension.Page)}");
Console.WriteLine();
}
// ์ฒ๋ฆฌ ์ ํ ๋น๊ต
public void CompareImages(Bitmap before, Bitmap after)
{
Console.WriteLine("=== ์ด๋ฏธ์ง ๋น๊ต ===");
Console.WriteLine($"ํฌ๊ธฐ ๋ณํ: {before.Width}x{before.Height} โ {after.Width}x{after.Height}");
// ํ์ผ ํฌ๊ธฐ ๋น๊ต (๋ฉ๋ชจ๋ฆฌ ์คํธ๋ฆผ ์ฌ์ฉ)
using (MemoryStream msBefore = new MemoryStream())
using (MemoryStream msAfter = new MemoryStream())
{
before.Save(msBefore, ImageFormat.Png);
after.Save(msAfter, ImageFormat.Png);
Console.WriteLine($"๋ฉ๋ชจ๋ฆฌ ํฌ๊ธฐ: {msBefore.Length / 1024}KB โ {msAfter.Length / 1024}KB");
}
}
๐ฌ ๋ง๋ฌด๋ฆฌํ๋ฉฐ
์! ์ฌ๊ธฐ๊น์ง ์ ๋ง ๊ธด ์ฌ์ ์ด์์ด. C# System.Drawing์ ํ์ฉํ ์ด๋ฏธ์ง ์ฒ๋ฆฌ์ ๊ฑฐ์ ๋ชจ๋ ๊ฒ์ ๋ค๋ค๋ดค์ด.
๐ ์ฐ๋ฆฌ๊ฐ ๋ฐฐ์ด ๋ด์ฉ ์ ๋ฆฌ:
โ
System.Drawing ๋ค์์คํ์ด์ค์ ๊ธฐ๋ณธ ๊ตฌ์กฐ์ ์ฃผ์ ํด๋์ค๋ค
โ
์ด๋ฏธ์ง ๋ถ๋ฌ์ค๊ธฐ, ์ ์ฅํ๊ธฐ, ํฌ๊ธฐ ์กฐ์ ํ๊ธฐ
โ
Graphics ํด๋์ค๋ก ๋ํ๊ณผ ํ
์คํธ ๊ทธ๋ฆฌ๊ธฐ
โ
๋ค์ํ ์ด๋ฏธ์ง ํํฐ์ ํจ๊ณผ ์ ์ฉํ๊ธฐ
โ
ColorMatrix๋ฅผ ํ์ฉํ ์์ ๋ณํ
โ
๊ณ ์ฑ๋ฅ ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๊ธฐ๋ฒ (LockBits, ๋ณ๋ ฌ ์ฒ๋ฆฌ)
โ
์ค์ ํ๋ก์ ํธ: ImageProcessor ํด๋์ค ๊ตฌํ
โ
WinForms์์ ํตํฉ
โ
์ฑ๋ฅ ์ต์ ํ ๋ฐฉ๋ฒ
โ
์์ฃผ ๋ฐ์ํ๋ ๋ฌธ์ ์ ํด๊ฒฐ๋ฐฉ๋ฒ
โ
.NET Core/5+ ํ๊ฒฝ์์์ ๋ณํ์ ๋์
๐ช ์ด์ ๋น์ ์:
โข ํ๋ก๊ทธ๋๋ฐ์ผ๋ก ์ด๋ฏธ์ง๋ฅผ ์์ ์์ฌ๋ก ๋ค๋ฃฐ ์ ์์ด
โข ์๋ํ๋ ์ด๋ฏธ์ง ์ฒ๋ฆฌ ์์คํ
์ ๋ง๋ค ์ ์์ด
โข ์ฑ๋ฅ์ ๊ณ ๋ คํ ํจ์จ์ ์ธ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ด
โข ์ค์ ํ๋ก์ ํธ์ ๋ฐ๋ก ์ ์ฉํ ์ ์๋ ์ค๋ ฅ์ ๊ฐ์ท์ด
๐ ๋ค์ ๋จ๊ณ:
์ด์ ๋ฐฐ์ด ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์์ ๋ง์ ํ๋ก์ ํธ๋ฅผ ๋ง๋ค์ด๋ด. ์์ ๊ฒ๋ถํฐ ์์ํด์ ์ ์ ๋ณต์กํ ๊ธฐ๋ฅ์ ์ถ๊ฐํด๊ฐ๋ฉด์ ์ค๋ ฅ์ ํค์๋๊ฐ๋ฉด ๋ผ.
ํน์ ๋งํ๋ ๋ถ๋ถ์ด ์๋ค๋ฉด, ์ด ๊ธ์ ๋ค์ ์ฐธ๊ณ ํ๊ฑฐ๋ ๊ณต์ ๋ฌธ์๋ฅผ ์ฐพ์๋ณด๋ ๊ฒ๋ ์ข์. ๊ทธ๋ฆฌ๊ณ ์ง์ ์ฝ๋๋ฅผ ์์ฑํ๊ณ ์คํํด๋ณด๋ ๊ฒ ๊ฐ์ฅ ์ค์ํด. ์ด๋ก ๋ง ์๋ ๊ฒ๊ณผ ์ง์ ํด๋ณด๋ ๊ฒ์ ์์ ํ ๋ค๋ฅด๊ฑฐ๋ !
๐ ๋ง์ง๋ง์ผ๋ก, ํ๋ก๊ทธ๋๋ฐ์ ๋์์๋ ํ์ต์ ๊ณผ์ ์ด์ผ. ์๋ก์ด ๊ธฐ์ ์ด ๊ณ์ ๋์ค๊ณ , ๋ ๋์ ๋ฐฉ๋ฒ๋ค์ด ๋ฐ๊ฒฌ๋์ง. ํ์ง๋ง ์ฌ๊ธฐ์ ๋ฐฐ์ด ๊ธฐ์ด ๊ฐ๋
๋ค์ ์ด๋ค ํ๊ฒฝ์์๋ ์ ์ฉํ๊ฒ ์ฐ์ผ ๊ฑฐ์ผ.
ํ์ด์ ๋น์ด! ๋ฉ์ง ์ด๋ฏธ์ง ์ฒ๋ฆฌ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด๋ณด๊ธธ ๋ฐ๋๊ฒ! ๐จโจ
๐ฏ ์ด ๊ธ์ด ๋์์ด ๋์๋์?
C# ์ด๋ฏธ์ง ์ฒ๋ฆฌ์ ๋ํด ๋ ๊ถ๊ธํ ์ ์ด ์๋ค๋ฉด,
์ธ์ ๋ ์ง ์ง๋ฌธํ๊ณ ํจ๊ป ์ฑ์ฅํด์!
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.