public static CVPixelBuffer ToCVPixelBuffer(this UIImage self)
        {
            var attrs = new CVPixelBufferAttributes();

            attrs.CGImageCompatibility         = true;
            attrs.CGBitmapContextCompatibility = true;

            var cgImg = self.CGImage;

            var pb = new CVPixelBuffer(cgImg.Width, cgImg.Height, CVPixelFormatType.CV32ARGB, attrs);

            pb.Lock(CVPixelBufferLock.None);
            var pData      = pb.BaseAddress;
            var colorSpace = CGColorSpace.CreateDeviceRGB();
            var ctxt       = new CGBitmapContext(pData, cgImg.Width, cgImg.Height, 8, pb.BytesPerRow, colorSpace, CGImageAlphaInfo.NoneSkipFirst);

            ctxt.TranslateCTM(0, cgImg.Height);
            ctxt.ScaleCTM(1.0f, -1.0f);
            UIGraphics.PushContext(ctxt);
            self.Draw(new CGRect(0, 0, cgImg.Width, cgImg.Height));
            UIGraphics.PopContext();
            pb.Unlock(CVPixelBufferLock.None);

            return(pb);
        }
Пример #2
0
        public static void DrawMarker(CGContext context, CGPoint position, MarkerStyle markerStyle)
        {
            markerStyle.Color.SetFill();

            var markerRect = new CGRect(new CGPoint(position.X + markerStyle.Offset.X - markerStyle.Size.Width / 2f, position.Y + markerStyle.Offset.Y - markerStyle.Size.Height / 2f), markerStyle.Size);

            UIGraphics.PushContext(context);

            UIBezierPath path = null;

            switch (markerStyle.Type)
            {
            case MarkerStyle.MarkerType.Oval:
                path = UIBezierPath.FromOval(markerRect);
                break;

            case MarkerStyle.MarkerType.Rect:
                path = UIBezierPath.FromRect(markerRect);
                break;
            }

            path.Fill();

            UIGraphics.PopContext();
        }
Пример #3
0
        public override void DrawMapRect(MKMapRect mapRect, float zoomScale, CGContext context)
        {
            UIGraphics.PushContext(context);

            context.SetLineWidth(4000);

            UIColor.Blue.SetFill();
            CGPath path = new CGPath();

            RectangleF r       = this.RectForMapRect(_overlay.BoundingMapRect());
            PointF     _origin = r.Location;

            path.AddLines(new PointF[] {
                _origin,
                new PointF(_origin.X + 35000, _origin.Y + 80000),
                new PointF(_origin.X - 50000, _origin.Y + 30000),
                new PointF(_origin.X + 50000, _origin.Y + 30000),
                new PointF(_origin.X - 35000, _origin.Y + 80000)
            });

            context.AddPath(path);
            context.DrawPath(CGPathDrawingMode.Fill);

            UIGraphics.PopContext();
        }
Пример #4
0
        public override void Draw(PdfDisplayBox box, CoreGraphics.CGContext context)
        {
            // Draw original content
            base.Draw(box, context);

            using (context)
            {
                // Draw watermark underlay
                UIGraphics.PushContext(context);
                context.SaveState();

                var pageBounds = this.GetBoundsForBox(box);
                context.TranslateCTM(0.0f, pageBounds.Size.Height);
                context.ScaleCTM(1.0f, -1.0f);
                context.RotateCTM((float)(Math.PI / 4.0f));


                Console.WriteLine($"{pageBounds}");

                var attributes = new UIStringAttributes()
                {
                    ForegroundColor = UIColor.FromRGBA(255, 0, 0, 125),
                    Font            = UIFont.BoldSystemFontOfSize(84)
                };

                var text = new NSAttributedString("WATERMARK", attributes);

                text.DrawString(new CGPoint(250, 40));

                context.RestoreState();
                UIGraphics.PopContext();
            }
        }
