Помните правила Клуба Оптимизации:
Так, принимая Вы на самом деле имеете рабочий код, запускаете Вашу программу под Devel:: NYTProf.
Находят узкие места. Тогда возвратитесь сюда, чтобы сказать нам, каковы они.
, Если Вы не делаете , имеют рабочий код, получают его работающий сначала. Единственная самая большая оптимизация, которую Вы будете когда-либо делать, идет от нерабочего до работы.
Find which is smaller: MaxWidth / w
or MaxHeight / h
Then multiply w
and h
by that number
Explanation:
You need to find the scaling factor which makes the image fit.
To find the scaling factor, s
, for the width, then s
must be such that:
s * w = MaxWidth
.
Therefore, the scaling factor is MaxWidth / w
.
Similarly for height.
The one that requires the most scaling (smaller s
) is the factor by which you must scale the whole image.
I'd similar problem and I've found that very helpful: article. As I understood correctly you need to resize the image?
Python code, but maybe it will point you in the right direction:
def fit_within_box(box_width, box_height, width, height):
"""
Returns a tuple (new_width, new_height) which has the property
that it fits within box_width and box_height and has (close to)
the same aspect ratio as the original size
"""
new_width, new_height = width, height
aspect_ratio = float(width) / float(height)
if new_width > box_width:
new_width = box_width
new_height = int(new_width / aspect_ratio)
if new_height > box_height:
new_height = box_height
new_width = int(new_height * aspect_ratio)
return (new_width, new_height)
Исходя из предложения Эрика, я бы сделал что-то вроде этого:
private static Size ExpandToBound(Size image, Size boundingBox)
{
double widthScale = 0, heightScale = 0;
if (image.Width != 0)
widthScale = (double)boundingBox.Width / (double)image.Width;
if (image.Height != 0)
heightScale = (double)boundingBox.Height / (double)image.Height;
double scale = Math.Min(widthScale, heightScale);
Size result = new Size((int)(image.Width * scale),
(int)(image.Height * scale));
return result;
}
Я мог бы немного переборщить с слепками, но я просто пытался сохранить точность расчетов.
]Чтобы выполнить заполнение формата вместо соотношения сторон, используйте большее соотношение сторон. То есть измените код Мэтта с Math.Min на Math.Max.
(заливка аспекта не оставляет пустым ни один ограничивающий прямоугольник, но может вывести часть изображения за границы, в то время как при подгонке аспекта изображение не останется за пределами границ, но может оставить часть ограничивающей рамки пустой.)