使用 C# 图形类调整图像大小时出现质量问题

发布于 2024-08-24 01:36:34 字数 545 浏览 6 评论 0原文

我正在将小图像(例如 20x25)调整为较大图像(例如 150x170)。 我的问题不在于质量,正如预期的那样,质量有些模糊。我的问题是,边框是在图像的右侧和底部创建浅色边框。有没有办法可以将其删除?

我的代码如下:

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

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最佳男配角 2024-08-31 01:36:34

将此语句添加到您的代码中:

  g.PixelOffsetMode = PixelOffsetMode.Half;

您现在将获得所有 4 个面都同样“亮”的图像。我认为这并不能真正解决你的问题。但这是不可避免的,插值器只是用完位图边缘的可用像素来做出更好的猜测。

您最好将 PixelOffsetMode 保留其原始设置,并故意将图像绘制得太大,以便边缘效果不可见。

这看起来不错:

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

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);
}
鸵鸟症 2024-08-31 01:36:34

也许尝试添加

g.PixelOffsetMode = PixelOffsetMode.HighQuality;

Maybe try adding

g.PixelOffsetMode = PixelOffsetMode.HighQuality;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文