상위분류 : 잡필방
|
중위분류 : 서류가방
|
하위분류 : 전산과 컴퓨터
|
작성자 : 문시형
|
작성일 : 2018-12-02
|
조회수 : 4,520
|
제 목 : C# 이미지 리사이즈(비율 유지)
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(); |
|
} |
|
} |
|
|
|
|
목록으로