Пример #5
0
        public override void DrawInContext(CGContext ctx)
        {
            UIGraphics.PushContext(ctx);
            CGRect         bounds = this.Bounds;
            TKFill         fill   = this.LabelStyle.Fill;
            TKStroke       stroke = new TKStroke(UIColor.Black);
            TKBalloonShape shape  = new TKBalloonShape(TKBalloonShapeArrowPosition.Bottom, new CGSize(bounds.Size.Width - stroke.Width, bounds.Size.Height - stroke.Width));
            CGRect         textRect;

            if (this.IsOutsideBounds == true)
            {
                shape.ArrowPosition = TKBalloonShapeArrowPosition.Top;
                textRect            = new CGRect(bounds.Left, bounds.Top - this.LabelStyle.Insets.Top + shape.ArrowSize.Height, bounds.Size.Width, bounds.Size.Height + this.LabelStyle.Insets.Bottom);
            }
            else
            {
                textRect = new CGRect(bounds.Left, bounds.Top - this.LabelStyle.Insets.Top, bounds.Size.Width, bounds.Size.Height + this.LabelStyle.Insets.Bottom);
            }

            shape.DrawInContext(ctx, new CGPoint(bounds.GetMidX(), bounds.GetMidY()), new TKDrawing[] { fill, stroke });
            NSMutableParagraphStyle paragraphStyle = new NSMutableParagraphStyle();

            paragraphStyle.Alignment = this.LabelStyle.TextAlignment;
            NSDictionary attributes = new NSDictionary(UIStringAttributeKey.Font, UIFont.SystemFontOfSize(18),
                                                       UIStringAttributeKey.ForegroundColor, this.LabelStyle.TextColor,
                                                       UIStringAttributeKey.ParagraphStyle, paragraphStyle);

            NSString text = new NSString(this.Text);

            text.WeakDrawString(textRect, NSStringDrawingOptions.TruncatesLastVisibleLine | NSStringDrawingOptions.UsesLineFragmentOrigin, attributes, null);
            UIGraphics.PopContext();
        }
Пример #6
0
        private void DrawText(string t, nfloat x, nfloat y, nfloat fontSize, CGContext gctx, int alignement, UIColor color, nfloat width, int position = PosCenter)
        {
            gctx.SaveState();
            UIGraphics.PushContext(gctx);
            gctx.SetFillColor(color.CGColor);

            var tfont = UIFont.FromName(FontName, fontSize);

            var nsstr = new NSString(t);
            var sz    = nsstr.StringSize(tfont, width, UILineBreakMode.WordWrap);

            while ((int)(sz.Width - width) == 0)
            {
                var temp = sz.Height;
                sz = nsstr.StringSize(tfont, new CGSize(width, sz.Height * 2.5f), UILineBreakMode.WordWrap);
                if (temp == sz.Height)
                {
                    break;
                }
            }
            var mitad = sz.Height / 2;

            switch (position)
            {
            case PosTop:
                mitad = 0;
                break;

            case PosCenter:
                mitad = sz.Height / 2;
                break;

            case PosBottom:
                mitad = sz.Height;
                break;
            }
            CGRect rect;

            switch (alignement)
            {
            case TextCenter:
                rect = new CGRect(x - sz.Width / 2, y - mitad, sz.Width, sz.Height * 2.5f);
                nsstr.DrawString(rect, tfont, UILineBreakMode.WordWrap, UITextAlignment.Center);
                break;

            case TextLeft:
                rect = new CGRect(x, y - mitad, sz.Width, sz.Height);
                nsstr.DrawString(rect, tfont, UILineBreakMode.WordWrap, UITextAlignment.Left);
                break;

            case TextRight:
                rect = new CGRect(x - sz.Width, y - mitad, sz.Width, sz.Height);
                nsstr.DrawString(rect, tfont, UILineBreakMode.WordWrap, UITextAlignment.Right);
                break;
            }

            UIGraphics.PopContext();
            gctx.RestoreState();
        }
        public override void DrawInContext(CGContext ctx)
        {
            UIGraphics.PushContext(ctx);

            Draw(Bounds);

            UIGraphics.PopContext();
        }
