如何使用C#绘制平滑图像? [英] How to draw smooth images with C#?

查看:455
本文介绍了如何使用C#绘制平滑图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to draw images on a C# form (in PictureBoxes, as well as using Graphics.DrawImage()), and am looking for a way to draw them smooth. The images must be a format that supports transparency, so PNG, GIF, SVG, and WMF. C# doesn't support SVG files out of the box, and I haven't found a good third-party library to use (I found SvgNet, but couldn't figure it out).

我需要绘制一个WMF文件,C#可以通过Image.FromFile()函数来完成该文件,但是它不是抗锯齿的.我想知道是否有任何方法可以解决这个问题?

I need to draw a WMF file, which C# can do via the Image.FromFile() function, but it's not anti-aliased. I was wondering if there's any way to smooth this out?

推荐答案

先前的答案虽然很好,但仅部分正确.

The previous answers, while well intended were only partially correct.

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

What was correct? PictureBox doesn't expose InterpolationMode.

什么是基地?

1)尽管您可以在图片框的Paint事件中,在其父级或通过派生类中的替代轻松设置该属性. . .不管哪种方式,两者都一样容易.但是,除非设置了SmoothingMode,否则将忽略InterpolationMode.如果将SmoothingMode设置为SmoothingMode.AnitAlias,您将不会获得任何抗锯齿.

1) While you can easily set that property in the Paint event from the picture box, in its parent, or via an override in a derived class. . . Either way works and both are just as easy. However, unless SmoothingMode is set, the InterpolationMode will be ignored. You won't get any anti-aliasing without SmoothingMode set to SmoothingMode.AnitAlias.

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

2) Using a Panel when you've clearly expressed an interest in using the features of PictureBox is the wrong direction to go. You will lack any ability to load, save, or assign images directly to it without explicitly coding those properties. . . Why re-invent the wheel? By deriving off of PictureBox you get all of that for free.

由于我为您完成了艰苦的工作,因此新闻变得更好了,并且花费的时间比编写此消息要少.

The news gets even better as I've done the hard work for you and it took me less time than writing this message.

我提供了两个版本,这两个版本均源自PictureBox. First 是一个简单的示例,始终使用最佳质量的渲染.这也是最慢的渲染. Second 是一个类,该类允许任何人通过派生类的属性来设置各种呈现参数.设置后,这些将用于OnPaint覆盖中.

I've provided two version both of which derive from PictureBox. First is a simple example which always uses the best quality rendering possible. This is also the slowest rendering. Second is a class that allows anyone to set the various rendering parameters via properties off the derived class. Once set these are used in the OnPaint override.

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

这篇关于如何使用C#绘制平滑图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