开发者

Quality Problem when resizing images using C# Graphics Class

开发者 https://www.devze.com 2022-12-21 22:41 出处:网络
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

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!


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);
}


Maybe try adding

g.PixelOffsetMode = PixelOffsetMode.HighQuality;
0

精彩评论

暂无评论...
验证码 换一张
取 消