Пример #8
0
        public void DrawImage(Image image, Rectangle source, Rectangle destination)
        {
            UIGraphics.PushContext(this.context);

            var handler = image.Handler as IImageHandler;

            handler.DrawImage(this, source, destination);
            UIGraphics.PopContext();
        }
Пример #9
0
        public void DrawImage(Image image, int x, int y, int width, int height)
        {
            UIGraphics.PushContext(this.context);

            var handler = image.Handler as IImageHandler;

            handler.DrawImage(this, x, y, width, height);
            UIGraphics.PopContext();
        }
Пример #10
0
        public SizeF MeasureString(Font font, string text)
        {
            UIGraphics.PushContext(this.context);
            var str  = new NSString(text);
            var size = str.StringSize(font.ControlObject as UIFont);

            UIGraphics.PopContext();
            return(new SizeF(size.Width, size.Height));
        }
Пример #11
0
 public void DrawLine(Color color, int startx, int starty, int endx, int endy)
 {
     UIGraphics.PushContext(this.context);
     context.SetStrokeColor(Generator.Convert(color));
     //context.SetShouldAntialias(false);
     context.SetLineCap(CGLineCap.Square);
     context.SetLineWidth(1.0F);
     context.StrokeLineSegments(new SD.PointF[] { TranslateView(new SD.PointF(startx, starty)), TranslateView(new SD.PointF(endx, endy)) });
     UIGraphics.PopContext();
 }
Пример #12
0
        void StartDrawing()
        {
#if OSX
            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext.CurrentContext = graphicsContext;
#elif IOS
            UIGraphics.PushContext(Control);
#endif
            Control.SaveState();
        }
Пример #13
0
        public void DrawIcon(Icon icon, int x, int y, int width, int height)
        {
            UIGraphics.PushContext(this.context);

            var nsimage  = icon.ControlObject as UIImage;
            var destRect = this.TranslateView(new SD.RectangleF(x, y, width, height), false);

            nsimage.Draw(destRect, CGBlendMode.Copy, 1);

            UIGraphics.PopContext();
        }
Пример #14
0
        public void DrawEllipse(Color color, int x, int y, int width, int height)
        {
            UIGraphics.PushContext(this.context);
            var rect = new System.Drawing.RectangleF(x, y, width, height);

            context.SetStrokeColor(Generator.Convert(color));
            //context.SetShouldAntialias(false);
            context.SetLineWidth(1.0F);
            context.StrokeEllipseInRect(TranslateView(rect));
            UIGraphics.PopContext();
        }
Пример #15
0
        public void DrawText(Font font, Color color, int x, int y, string text)
        {
            UIGraphics.PushContext(this.context);

            var str  = new NSString(text);
            var size = str.StringSize(font.ControlObject as UIFont);

            //context.SetShouldAntialias(true);
            str.DrawString(new SD.PointF(x, height - y - size.Height), font.ControlObject as UIFont);
            UIGraphics.PopContext();
        }
Пример #16
0
            public override void DrawInContext(CGContext ctx)
            {
                base.DrawInContext(ctx);

                ctx.SetLineWidth(1);
                ctx.SetStrokeColor(UIColor.Green.CGColor);
                ctx.AddLines(new[] { new CGPoint(0, Frame.Height * .5f), new CGPoint(Frame.Width, Frame.Height * .5f) });
                ctx.AddLines(new[] { new CGPoint(Frame.Width * .5f, Frame.Height), new CGPoint(Frame.Width * .5f, 0) });
                ctx.DrawPath(CGPathDrawingMode.Stroke);

                UIGraphics.PushContext(ctx);
                ctx.SetFillColor(UIColor.Black.CGColor);
                NSString number = new NSString(string.Format("{0}:{1}", columnIndex, rowIndex));

                number.DrawString(new CGPoint(Frame.Width * .5f - 18, Frame.Height * .5f - 8), UIFont.FromName("Arial", 12));
                UIGraphics.PopContext();
            }
