이 주먹은 원본에 맞는 사각형 크기의 메모리 내부 비트 맵 square
을 만듭니다. 그런 다음 thumbSize
까지 크기를 조정합니다.
string imagefolder = @"C:\Users\russ\Originals";
string thumbfolder = @"C:\Users\russ\Squares";
int thumbSize = 100;
foreach (string file in System.IO.Directory.GetFiles(imagefolder, "*.jpg"))
{
using (Image original = Bitmap.FromFile(file))
{
Size size = new Size(
Math.Min(original.Width, original.Height),
Math.Min(original.Width, original.Height)
);
int translateX = (size.Width - original.Width)/2;
int translateY = (size.Height - original.Height)/2;
using (Bitmap square = new Bitmap(size.Width, size.Height))
{
using (Graphics g = Graphics.FromImage(square))
{
g.DrawImage(original, translateX, translateY, original.Width, original.Height);
}
using (Bitmap thumb = new Bitmap(thumbSize, thumbSize))
{
using (Graphics g2 = Graphics.FromImage(thumb))
{
g2.DrawImage(square, 0, 0, thumbSize, thumbSize);
}
string thumbFile = Path.Combine(thumbfolder, Path.GetFileName(file));
thumb.Save(thumbFile, ImageFormat.Jpeg);
}
}
}
}
이미지를 사용하지 않는 줄을 보지 못했습니다 : $ –