GDI畫圖時的鋸齒效果弱化
當在畫圖時,圖形有時會出現鋸齒,可以使用SmoothingMode.AntiAlias來消除
代碼如下:
g.SmoothingMode = SmoothingMode.AntiAlias;
使圖像的邊緣圓滑清晰銳化的可以試試FillPath
代碼如下
g.FillPath((Brushes.Black), path);
或者是針對於文本鋸齒的話,可以採用TextRenderingHint
代碼如下
SolidBrush brush = new SolidBrush(Color.Green);
e.Graphics.DrawString("ABCDEFGHIJKL", new Font("宋體", 15f), brush, 0, 20);
//消除鋸齒
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
e.Graphics.DrawString("ABCDEFGHIJKL", new Font("宋體", 15f), brush, 0, 50);
有時候使用Graphics.Clear()方法,會在圖像上出現顆粒型的點,儘量不要採用Graphics.Clear()來填充區域,一般使用Graphics.FillPath();
有興趣的可以嘗試下下面的代碼:
public Form11()
{
InitializeComponent();
this.BackColor = Color.Black;
Method();
this.Paint += Draw;
}
Bitmap bitmap1;
private void Draw(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(bitmap1, 0, 0);
e.Graphics.DrawString("ABCDEFGHIJKL", new Font("宋體", 15f), new SolidBrush(Color.Green), 0, 50);
}
private void Method()
{
bitmap1 = new Bitmap(500, 500);
Graphics g = Graphics.FromImage(bitmap1);
GraphicsPath path = new GraphicsPath();
//g.SmoothingMode = SmoothingMode.AntiAlias; //使繪圖質量最高,即消除鋸齒
//g.CompositingMode = CompositingMode.SourceCopy;
//g.TextRenderingHint = TextRenderingHint.AntiAlias;
int alpha = 1;
for (int i = 20; i > 2; i--)
{
path.Reset();
path.AddEllipse(20, 20, 50, 50);
path.Widen(new Pen(Color.Black, i));
g.SetClip(path);
//g.Clear(Color.Black); // wrong
g.ResetClip();
//g.FillPath((Brushes.Black), path); // right
g.FillPath(new SolidBrush(Color.FromArgb(alpha += 10, 255, 255, 255)), path);
}
}