Esempio n. 1
0
        public static unsafe void RectShouldHaveColor(this WriteableBitmap b, int x0, int y0, int x1, int y1, Color color)
        {
            var expectedColor     = WriteableBitmapExtensions.ConvertColor(color);
            var expectedColorName = GetColorName(color);

            using (var c = b.GetBitmapContext(ReadWriteMode.ReadOnly))
            {
                x1 = Clamp(x1, 0, c.Width - 1);
                y1 = Clamp(y1, 0, c.Height - 1);
                x0 = Clamp(x0, 0, x1);
                y0 = Clamp(y0, 0, y1);

                for (var y = y0; y < y1; y++)
                {
                    for (var x = x0; x < x1; x++)
                    {
                        var actualColor = c.Pixels[y * c.Width + x];
                        if (actualColor != expectedColor)
                        {
                            var actualColorName = GetColorName(b.GetPixel(x, y));
                            Assert.Fail($"Pixel at ({x},{y}) should be '{expectedColorName}' but is '{actualColorName}'");
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        //int intBorderColor;

        //int intFillColor;

        public WriteableBitmap ParseSqlGeometry(List <SqlGeometry> geometries, Func <Point, Point> transform, int width, int height, Color border, Color fill, ImageSource pointSymbol = null, sb.Primitives.Geometry <sb.Primitives.Point> symbol = null)
        {
            //int? intBorderColor = border.HasValue ? WriteableBitmapExtensions.ConvertColor(border.Value) : (int?)null;

            //int? intFillColor = fill.HasValue ? WriteableBitmapExtensions.ConvertColor(fill.Value) : (int?)null;
            int intBorderColor = WriteableBitmapExtensions.ConvertColor(border);

            int intFillColor = WriteableBitmapExtensions.ConvertColor(fill);

            WriteableBitmap result = new WriteableBitmap(width, height, 96, 96, PixelFormats.Pbgra32, null);

            if (geometries != null)
            {
                using (result.GetBitmapContext())
                {
                    foreach (SqlGeometry item in geometries)
                    {
                        AddGeometry(result, item, transform, intBorderColor, intFillColor, pointSymbol, symbol);
                    }
                }
            }

            //result.Freeze();

            return(result);
        }
Esempio n. 3
0
        public EdgeProgressBar()
        {
            this.Loaded += (s, e) =>
            {
                if (bScaleBackToPixels)
                {
                    Matrix         m            = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice;
                    ScaleTransform dpiTransform = new ScaleTransform(1 / m.M11, 1 / m.M22);
                    if (dpiTransform.CanFreeze)
                    {
                        dpiTransform.Freeze();
                    }
                    this.LayoutTransform = dpiTransform;
                }

                ProgressWidth = (int)this.ActualWidth - 20;
                bm            = BitmapFactory.New(ProgressWidth, 8);
                image.Source  = bm;
                image.Effect  = null;

                RenderGeometry();
            };

            InitializeComponent();
            backRectFill   = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#B4B4B4"));
            backRectStroke = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#ABABAB"));
            backEmpty      = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#FFFFFF"));
            r0             = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#4687E8"));
            l1             = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#79C0F9"));
            l2             = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#6DB3F7"));
            l3             = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#63A8F7"));
            l4             = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#589CF5"));
            lm1            = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#59A3F6"));
            lm2            = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#5095F6"));
        }
Esempio n. 4
0
            public static async Task FillBitmap(WriteableBitmap bmp, Func <float, float, Color> fillPixel)
            {
#if WINDOWS_PHONE
#else
                var stream = bmp.PixelBuffer.AsStream();
#endif
                int width  = bmp.PixelWidth;
                int height = bmp.PixelHeight;
                await Task.Run(() =>
                {
                    for (int y = 0; y < width; y++)
                    {
                        for (int x = 0; x < height; x++)
                        {
                            var color = fillPixel((float)x / width, (float)y / height);

#if WINDOWS_PHONE
                            bmp.Pixels[x + y *width] = WriteableBitmapExtensions.ConvertColor(color);
#else
                            WriteBGRA(stream, color);
#endif
                        }
                    }
                });

#if !WINDOWS_PHONE
                stream.Dispose();
#endif
                bmp.Invalidate();
            }
Esempio n. 5
0
        private int AddColors(int col1, int col2)
        {
            var c1 = GetColor(col1);
            var c2 = GetColor(col2);

            var result = Color.FromArgb((byte)(Math.Min(255, c1.A + c2.A)),
                                        (byte)(Math.Min(255, c1.R + c2.R)),
                                        (byte)(Math.Min(255, c1.G + c2.G)),
                                        (byte)(Math.Min(255, c1.B + c2.B)));

            return(WriteableBitmapExtensions.ConvertColor(result));
        }
        public void DrawLines(double[] vertices, DicomColor color)
        {
            var actualColor = WriteableBitmapExtensions.ConvertColor(DicomColorConverter.FromDicomColor(color));

            using (var context = _bitmap.GetBitmapContext())
            {
                for (int i = 0; i < vertices.Length; i += 4)
                {
                    WriteableBitmapExtensions.DrawLine(context, context.Width, context.Height, transxi(vertices[i]), transyi(vertices[i + 1]), transxi(vertices[i + 2]), transyi(vertices[i + 3]), actualColor);
                }
            }
        }
Esempio n. 7
0
 private void PreviewImage_OnLoaded(object sender, RoutedEventArgs e)
 {
     _bitmap = BitmapFactory.New(500, 500);
     for (var y = 0; y < 500; ++y)
     {
         for (var x1 = 0; x1 < 250; ++x1)
         {
             _bitmap.SetPixel(x1, y, WriteableBitmapExtensions.ConvertColor(Colors.DodgerBlue));
             _bitmap.SetPixel(x1 + 250, y, WriteableBitmapExtensions.ConvertColor(Colors.SeaGreen));
         }
     }
     _bitmap.FillEllipseCentered(225, 225, 50, 50, WriteableBitmapExtensions.ConvertColor(0.5, Colors.Red), true);
     //_bitmap.FillRectangle(200, 200, 250, 250, WriteableBitmapExtensions.ConvertColor(0.5, Colors.Red), true);
     PreviewImage.Source = _bitmap;
 }
Esempio n. 8
0
        public unsafe void MyDrawLine(int x1, int y1, int x2, int y2, Color col, bool addColors)
        {
            int color       = WriteableBitmapExtensions.ConvertColor(col);
            int pixelWidth  = Width;
            int pixelHeight = Height;
            // Get clip coordinates
            int clipX1 = 0;
            int clipX2 = pixelWidth;
            int clipY1 = 0;
            int clipY2 = pixelHeight;

            // Perform cohen-sutherland clipping if either point is out of the viewport
            //if (!MyCohenSutherlandLineClip(new Rect(clipX1, clipY1, clipX2 - clipX1, clipY2 - clipY1), ref x1, ref y1, ref x2, ref y2)) return;

            // Distance start and end point
            int dx = x2 - x1;
            int dy = y2 - y1;

            const int PRECISION_SHIFT = 8;

            // Determine slope (absolute value)
            int lenX, lenY;

            if (dy >= 0)
            {
                lenY = dy;
            }
            else
            {
                lenY = -dy;
            }

            if (dx >= 0)
            {
                lenX = dx;
            }
            else
            {
                lenX = -dx;
            }

            if (lenX > lenY)
            { // x increases by +/- 1
                if (dx < 0)
                {
                    int t = x1;
                    x1 = x2;
                    x2 = t;
                    t  = y1;
                    y1 = y2;
                    y2 = t;
                }

                // Init steps and start
                int incy = (dy << PRECISION_SHIFT) / dx;

                int y1s = y1 << PRECISION_SHIFT;
                int y2s = y2 << PRECISION_SHIFT;
                int hs  = pixelHeight << PRECISION_SHIFT;

                if (y1 < y2)
                {
                    if (y1 >= clipY2 || y2 < clipY1)
                    {
                        return;
                    }
                    if (y1s < 0)
                    {
                        if (incy == 0)
                        {
                            return;
                        }
                        int oldy1s = y1s;
                        // Find lowest y1s that is greater or equal than 0.
                        y1s = incy - 1 + ((y1s + 1) % incy);
                        x1 += (y1s - oldy1s) / incy;
                    }
                    if (y2s >= hs)
                    {
                        if (incy != 0)
                        {
                            // Find highest y2s that is less or equal than ws - 1.
                            // y2s = y1s + n * incy. Find n.
                            y2s = hs - 1 - (hs - 1 - y1s) % incy;
                            x2  = x1 + (y2s - y1s) / incy;
                        }
                    }
                }
                else
                {
                    if (y2 >= clipY2 || y1 < clipY1)
                    {
                        return;
                    }
                    if (y1s >= hs)
                    {
                        if (incy == 0)
                        {
                            return;
                        }
                        int oldy1s = y1s;
                        // Find highest y1s that is less or equal than ws - 1.
                        // y1s = oldy1s + n * incy. Find n.
                        y1s = hs - 1 + (incy - (hs - 1 - oldy1s) % incy);
                        x1 += (y1s - oldy1s) / incy;
                    }
                    if (y2s < 0)
                    {
                        if (incy != 0)
                        {
                            // Find lowest y2s that is greater or equal than 0.
                            // y2s = y1s + n * incy. Find n.
                            y2s = y1s % incy;
                            x2  = x1 + (y2s - y1s) / incy;
                        }
                    }
                }

                if (x1 < 0)
                {
                    y1s -= incy * x1;
                    x1   = 0;
                }
                if (x2 >= pixelWidth)
                {
                    x2 = pixelWidth - 1;
                }

                int ys = y1s;

                // Walk the line!
                int y         = ys >> PRECISION_SHIFT;
                int previousY = y;
                int index     = x1 + y * pixelWidth;
                int k         = incy < 0 ? 1 - pixelWidth : 1 + pixelWidth;
                for (int x = x1; x <= x2; ++x)
                {
                    SetPixel(index, addColors ? AddColors(GetPixel(index), color) : color);
                    ys += incy;
                    y   = ys >> PRECISION_SHIFT;
                    if (y != previousY)
                    {
                        previousY = y;
                        index    += k;
                    }
                    else
                    {
                        ++index;
                    }
                }
            }
            else
            {
                // Prevent division by zero
                if (lenY == 0)
                {
                    return;
                }
                if (dy < 0)
                {
                    int t = x1;
                    x1 = x2;
                    x2 = t;
                    t  = y1;
                    y1 = y2;
                    y2 = t;
                }

                // Init steps and start
                int x1s = x1 << PRECISION_SHIFT;
                int x2s = x2 << PRECISION_SHIFT;
                int ws  = pixelWidth << PRECISION_SHIFT;

                int incx = (dx << PRECISION_SHIFT) / dy;

                if (x1 < x2)
                {
                    if (x1 >= clipX2 || x2 < clipX1)
                    {
                        return;
                    }
                    if (x1s < 0)
                    {
                        if (incx == 0)
                        {
                            return;
                        }
                        int oldx1s = x1s;
                        // Find lowest x1s that is greater or equal than 0.
                        x1s = incx - 1 + ((x1s + 1) % incx);
                        y1 += (x1s - oldx1s) / incx;
                    }
                    if (x2s >= ws)
                    {
                        if (incx != 0)
                        {
                            // Find highest x2s that is less or equal than ws - 1.
                            // x2s = x1s + n * incx. Find n.
                            x2s = ws - 1 - (ws - 1 - x1s) % incx;
                            y2  = y1 + (x2s - x1s) / incx;
                        }
                    }
                }
                else
                {
                    if (x2 >= clipX2 || x1 < clipX1)
                    {
                        return;
                    }
                    if (x1s >= ws)
                    {
                        if (incx == 0)
                        {
                            return;
                        }
                        int oldx1s = x1s;
                        // Find highest x1s that is less or equal than ws - 1.
                        // x1s = oldx1s + n * incx. Find n.
                        x1s = ws - 1 + (incx - (ws - 1 - oldx1s) % incx);
                        y1 += (x1s - oldx1s) / incx;
                    }
                    if (x2s < 0)
                    {
                        if (incx != 0)
                        {
                            // Find lowest x2s that is greater or equal than 0.
                            // x2s = x1s + n * incx. Find n.
                            x2s = x1s % incx;
                            y2  = y1 + (x2s - x1s) / incx;
                        }
                    }
                }

                if (y1 < 0)
                {
                    x1s -= incx * y1;
                    y1   = 0;
                }
                if (y2 >= pixelHeight)
                {
                    y2 = pixelHeight - 1;
                }

                int index          = x1s;
                int indexBaseValue = y1 * pixelWidth;

                // Walk the line!
                var inc = (pixelWidth << PRECISION_SHIFT) + incx;
                for (int y = y1; y <= y2; ++y)
                {
                    var idx = indexBaseValue + (index >> PRECISION_SHIFT);
                    SetPixel(idx, addColors ? AddColors(GetPixel(idx), color) : color);
                    index += inc;
                }
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Draw an ellipse.
        /// </summary>
        /// <param name="x">The centre X.</param>
        /// <param name="y">The centre Y.</param>
        /// <param name="radius">The radius.</param>
        public void Circle(int x, int y, int radius)
        {
            var colour = WriteableBitmapExtensions.ConvertColor(Pen);

            _wb.DrawEllipseCenteredClipped(x, y, radius, radius, colour, out _);
        }