by Martin
6. januar 2009 20:03
I've for some time had a homemade gallery online (which works pretty ok for me). It leverages some code which resizes a Bitmap-object. After moving to a new hosting provider some of my code threw "Out of memory"-exceptions when resizing. I found out this is because the new hosting provider is running their Windows servers set up to Medium Trust. Following is the way I solved the problem. Old code (faulty if running in medium trust, but I don't think it's faulty in high trust):
public static Bitmap ResizeImage(Image b, int nWidth, int nHeight)
{
Bitmap result = new Bitmap(nWidth, nHeight);
using (Graphics g = Graphics.FromImage(result))
{
g.DrawImage(b, 0, 0, nWidth, nHeight);
g.Save();
}
return result;
}
New working code, now with added imagequalitysettings:
public static Bitmap ResizeImage(Image b, int nWidth, int nHeight)
{
Bitmap result = new Bitmap(nWidth, nHeight);
using (Graphics g = Graphics.FromImage(result))
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
g.DrawImage(b, 0, 0, nWidth, nHeight);
g.Save();
}
return result;
}
Might work for you too :)