1
votes

I'm drawing a lot of shapes on WritableBitmap with help of WritableBitmapEx in my WPF application. Unfortunately there is no ready to go function to draw an arc on it.

How can I:

1. Draw an arc on WritableBitmap?

2. Draw an anti-aliased arc with variable thickness on WritableBitmap?

I just need to draw circular arcs.

There is possibility to draw a nice, anti-aliased arc with variable thickness (System.Windows.Media.ArcSegment) on Canvas - but with thousands of shapes the performance of Canvas is poor - that's why I'm using WritableBitmap.

If it would be needed by some algorithms I have already calculated arc parameters like: CenterPoint, Radius, StartPoint, EndPoint, StartAngle, EndAngle, ArcLength, IsLarge or Direction

I was trying to draw it manually with code similar to this:

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

but with varying resolution of picture, varying size of arc (how to calculate optimum number_of_points?), varying thickness of arc and with anti aliasing it starts to be a little tricky.

1
Have you considered converting to a System.Drawing.Bitmap, using System.Drawing.Graphics to perform the draw functions, which also has options for anti-aliasing, and then converting back? I imagine there is probably a more efficient way, but that would work.ForeverZer0
No, I haven't. Is there an easy way to draw an arc on System.Drawing.Bitmap ?vac
I can see that there is possibility of drawing arcs in System.Drawing.Graphics - thanks. I will take a look on performance issues System.Drawing.Bitmap vs WritableBitmap - I'm using WPF.vac
It should still be possible, even with WPF. A WritableBitmap inherits from BitmapSource, which can be saved to a stream, and then a Bitmap can be loaded from a stream. But yes, you will need to test the overhead with converting back and forth...ForeverZer0

1 Answers

1
votes

1. Draw an arc on WritableBitmap?

After analyzing mono libgdiplus sources on github I found that they are drawing an arc using Bezier curve.

I have ported some of their functions to c#. DrawArc extension function can be used (with help of DrawBezier from WritableBitmapEx) to draw an simple arc. There is no anti-aliased version of DrawBezier in WritableBitmapEx so this solution answers (only) my first question:

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

I have commented an issue on WritableBitmapEx site: I would like to draw arcs - so maybe part of this code would be included in WritableBitmapEx library.


2. Draw an anti-aliased arc with variable thickness on WritableBitmap?

After reading comment from ForeverZer0 I have made some experiments with System.Drawing.Graphics and WritableBitmap. With help of getting a DrawingContext for a wpf WriteableBitmap I have done it with such code:

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;