Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

.net - How to draw smooth images with C#?

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).

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

What was correct? PictureBox doesn't expose InterpolationMode.

What was off base?

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) 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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...