Пример #17
0
        public static UIImage ResizeCanvas(this UIImage image, CGSize newSize)
        {
            UIGraphics.BeginImageContextWithOptions(newSize, false, 0f);

            var context = UIGraphics.GetCurrentContext();

            UIGraphics.PushContext(context);
            image.Draw(new CGPoint((newSize.Width - image.Size.Width) / 2, (newSize.Height - image.Size.Height) / 2));
            UIGraphics.PopContext();

            var resultImage = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            image = null;

            return(resultImage);
        }
Пример #18
0
        /// <summary>
        /// Adds a transparent padding around all edges of an image.
        /// </summary>
        /// <param name="image">The image to update.</param>
        /// <param name="padding">The amount of padding to add.</param>
        /// <returns>A new image containing the transparent padding.</returns>
        public static UIImage WithPadding(this UIImage image, nfloat padding)
        {
            nfloat newWidth  = image.Size.Width + (padding * UIScreen.MainScreen.Scale);
            nfloat newHeight = image.Size.Height + (padding * UIScreen.MainScreen.Scale);

            UIGraphics.BeginImageContextWithOptions(new CGSize(newWidth, newHeight), false, 0);
            CGContext context = UIGraphics.GetCurrentContext();

            UIGraphics.PushContext(context);
            image.Draw(new CGPoint(padding, padding));
            UIGraphics.PopContext();

            UIImage newImage = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            return(newImage);
        }
Пример #19
0
        public void FillEllipse(Color color, int x, int y, int width, int height)
        {
            /*	if (width == 1 || height == 1)
             * {
             *      DrawLine(color, x, y, x+width-1, y+height-1);
             *      return;
             * }*/

            UIGraphics.PushContext(this.context);
            //this.Control.CompositingOperation = NSComposite.SourceOver;
            //this.Control.ColorRenderingIntent = NSColorRenderingIntent.Default;
            //this.context.SetFillColorSpace(CGColorSpace.CreateCalibratedRGB(new float[] { 1.0F, 1.0, 1.0 }, new float[] { 0, 0, 0 }, new float[] { 1.0F, 1.0, 1.0 }, ));
            //this.context.SetStrokeColorSpace(CGColorSpace.CreateDeviceCMYK());
            //this.context.SetAlpha(1.0F);
            context.SetFillColor(Generator.Convert(color));
            //context.SetShouldAntialias(false);
            context.FillEllipseInRect(TranslateView(new SD.RectangleF(x, y, width, height)));
            UIGraphics.PopContext();
        }
Пример #20
0
        public override void Draw(CGRect rect)
        {
            base.Draw(rect);

            if (_drawing)
            {
                _needsDraw = true;
                return;
            }

            try
            {
                _drawing = true;

                using (var context = UIGraphics.GetCurrentContext())
                {
                    context.InterpolationQuality = CGInterpolationQuality.Medium;

                    context.TranslateCTM(this.Frame.Size.Width / 2f, this.Frame.Size.Height / 2f);

                    context.ScaleCTM(Scale, Scale);

                    context.TranslateCTM(-TouchPoint.X, -TouchPoint.Y + (ScaleAtTouchPoint ? 0 : this.Bounds.Size.Height / 2f));

                    if (ViewToMagnify != null)
                    {
                        UIGraphics.PushContext(context);
                        ViewToMagnify.DrawViewHierarchy(ViewToMagnify.Bounds, false);
                        UIGraphics.PopContext();
                    }
                }
            }
            finally
            {
                _drawing = false;

                if (_needsDraw)
                {
                    _needsDraw = false;
                    SetNeedsDisplay();
                }
            }
        }
