添加抗锯齿 [英] Adding antialiasing

查看:137
本文介绍了添加抗锯齿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用抗锯齿,但我不知道为什么它不工作:

I am trying to using antialiasing but I don't why it isn't working:

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


推荐答案

首先,它应注意的是在图形对象没有的包含任意图形;这是一个的工具,让你画到相关的位图,包括控制表面。因此改变其任何属性的,如 SmoothingMode 影响显卡你再画,没有什么你绘制的之前 ..

First it should be noted that the Graphics object does not contain any graphics; it is a tool that lets you draw onto a related bitmap, including a control's surface. Therefore changing any of its properties, like the SmoothingMode only influences graphics you draw from then on, not anything you have drawn before..

圆肯定的的antialised像素如果你会绘制的 SmoothingMode 从默认设置为消除锯齿

The circle certainly would have antialised pixels if you would draw it after setting the SmoothingMode from its default None to AntiAlias.

该行的纵向,所以它不需要抗锯齿,除了在其两端,哪里有部分。但是,如果你倾斜它或将它移动到非整数位置抗锯齿将显示!

The Line is vertical, so it doesn't need antialiasing except at its ends, where there is some. But if you tilt it or move it to a non-integer position anti-aliasing will show!

让我们来修改你的代码一点点,并期待密切关注结果:

Let's modify your code a little and look closely at the result:

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

较小的圈子有很多灰色像素,甚至原来的绿线具有更轻的顶端。这两个新的生产线,现在完全抗锯齿,一是因为它是倾斜的,另外因为它位于像素之间

The smaller circle has many gray pixels and even the original green line has a lighter top end. The two new lines are fully anti-aliased now, one because it is tilted, the other because it sits 'between' pixels.

顺便说一句:如果是开启你将同时看防alising当你的 Pen.Width 均匀或当它是一个非整数号码。究其原因,后者应该是显而易见的;前者来自 PenAlignment 财产。它的默认中心试图居中笔,而不是在像素边界,但在坐标像素的中心。因此,只有一个不平宽度将完全填充像素,并不会引起反走样。对于关闭形状您可以通过修改 Pen.Alignment 改变这种行为,以插图

Btw: If it is turned on you will also see anti-alising when your Pen.Width is even or when it is a non-integer number. The reason for the latter should be obvious; the former comes from the PenAlignment property. Its default Center tries to center the pen, but not at the pixel boundary but at the center of the coordinate pixels. Therefore only an uneven width will completely fill the pixels and not cause anti-aliasing. For closed shapes you can change this behaviour by changing the Pen.Alignment to Inset:

这属性确定如何笔绘制封闭曲线和
多边形。该PenAlignment枚举指定五个值;
然而,只有两个值 - 中心和插图-会改变绘制线条的外观
。中心是此属性和
默认值指定笔的宽度在
曲线或多边形的轮廓居中。插图为这个属性值指定笔的
宽度为曲线或多边形的轮廓内。在
等三个值,右,左,和一开始,将导致
为中心的笔。

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.

一支笔,有它对齐设置为插图将产生不可靠的
的结果,有时在插入的位置,有时在
居中position.Also上,嵌入笔不能用来绘制复合
线画画不能画虚线与三角破折号帽

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 ,因为这永远只是导致的非持久性图形。相反,你需要使用油漆事件及其 e.Graphics 对象..

PS: The question was not about how to draw properly, so let me just note that you never ought to do it using control.CreateGraphics as this will always only result in non-persistent graphics. Instead you need to use the Paint event and its e.Graphics object..

这篇关于添加抗锯齿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