C# imageresizing in application running Medium Trust

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 :)

Tags: , , , ,

.NET

Tilføj kommentar

  Country flag

biuquote
  • Kommentar
  • Eksempel
Loading

Page List

About

Take a look at my LinkedIn-profile.

DISCALIMER: The content on this blog is in NO way affiliated with my employer. The opinions expressed on this blog are solely my own and in NO way endorsed by my employer.

Recent comments

Comment RSS

Month List