如何在WriteableBitmap + Writeablebitmap上绘制弧

时间:2014-08-19 21:05:35

标签: c# wpf antialiasing writablebitmap

我在WPF应用程序中借助WritableBitmapEx在WritableBitmap上绘制了很多形状。 不幸的是,没有准备好去绘制弧线的功能。

我该怎么做:

1。在WritableBitmap上画一个圆弧?

2。在WritableBitmap上绘制具有可变粗细的抗锯齿弧?

我只需要绘制circular arcs

有可能在Canvas上绘制一个具有可变厚度的漂亮的抗锯齿弧(System.Windows.Media.ArcSegment) - 但是有数千种形状,Canvas的性能很差 - 这就是我使用WritableBitmap的原因。

如果某些算法需要它,我已经计算了弧参数,如: CenterPoint,Radius,StartPoint,EndPoint,StartAngle,EndAngle,ArcLength,IsLarge或Direction

我试图用类似的代码手动绘制它:

int number_of_points = 1000;
for(int i=0; i<=number_of_points; i++){
    double progress=(double)i/number_of_points;
    double theta = (StartAngle + ArcLength * progress) * Math.PI / 180.0;
    draw_pixel(
        Center.X + Radius * Math.Cos(theta),
        Center.Y + Radius * Math.Sin(theta)
    );
}

但是随着图像分辨率的变化,弧的大小变化(如何计算最佳的number_of_points?),不同的弧形厚度和抗锯齿,它开始变得有点棘手。

1 个答案:

答案 0 :(得分:1)

1。在WritableBitmap上绘制一个圆弧?

在github上分析mono libgdiplus源后,我发现他们正在使用贝塞尔曲线绘制弧线。

我已将其部分功能移植到c#。 可以使用DrawArc扩展函数(借助WritableBitmapEx的DrawBezier)绘制一个简单的弧。 WritableBitmapEx中没有反锯齿版本的DrawBezier,所以此解决方案回答(仅)我的第一个问题

namespace System.Windows.Media.Imaging
{
    public static partial class WriteableBitmapArcExtensions
    {
        //port of mono libgdiplus function
        //append_arcs (GpPath *path, float x, float y, float width, float height, float startAngle, float sweepAngle)
        //from: https://github.com/mono/libgdiplus/blob/master/src/graphics-path.c
        public static void DrawArc(this WriteableBitmap bmp, float x, float y, float width, float height, float startAngle, float sweepAngle, Color color)
        {
            int i;
            float drawn = 0;
            int increment;
            float endAngle;
            bool enough = false;

            if (Math.Abs(sweepAngle) >= 360)
            {
                bmp.DrawEllipse((int)x, (int)y, (int)width, (int)height, color);
                return;
            }

            endAngle = startAngle + sweepAngle;
            increment = (endAngle < startAngle) ? -90 : 90;

            /* i is the number of sub-arcs drawn, each sub-arc can be at most 90 degrees.*/
            /* there can be no more then 4 subarcs, ie. 90 + 90 + 90 + (something less than 90) */
            for (i = 0; i < 4; i++)
            {
                float current = startAngle + drawn;
                float additional;

                if (enough)
                    return;

                additional = endAngle - current; /* otherwise, add the remainder */
                if (Math.Abs(additional) > 90)
                {
                    additional = increment;
                }
                else
                {
                    /* a near zero value will introduce bad artefact in the drawing */
                    if ((additional >= -0.0001f) && (additional <= 0.0001f))
                        return;
                    enough = true;
                }

                bmp._DrawArc(
                        x, y,
                        width, height, /* bounding rectangle */
                        current, current + additional, color);
                drawn += additional;
            }
        }

        //port of mono libgdiplus function
        //append_arc (GpPath *path, BOOL start, float x, float y, float width, float height, float startAngle, float endAngle)
        //from: https://github.com/mono/libgdiplus/blob/master/src/graphics-path.c
        private static void _DrawArc(this WriteableBitmap bmp, float x, float y, float width, float height, float startAngle, float endAngle, Color color)
        {
            double sin_alpha, sin_beta, cos_alpha, cos_beta;

            var rx = width / 2;
            var ry = height / 2;

            /* center */
            var cx = x + rx;
            var cy = y + ry;

            /* angles in radians */
            var alpha = startAngle * Math.PI / 180;
            var beta = endAngle * Math.PI / 180;

            /* adjust angles for ellipses */
            alpha = Math.Atan2(rx * Math.Sin(alpha), ry * Math.Cos(alpha));
            beta = Math.Atan2(rx * Math.Sin(beta), ry * Math.Cos(beta));

            if (Math.Abs(beta - alpha) > Math.PI)
            {
                if (beta > alpha)
                    beta -= 2 * Math.PI;
                else
                    alpha -= 2 * Math.PI;
            }

            var delta = beta - alpha;
            // http://www.stillhq.com/ctpfaq/2001/comp.text.pdf-faq-2001-04.txt (section 2.13)
            var bcp = 4.0 / 3 * (1 - Math.Cos(delta / 2)) / Math.Sin(delta / 2);

            sin_alpha = Math.Sin(alpha);
            sin_beta = Math.Sin(beta);
            cos_alpha = Math.Cos(alpha);
            cos_beta = Math.Cos(beta);

            /* starting point */
            double sx = cx + rx * cos_alpha;
            double sy = cy + ry * sin_alpha;

            //DrawBezier comes from WritableBitmapEx library
            bmp.DrawBezier(
                    (int)(sx),
                    (int)(sy),
                    (int)(cx + rx * (cos_alpha - bcp * sin_alpha)),
                    (int)(cy + ry * (sin_alpha + bcp * cos_alpha)),
                    (int)(cx + rx * (cos_beta + bcp * sin_beta)),
                    (int)(cy + ry * (sin_beta - bcp * cos_beta)),
                    (int)(cx + rx * cos_beta),
                    (int)(cy + ry * sin_beta),
                    color
            );
        }
    }
}

我在WritableBitmapEx网站上评论了一个问题:I would like to draw arcs - 所以也许这部分代码会包含在WritableBitmapEx库中。


2。在WritableBitmap上绘制具有可变粗细的抗锯齿弧?

在阅读了ForeverZer0的评论后,我使用System.Drawing.Graphics和WritableBitmap进行了一些实验。在getting a DrawingContext for a wpf WriteableBitmap的帮助下,我用这样的代码完成了它:

WritableBitmap ret = BitmapFactory.New(img_width, img_height);

ret.Lock();
var bmp = new System.Drawing.Bitmap(
    ret.PixelWidth,
    ret.PixelHeight,
    ret.BackBufferStride,
    System.Drawing.Imaging.PixelFormat.Format32bppPArgb,
    ret.BackBuffer
);

System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);

g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

g.DrawArc(...); //<-- draws an antialiased arc with variable thickness

g.Dispose();
bmp.Dispose();
ret.AddDirtyRect(new Int32Rect(0, 0, ret.PixelWidth, ret.PixelHeight));
ret.Unlock();

return ret; //<-- WritableBitmap with beautifull arc on it;