1

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

我怎样才能:

1、在WritableBitmap上画弧线?

2、在WritableBitmap上画一条可变粗细的抗锯齿弧?

我只需要画圆弧

有可能在 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?),弧的厚度和抗锯齿的变化,它开始有点棘手。

4

1 回答 1

1

1、在WritableBitmap上画弧线?

在分析了 github 上的 mono libgdiplus 源代码后,我发现他们正在使用 Bezier 曲线绘制弧线。

我已经将他们的一些功能移植到了 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 网站上评论了一个问题:我想绘制弧线- 所以可能部分代码将包含在 WritableBitmapEx 库中。


2、在WritableBitmap上画一条可变粗细的抗锯齿弧?

在阅读了 ForeverZer0 的评论后,我对 System.Drawing.Graphics 和 WritableBitmap 做了一些实验。在为 wpf WriteableBitmap 获取 DrawingContext 的帮助下,我用这样的代码完成了它:

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;
于 2014-08-31T18:24:21.193 回答