Июл
13
Ранее я уже писал об изменении размеров загружаемых картинок и фоток, но в ходе реализации оного на новом проекте решил сделать иначе: незамарачиваясь.
Вот простой код:
private void SaveAvatarImg(Stream stream, int _size, string filename)
{
Image img = Image.FromStream(stream);
int width = img.Width;
int height = img.Height;
int theight = 0;
int twidth = 0;
if (height >= width)
{
theight = (int)(_size);
twidth = (int)width * _size / height;
}
else
{
twidth = _size;
theight = (int)height * _size / width;
}
Bitmap newimg = new Bitmap(twidth, theight, img.PixelFormat);
img.GetThumbnailImage(twidth, theight, null, IntPtr.Zero).Save(Server.MapPath("/upload/users/") + filename);
}
{
Image img = Image.FromStream(stream);
int width = img.Width;
int height = img.Height;
int theight = 0;
int twidth = 0;
if (height >= width)
{
theight = (int)(_size);
twidth = (int)width * _size / height;
}
else
{
twidth = _size;
theight = (int)height * _size / width;
}
Bitmap newimg = new Bitmap(twidth, theight, img.PixelFormat);
img.GetThumbnailImage(twidth, theight, null, IntPtr.Zero).Save(Server.MapPath("/upload/users/") + filename);
}
Используем так:
SaveAvatarImg(avatarImg.InputStream, size, avatarImg.FileName);
avatarImg – загружаемый файл, максимальный размер одной из сторон (высоты или ширины). Получается вполне приемлимо.
Подробнее здесь Image.GetThumbnailImage
Там же отмечено, что если размер желаемой миниатюры больше 300 пикселей, то лучше использовать метод из прошлой заметки.






