Resize an Image in C#

tech — tags: , — rohand @ June 3, 2010 11:46 AM

This function will let you resize an image in C#.

public static Bitmap Resize(Bitmap original,
                        int width, int height)
{
  Bitmap bitmap = new Bitmap(width, height);

  using (Graphics gfx = Graphics.FromImage(bitmap))
  {
    gfx.InterpolationMode
            = Drawing2D.InterpolationMode.HighQualityBicubic;
    gfx.SmoothingMode
            = Drawing2D.SmoothingMode.HighQuality;
    gfx.PixelOffsetMode
            = Drawing2D.PixelOffsetMode.HighQuality;
    gfx.CompositingQuality
            = Drawing2D.CompositingQuality.HighQuality;

    gfx.DrawImage(original, 0, 0, width, height);

    return bitmap;
  }
}

Do Not .IndexOf Without Further Checks

tech — tags: , — rohand @ January 26, 2010 11:42 AM

Do not do this:

if ("A,B,C,D,E".IndexOf(key) > -1)
{
    // CODE REDACTED
}

If key is an empty string, the result is 0 and the code in the IF block is executed. This is probably not what you expected.

©2010 appytizers. All rights reserved.