English 中文(简体)
Quality Problem when resizing images using C# Graphics Class
原标题:

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!

最佳回答

Maybe try adding

g.PixelOffsetMode = PixelOffsetMode.HighQuality;
问题回答

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签