Пример #21
0
        public static CVPixelBuffer ToPixelBuffer(this UIImage self)
        {
            var width = self.Size.Width;

            var height = self.Size.Height;

            var attrs = new CVPixelBufferAttributes();

            attrs.CGBitmapContextCompatibility = true;
            attrs.CGImageCompatibility         = true;

            var resultPixelBuffer = new CVPixelBuffer((int)(width),
                                                      (int)(height),
                                                      CVPixelFormatType.CV32ARGB,
                                                      attrs);

            resultPixelBuffer.Lock(CVPixelBufferLock.None);
            var pixelData = resultPixelBuffer.GetBaseAddress(0);

            var rgbColorSpace = CGColorSpace.CreateDeviceRGB();

            var context = new CGBitmapContext(data: pixelData,
                                              width: (int)(width),
                                              height: (int)(height),
                                              bitsPerComponent: 8,
                                              bytesPerRow: resultPixelBuffer.GetBytesPerRowOfPlane(0),
                                              colorSpace: rgbColorSpace,
                                              bitmapInfo: CGImageAlphaInfo.NoneSkipFirst);

            context.TranslateCTM(tx: 0, ty: height);
            context.ScaleCTM(sx: 1.0f, sy: -1.0f);

            UIGraphics.PushContext(context);

            self.Draw(new CGRect(x: 0, y: 0, width: width, height: height));

            UIGraphics.PopContext();

            resultPixelBuffer.Unlock(CVPixelBufferLock.None);

            return(resultPixelBuffer);
        }
        public void DrawTextWithUTF8Fonts(CGContext mBmpContext, string text, float x, float y, string font_family, double text_size)
        {
            /*
             * The problem is that CoreGraphics APIs for CGContext do not support rendering UTF8 code, and then ShowTextAtPoint() is limited to the text encoding in MacRoman.
             * The fix is to use the UIKit function DrawString() instead.
             */

            UIGraphics.PushContext(mBmpContext);
            mBmpContext.SetRGBFillColor(1f, 1f, 1f, 1f);

            UIFont tfont = UIFont.FromName(font_family, (float)text_size);

            NSString   nsstr = new NSString(text);
            SizeF      sz    = nsstr.StringSize(tfont);
            RectangleF rect  = new RectangleF(x, y, sz.Width, sz.Height);

            nsstr.DrawString(rect, tfont);


            UIGraphics.PopContext();
        }
Пример #23
0
        public static void DrawText(CGContext context, string text, CGPoint position, TextStyle textStyle)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            UIGraphics.PushContext(context);

            textStyle.Color.SetColor();

            var textRect = new CGRect(new CGPoint(), text.StringSize(textStyle.Font));

            position = position.Add(textStyle.Offset);

            textRect = textRect.LocateAtPosition(position, textStyle.Position);

            text.DrawString(textRect, textStyle.Font);

            UIGraphics.PopContext();
        }
Пример #24
0
        CGBitmapContext CreateTextBitmapContext(string str, out byte[] bitmapData)
        {
            NSString text   = new NSString(str);
            UIFont   font   = UIFont.FromName("HelveticaNeue-Light", 128);
            SizeF    size   = text.StringSize(font);
            int      width  = (int)size.Width;
            int      height = (int)size.Height;

            bitmapData = new byte[256 * 256 * 4];
            CGBitmapContext bitmapContext = new CGBitmapContext(bitmapData, 256, 256, 8, 256 * 4, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedLast);

            //Console.WriteLine ("bitmap context size: {0} x {1}", bitmapContext.Width, bitmapContext.Height);
            UIGraphics.PushContext(bitmapContext);
            float grayLevel = str == " " ? .8f : 1;

            bitmapContext.SetRGBFillColor(grayLevel, grayLevel, grayLevel, 1);
            bitmapContext.FillRect(new RectangleF(0, 0, 256.0f, 256.0f));
            bitmapContext.SetRGBFillColor(0, 0, 0, 1);

            text.DrawString(new PointF((256.0f - width) / 2.0f, (256.0f - height) / 2.0f + font.Descender), font);
            UIGraphics.PopContext();

            return(bitmapContext);
        }
