星期六, 1月 02, 2021

[C#] 繪製線條、曲線 - 平滑化

參考該篇 - Antialiasing with Lines and Curves 的練習筆記,該文章內容 Aliasing 和 Antialiasing 說明是精華,擷取示意圖來筆記

[C#] 繪製線條、曲線 - 平滑化-1

SmoothingMode 有六種列舉,可以分為三類
 
類型說明
InvalidSpecifies an invalid mode.
None、Default、HighSpeedSpecifies no antialiasing.
AntiAlias、HighQualitySpecifies antialiased rendering.

有兩個平滑化情況,無法依靠 SmoothingMode 達到
C# 測試 Code
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace GraphicsSample
{
    public partial class FrmLineCurveAntialiAsing : Form
    {
        public FrmLineCurveAntialiAsing()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            int width = 100;
            int height = 50;

            using (Pen p = new Pen(Color.Red, 2))
            {
                e.Graphics.DrawEllipse(p, 10, 10, width, height);

                // 平滑化
                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                e.Graphics.DrawEllipse(p, 130, 10, width, height);
            }
        }
    }
}
[C#] 繪製線條、曲線 - 平滑化-2

沒有留言:

張貼留言