gpt4 book ai didi

c# - 添加抗锯齿

转载 作者:太空宇宙 更新时间:2023-11-03 21:13:30 25 4
gpt4 key购买 nike

我正在尝试使用抗锯齿功能,但我不知道为什么它不起作用:

Pen pen = new Pen(Color.Black, 3);
Pen r = new Pen(Color.YellowGreen, 3);
Graphics b = panel2.CreateGraphics();
b.DrawEllipse(pen, 6, 0, 90, 90);
b.SmoothingMode = SmoothingMode.AntiAlias;
b.DrawLine(r, new Point(50, 90), new Point(50, 0));

最佳答案

首先应该注意的是Graphics对象不包含任何图形;它是一个工具,可让您在相关位图(包括控件表面)上绘图。因此,更改它的任何属性,例如 SmoothingMode会影响您从那时起绘制的图形,而不会影响您之前绘制的任何图形/strong>..

圆圈肯定具有抗锯齿像素如果您在之后SmoothingMode设置为默认值NoneAntiAlias

这条线是垂直的,所以它不需要抗锯齿,除了它的末端,那里一些。但是,如果您倾斜它或将其移动到非整数位置,抗锯齿就会显示出来!

让我们稍微修改一下您的代码并仔细查看结果:

Pen pen = new Pen(Color.Black, 3);
Pen r = new Pen(Color.YellowGreen, 3);
Graphics b = panel2.CreateGraphics();
b.DrawEllipse(pen, 6, 6, 90, 90);
b.SmoothingMode = SmoothingMode.AntiAlias;
b.DrawLine(r, new Point(50, 90), new Point(50, 0));

b.DrawLine(r, new Point(60, 90), new Point(70, 0));
b.DrawLine(r, new PointF(40.5f, 90), new PointF(40.5f, 0));
b.DrawEllipse(pen, 6, 6, 30, 30);

enter image description here enter image description here

较小的圆圈有很多灰色像素,即使是原来的绿线也有较浅的顶端。这两条新线现在完全消除了锯齿,一条是因为它是倾斜的,另一条是因为它位于“像素之间”。

顺便说一句:如果打开它,当您的 Pen.Width偶数 或当它是非整数 数字。后者的原因应该很明显;前者来自 PenAlignment属性(property)。它的默认 Center 尝试将笔居中,但不是在像素边界而是在坐标像素的中心。因此只有不均匀宽度才会完全填满像素并且不会导致抗锯齿。对于闭合形状,您可以通过将Pen.Alignment 更改为Inset 来更改此行为:

This property determines how the Pen draws closed curves and polygons. The PenAlignment enumeration specifies five values; however, only two values—Center and Inset—will change the appearance of a drawn line. Center is the default value for this property and specifies that the width of the pen is centered on the outline of the curve or polygon. A value of Inset for this property specifies that the width of the pen is inside the outline of the curve or polygon. The other three values, Right, Left, and Outset, will result in a pen that is centered.

A Pen that has its alignment set to Inset will yield unreliable results, sometimes drawing in the inset position and sometimes in the centered position.Also, an inset pen cannot be used to draw compound lines and cannot draw dashed lines with Triangle dash caps.

PS:问题不是关于如何绘制正确,所以让我注意一下,您永远不应该使用control.CreateGraphics 因为这将始终只会产生非持久性 图形。相反,您需要使用 Paint 事件及其 e.Graphics 对象..

关于c# - 添加抗锯齿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36094055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com