Пример #25
0
        public override void DrawInContext(CGContext ctx)
        {
            base.DrawInContext(ctx);

            UIGraphics.PushContext(ctx);

            ////// Addition Drawing
            ctx.TranslateCTM(StartPoint.X, StartPoint.Y);
            ctx.ScaleCTM(ScaleFactor.Height, ScaleFactor.Width);

            //ctx.SetLineWidth(1.0f);
            //ctx.SetStrokeColor(FillColor.CGColor);
            this.FillColor.SetFill();

            if (this._drawingDelegate != null)
            {
                this._drawingDelegate.Invoke(ctx);
            }
            else
            {
                this.BackgroundColor = UIColor.Yellow.CGColor;
            }
            UIGraphics.PopContext();
        }
        internal override pTexture CreateText(string text, float size, OpenTK.Vector2 restrictBounds, OpenTK.Graphics.Color4 Color4, bool shadow, bool bold, bool underline, TextAlignment alignment, bool forceAa, out OpenTK.Vector2 measured, OpenTK.Graphics.Color4 background, OpenTK.Graphics.Color4 border, int borderWidth, bool measureOnly, string fontFace)
        {
            //UIFont font = bold ? UIFont.FromName("GillSans-Bold", size) : UIFont.FromName("GillSans",size);
            //UIFont font = bold ? UIFont.BoldSystemFontOfSize(size) : UIFont.SystemFontOfSize(size);
            UIFont font = UIFont.FromName(bold ? "Futura-CondensedExtraBold" : "Futura-Medium",size);

            CGSize actualSize = CGSize.Empty;

            // Render the text to a UILabel to calculate sizing
            // and line-wrapping, and then copy the pixels to our texture buffer.
            UILabel textLabel = new UILabel();
            textLabel.Font = font;
            textLabel.BackgroundColor = UIColor.Clear;
            textLabel.TextColor = UIColor.White;
            textLabel.LineBreakMode = UILineBreakMode.WordWrap;
            textLabel.Lines = 0; // Needed for multiple lines
            textLabel.Text = text;

            textLabel.TextAlignment = UITextAlignment.Left;
            switch (alignment) {
            case TextAlignment.Centre:
                textLabel.TextAlignment = UITextAlignment.Center;
                break;
            case TextAlignment.Right:
                textLabel.TextAlignment = UITextAlignment.Right;
                break;
            }

            if (restrictBounds == Vector2.Zero)
            {
                textLabel.SizeToFit();
                actualSize = textLabel.Frame.Size;

                restrictBounds = new Vector2((float)actualSize.Width, (float)actualSize.Height);
            }
            else if (restrictBounds.Y == 0)
            {
                SizeF boundsSize = new SizeF (restrictBounds.X, GameBase.NativeSize.Height);
                actualSize = textLabel.SizeThatFits(boundsSize);
                textLabel.Frame = new CGRect(CGPoint.Empty, actualSize);

                restrictBounds = new Vector2((float)actualSize.Width, (float)actualSize.Height);
            }

            int width = TextureGl.GetPotDimension((int)restrictBounds.X);
            int height = TextureGl.GetPotDimension((int)restrictBounds.Y);

            IntPtr data = Marshal.AllocHGlobal(width * height);
            unsafe {
                byte* bytes = (byte*)data;
                for (int i = width * height - 1; i >= 0; i--) bytes[i] = 0;
            }

            using (CGColorSpace colorSpace = CGColorSpace.CreateDeviceGray())
            using (CGBitmapContext context = new CGBitmapContext(data, width, height, 8, width, colorSpace,CGImageAlphaInfo.None))
            {
                context.TranslateCTM(0, height);
                context.ScaleCTM(1, -1);

                UIGraphics.PushContext(context);

                textLabel.SetNeedsDisplay();
                textLabel.Layer.DrawInContext (context);
                
                UIGraphics.PopContext();

                measured = new Vector2((float)actualSize.Width, (float)actualSize.Height);

    			SpriteManager.TexturesEnabled = true;

                TextureGl gl = new TextureGl(width, height);
                gl.SetData(data, 0, All.Alpha);

                Marshal.FreeHGlobal(data);

                return new pTexture(gl, (int)actualSize.Width, (int)actualSize.Height);
            }
        }
