Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C# 이미지 리사이즈(비율 유지)
using System;
using System.Drawing;
namespace ImageConvertor {
class ImageConvertor {
/**
* <summary>
* 이미지를 targetX, targetY로 리사이징함. 비율은 유지하고 빈 공간은 검은색으로 채움
* </summary>
*/
public static void Resize(string importPath, string exportPath, int targetX, int targetY) {
Image originalImage = Image.FromFile(importPath);
double ratioX = targetX / (double)originalImage.Width;
double ratioY = targetY / (double)originalImage.Height;
double ratio = Math.Min(ratioX, ratioY);
int newWidth = (int)(originalImage.Width * ratio);
int newHeight = (int)(originalImage.Height * ratio);
Bitmap newImage = new Bitmap(targetX, targetY);
using (Graphics g = Graphics.FromImage(newImage)) {
g.FillRectangle(Brushes.Black, 0, 0, newImage.Width, newImage.Height);
g.DrawImage(originalImage, (targetX - newWidth) / 2, (targetY - newHeight) / 2, newWidth, newHeight);
}
newImage.Save(exportPath);
originalImage.Dispose();
newImage.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.