C#窗口形式的外观精美的图形 [英] Good looking graphics in C# window form
问题描述
我需要使用C#在Windows窗体中创建一些简单的图形.简单来说,我指的是线条,圆形等.实心圆,其边缘不平滑(如使用正方形像素绘制圆时所预期的那样),但是在矢量程序中以相同像素数绘制相同圆时,它看起来很完美.我已经在Inkscape中绘制了此示例.
I need to create some simple graphics in a windows form using C#. By simple I mean lines, circles etc. However, when I draw e.g. a filled circle, the edge is not smooth (as expected when drawing a circle using square pixels), but when drawing the same circle with the same number of pixels in a vector program it looks perfect. I have been drawing in Inkscape for this example.
也许矢量软件使用某种渲染功能来使颜色平滑,但这在C#中无需创建太多代码就可以吗?这是代码示例,它使用Graphics
创建要绘制的画布.
Maybe the vector software uses some sort of render function to smooth the colors, but is this possible in C# without creating too much code? Here is an example of the code, which is using Graphics
to create a canvas to draw on.
private void StatGraphicsPanel_Paint(object sender, PaintEventArgs e)
{
Graphics canvas = e.Graphics;
Brush brush = Brushes.Aqua;
canvas.FillEllipse(brush, 0, 0, 10, 10);
}
解决方案 这段代码可以达到目的:
Solution This code does the trick:
private void StatGraphicsPanel_Paint(object sender, PaintEventArgs e)
{
Graphics canvas = e.Graphics;
canvas.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Brush brush = Brushes.Aqua;
canvas.FillEllipse(brush, 0, 0, 10, 10);
}
推荐答案
You want to use the System.Drawing.Graphics.SmoothingMode
property. Before beginning to use your Graphics
object, do this:
canvas.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
那应该给你一个漂亮的抗锯齿的椭圆,而不是默认的锯齿状的椭圆.
That should give you a nice anti-aliased ellipse instead of the default jaggy one.
Similarly, if you want to draw high-quality text in an image, use the System.Drawing.Graphics.TextRenderingHint
property.
正如其他人在评论中提到的那样,您可能需要考虑使用WPF. WinForms已过时.
As others have mentioned in comments, you might want to consider using WPF instead. WinForms are dated.
这篇关于C#窗口形式的外观精美的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!