Пример #27
0
        public override void Draw(CGRect rect)
        {
            var boundsRect = Bounds;

            boundsRect.Height = drawHeight;
            if (SizeF.Equals(boundsRect.Size, lastBoundsSize))
            {
                RegenerateImages();
                lastBoundsSize = boundsRect.Size;
            }

            nfloat width       = boundsRect.Size.Width;
            nfloat drawPercent = percent;

            if (((width - knobWidth) * drawPercent) < 3)
            {
                drawPercent = 0.0f;
            }
            if (((width - knobWidth) * drawPercent) > (width - knobWidth - 3))
            {
                drawPercent = 1.0f;
            }

            if (endDate != DateTime.MinValue)
            {
                var interval = (endDate - DateTime.Now).TotalSeconds;
                if (interval < 0.0)
                {
                    endDate = DateTime.MinValue;
                }
                else
                {
                    if (percent == 1f)
                    {
                        drawPercent = (float)Math.Cos((interval / animationDuration) * (Math.PI / 2.0));
                    }
                    else
                    {
                        drawPercent = 1.0f - (float)Math.Cos((interval / animationDuration) * (Math.PI / 2.0));
                    }
                    PerformSelector(new Selector("setNeedsDisplay"), null, 0.0);
                }
            }

            CGContext context = UIGraphics.GetCurrentContext();

            context.SaveState();
            UIGraphics.PushContext(context);

            if (drawPercent != 1.0)
            {
                var sliderOffRect = boundsRect;
                sliderOffRect.Height = sliderOff.Size.Height;
                sliderOff.Draw(sliderOffRect);
            }


            if (drawPercent > 0.0f && drawPercent < 1.0f)
            {
                nfloat  onWidth          = knobWidth / 2 + ((width - knobWidth / 2) - knobWidth / 2) * drawPercent;
                var     sourceRect       = new CGRect(0, 0, onWidth * scale, sliderOn.Size.Height * scale);
                var     drawOnRect       = new CGRect(0, 0, onWidth, sliderOn.Size.Height);
                CGImage sliderOnSubImage = sliderOn.CGImage.WithImageInRect(sourceRect);
                context.SaveState();
                context.ScaleCTM(1, -1);
                context.TranslateCTM(0, -drawOnRect.Height);
                context.DrawImage(drawOnRect, sliderOnSubImage);
                context.RestoreState();
            }

            if (drawPercent == 1.0)
            {
                nfloat  onWidth          = sliderOn.Size.Width;
                var     sourceRect       = new CGRect(0, 0, onWidth * scale, sliderOn.Size.Height * scale);
                var     drawOnRect       = new CGRect(0, 0, onWidth, sliderOn.Size.Height);
                CGImage sliderOnSubImage = sliderOn.CGImage.WithImageInRect(sourceRect);
                context.SaveState();
                context.ScaleCTM(1, -1);
                context.TranslateCTM(0, -drawOnRect.Height);
                context.DrawImage(drawOnRect, sliderOnSubImage);
                context.RestoreState();
            }


            context.SaveState();
            UIGraphics.PushContext(context);
            var insetClipRect = boundsRect.Inset(4, 4);

            UIGraphics.RectClip(insetClipRect);
            DrawUnderlayersInRect(rect, drawPercent * (boundsRect.Width - knobWidth), boundsRect.Width - knobWidth);
            UIGraphics.PopContext();
            context.RestoreState();

            context.ScaleCTM(1, -1);
            context.TranslateCTM(0, -boundsRect.Height);
            CGPoint location    = boundsRect.Location;
            UIImage imageToDraw = knobImage;

            if (this.Highlighted)
            {
                imageToDraw = knobImageOff;
            }

            nfloat xlocation;

            if (drawPercent == 0.0f)
            {
                xlocation = location.X + 3 + (nfloat)Math.Round(drawPercent * (boundsRect.Width - knobWidth + 2));
            }
            else
            {
                xlocation = location.X - 5 + (nfloat)Math.Round(drawPercent * (boundsRect.Width - knobWidth + 2));
                xlocation = xlocation < 0.0f ? 0.0f : xlocation;
            }

            var rectToDraw = new CGRect(xlocation, location.Y + 9.5f, knobWidth, knobImage.Size.Height);

            context.DrawImage(rectToDraw, imageToDraw.CGImage);

            UIGraphics.PopContext();
            context.RestoreState();
        }