7

我正在尝试在 C# 表单上绘制图像(在 PictureBoxes 中以及使用 Graphics.DrawImage()),并且正在寻找一种将它们绘制平滑的方法。图像必须是支持透明度的格式,例如 PNG、GIF、SVG 和 WMF。C# 不支持开箱即用的 SVG 文件,而且我还没有找到一个好的第三方库可以使用(我找到了 SvgNet,但无法弄清楚)。

我需要绘制一个 WMF 文件,C# 可以通过 Image.FromFile() 函数完成该文件,但它没有抗锯齿。我想知道有什么办法可以解决这个问题吗?

4

2 回答 2

16

先前的答案虽然是出于好意,但只是部分正确。

什么是正确的?PictureBox 不公开 InterpolationMode。

出了什么问题?

1) 虽然您可以轻松地从图片框、其父级或通过派生类中的覆盖在 Paint 事件中设置该属性。. . 无论哪种方式都有效,两者都一样容易。但是,除非设置 SmoothingMode,否则 InterpolationMode 将被忽略。如果将 SmoothingMode 设置为 SmoothingMode.AnitAlias,您将不会获得任何抗锯齿。

2) 当您明确表示有兴趣使用 PictureBox 的功能时使用面板是错误的方向。您将无法直接加载、保存或分配图像,而无需显式编码这些属性。. . 为什么要重新发明轮子? 通过从 PictureBox 派生,您可以免费获得所有这些。

消息变得更好,因为我已经为您完成了艰苦的工作,而且我花费的时间比写此消息要少。

我提供了两个版本,它们都来自 PictureBox。首先是一个简单的示例,它始终使用可能的最佳质量渲染。这也是最慢的渲染。第二个是允许任何人通过派生类的属性设置各种渲染参数的类。一旦设置,这些将在 OnPaint 覆盖中使用。

public class HighQualitySmoothPictureBox : PictureBox
{
    protected override void OnPaint(PaintEventArgs pe)
    {
        // This is the only line needed for anti-aliasing to be turned on.
        pe.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

        // the next two lines of code (not comments) are needed to get the highest 
        // possible quiality of anti-aliasing. Remove them if you want the image to render faster.
        pe.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        pe.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        // this line is needed for .net to draw the contents.
        base.OnPaint(pe);
    }
}

...

public class ConfigurableQualityPictureBox : PictureBox
{
    // Note: the use of the "?" indicates the value type is "nullable."  
    // If the property is unset, it doesn't have a value, and therefore isn't 
    // used when the OnPaint method executes.
    System.Drawing.Drawing2D.SmoothingMode? smoothingMode;
    System.Drawing.Drawing2D.CompositingQuality? compositingQuality;
    System.Drawing.Drawing2D.InterpolationMode? interpolationMode;

    public System.Drawing.Drawing2D.SmoothingMode? SmoothingMode
    {
        get { return smoothingMode; }
        set { smoothingMode = value; }
    }

    public System.Drawing.Drawing2D.CompositingQuality? CompositingQuality
    {
        get { return compositingQuality; }
        set { compositingQuality = value; }
    }

    public System.Drawing.Drawing2D.InterpolationMode? InterpolationMode
    {
        get { return interpolationMode; }
        set { interpolationMode = value; }
    }

    protected override void OnPaint(PaintEventArgs pe)
    {
        if (smoothingMode.HasValue)
            pe.Graphics.SmoothingMode = smoothingMode.Value;
        if (compositingQuality.HasValue)
            pe.Graphics.CompositingQuality = compositingQuality.Value;
        if (interpolationMode.HasValue)
            pe.Graphics.InterpolationMode = interpolationMode.Value;

        // this line is needed for .net to draw the contents.
        base.OnPaint(pe);
    }
}
于 2009-11-21T05:18:14.380 回答
7

将图像绘制到画布上时,您可以将插值模式更改为比最近邻更好的方式,以使调整大小的图像平滑:

Graphics g = ...
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(...);

您需要添加 System.Drawing.Drawing2D 以获取 InterpolationMode 枚举。

使用 PictureBox 将是一个问题 - 它不会公开 InterpolationMode 属性,因此您需要自己滚动或下载一个。

于 2009-02-19T17:21:22.610 回答