Я несколько раз сталкивался с этим и думал, что было бы неплохо поместить его туда. Какова ваша лучшая модификация изображения и/или кадрирующая логика. Идея заключается в том, что вызывается какой-либо метод с целевым изображением, размерами и флагом обрезки - это вернет или сохранит или сохранит желаемое изображение.Лучшая логика изменения размера и/или обрезка
Шахта внизу. Преобразован с VB на C#, так что да, будут небольшие ошибки, но логика - это то, на что мы смотрим.
// INIT
// On/off
bool WeAreCropping = true;
// Get some dimensions
int TargetWidth = RequestedWidth;
int TargetHeight = RequestedHeight;
int SourceWidth = SourceImage.Width;
int SourceHeight = SourceImage.Height;
int ResizeWidth = TargetWidth;
int ResizeHeight = TargetHeight;
// GET RESIZE VALUES
// Are we cropping?
if (WeAreCropping) {
// Get source and target aspect ratio
double SourceAspectRatio = SourceWidth/SourceHeight;
double TargetAspectRatio = TargetWidth/TargetHeight;
// Compare aspect ratios to find out if we should we resize by
// width or height or nothing
if (TargetAspectRatio < SourceAspectRatio) {
ResizeWidth = TargetHeight/SourceHeight * SourceWidth;
}
else if (TargetAspectRatio > SourceAspectRatio) {
ResizeHeight = TargetWidth/SourceWidth * SourceHeight;
}
else {
// Same aspect ratio
}
}
else {
// If the target image is bigger than the source
if (TargetWidth > SourceWidth && TargetHeight > SourceHeight) {
TargetWidth = SourceWidth;
TargetHeight = SourceHeight;
}
double Ratio = 0;
// What ratio should we resize it by
if (SourceWidth/TargetWidth > SourceHeight/TargetHeight) {
Ratio = SourceWidth/TargetWidth;
}
else {
Ratio = SourceHeight/TargetHeight;
}
ResizeWidth = Math.Ceiling(SourceWidth/Ratio);
ResizeHeight = Math.Ceiling(SourceHeight/Ratio);
}
// TIME TO DO SUMFINK
// Resize the image using ResizeWidth and ResizeHeight
// Do it
if (WeAreCropping) {
// Crop the resized image at the center TargetWidth and TargetHeight
// Do it
}
Я подозреваю, что это может быть намного элегантнее, поэтому, возможно, вы могли бы сделать лучше.
Спасибо за ссылку. – Maleks
Есть специальная кнопка для «спасибо» :)) –
Ссылка была сломана – Flexicoder