views:

323

answers:

2

I am resizing an small images (eg 20x25) to larger images (eg 150x170). My problem is not about quality, which as expected is has some blurring. My problem is that a border is that a light colour border is being created on the right hand side and bottom of the image. Is there a way that this can be removed?

My code is the following:

using (Graphics g = Graphics.FromImage((Image)ResizedImage))
{
    g.CompositingQuality = CompositingQuality.HighQuality;
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.SmoothingMode = SmoothingMode.HighQuality;

    g.DrawImage(OrigImage, new Rectangle(0, 0, Width, Height),
     new Rectangle(0, 0, OrigCImage.Width, OrigImage.Height), GraphicsUnit.Pixel);
}

thanks!

+1  A: 

Add this statement to your code:

  g.PixelOffsetMode = PixelOffsetMode.Half;

You'll now get an image that is equally "light" on all 4 sides. That doesn't really solve your problem I would assume. But it is fairly inevitable, the interpolator simply runs out of usable pixels at the edges of the bitmap to make a better guess.

You might be better off by leaving the PixelOffsetMode at its original setting and intentionally drawing the image too large so the edge effects are not visible.

This looked good:

protected override void OnPaint(PaintEventArgs e) {
  e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
  e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
  Image img = Properties.Resources.progress;
  int w = this.ClientSize.Width + this.ClientSize.Width / img.Width;
  int h = this.ClientSize.Height + this.ClientSize.Height / img.Height;
  Rectangle rc = new Rectangle(0, 0, w, h);
  e.Graphics.DrawImage(img, rc);
}
Hans Passant
A: 

Maybe try adding

g.PixelOffsetMode = PixelOffsetMode.HighQuality;
AVB