private Bitmap resize(Stream SourceImage, int MaxWidth, int MaxHeight)
{
Bitmap a = null;
using (Image i = Image.FromStream(SourceImage))
{
int _maxWidth = (MaxWidth > 0) ? MaxWidth : i.Width;
int _maxHeight = (MaxHeight > 0) ? MaxHeight : i.Height;
double _scaleWidth = (double)_maxWidth / (double)i.Width;
double _scaleHeight = (double)_maxHeight / (double)i.Height;
double _scale = (_scaleHeight < _scaleWidth) ? _scaleHeight : _scaleWidth;
_scale = (_scale > 1) ? 1 : _scale;
int _newWidth = (int)(_scale * i.Width);
int _newHeight = (int)(_scale * i.Height);
a = new Bitmap(MaxWidth, MaxHeight);
using (Graphics g = Graphics.FromImage(a))
{
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(i, new Rectangle(0, 0, _newWidth, _newHeight));
g.Save();
}
}
return a;
}