Esempio n. 1
0
        protected UIImage rotateImage(UIImage sourceImage, float rotate)
        {
            float   rads = (float)Math.PI * rotate / 180;
            UIImage returnImage;

            using (CGImage imageRef = sourceImage.CGImage)
            {
                UIView rotatedViewBox = new UIView(new CGRect(0, 0, imageRef.Width, imageRef.Height));

                CGAffineTransform t = CGAffineTransform.MakeIdentity();
                t.Rotate(rads);
                rotatedViewBox.Transform = t;
                CGSize rotatedSize = rotatedViewBox.Frame.Size;


                UIGraphics.BeginImageContextWithOptions(rotatedSize, false, sourceImage.CurrentScale);
                var context = UIGraphics.GetCurrentContext();
                context.TranslateCTM(rotatedSize.Width / 2, rotatedSize.Height / 2);
                context.RotateCTM(rads);
                context.ScaleCTM(1.0f, -1.0f);
                var rect = new CGRect(-sourceImage.Size.Width / 2, -sourceImage.Size.Height / 2, sourceImage.Size.Width, sourceImage.Size.Height);
                context.DrawImage(rect, sourceImage.CGImage);
                //context.FillRect(rect);

                returnImage = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();
            }

            return(returnImage);
        }
        /// <summary>
        /// Generates the image for button.
        /// </summary>
        /// <param name="rect">The rect.</param>
        /// <returns>UIImage.</returns>
        private UIImage GenerateImageForButton(CGRect rect)
        {
            UIGraphics.BeginImageContextWithOptions(rect.Size, false, 0);
            UIImage image;

            using (var context = UIGraphics.GetCurrentContext())
            {
                CGPoint p1, p2, p3;
                if (_arrowDirection == ArrowDirection.Left)
                {
                    p1 = new CGPoint(0, (rect.Height) / 2);
                    p2 = new CGPoint(rect.Width, 0);
                    p3 = new CGPoint(rect.Width, rect.Height);
                }
                else
                {
                    p1 = new CGPoint(rect.Width, rect.Height / 2);
                    p2 = new CGPoint(0, 0);
                    p3 = new CGPoint(0, rect.Height);
                }

                context.SetFillColor(UIColor.Clear.CGColor);
                context.FillRect(rect);
                context.SetFillColor(_color.CGColor);
                context.MoveTo(p1.X, p1.Y);
                context.AddLineToPoint(p2.X, p2.Y);
                context.AddLineToPoint(p3.X, p3.Y);
                context.FillPath();
                image = UIGraphics.GetImageFromCurrentImageContext();
            }

            UIGraphics.EndImageContext();
            return(image);
        }
        public static UIImage ConvertViewToImage(UIView view)
        {
            UIGraphics.BeginImageContextWithOptions(view.Bounds.Size, false, 0);
            view.Layer.RenderInContext(UIGraphics.GetCurrentContext());
            UIImage img = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            // Optimization: Let's try to reuse any of the last 10 images we generated
            var bytes = img.AsPNG().ToArray();
            var md5   = MD5.Create();
            var hash  = Convert.ToBase64String(md5.ComputeHash(bytes));

            var exists = cache.ContainsKey(hash);

            if (exists)
            {
                lruTracker.Remove(hash);
                lruTracker.AddLast(hash);
                return(cache[hash]);
            }
            if (lruTracker.Count > 10) // O(1)
            {
                cache.TryRemove(lruTracker.First.Value, out UIImage _);
                lruTracker.RemoveFirst();
            }
            cache.GetOrAdd(hash, img);
            lruTracker.AddLast(hash);
            return(img);
        }
Esempio n. 4
0
        public Task <UIImage> LoadImageAsync(
            ImageSource imagesource,
            CancellationToken cancelationToken = default(CancellationToken),
            float scale = 1f)
        {
            UIImage image      = null;
            var     fontsource = imagesource as FontImageSource;

            if (fontsource != null)
            {
                // This will allow lookup from the Embedded Fonts
                var font      = Font.OfSize(fontsource.FontFamily, fontsource.Size).ToUIFont(imagesource.RequireFontManager());
                var iconcolor = fontsource.Color ?? _defaultColor;
                var attString = new NSAttributedString(fontsource.Glyph, font: font, foregroundColor: iconcolor.ToUIColor());
                var imagesize = ((NSString)fontsource.Glyph).GetSizeUsingAttributes(attString.GetUIKitAttributes(0, out _));

                UIGraphics.BeginImageContextWithOptions(imagesize, false, 0f);
                var ctx          = new NSStringDrawingContext();
                var boundingRect = attString.GetBoundingRect(imagesize, (NSStringDrawingOptions)0, ctx);
                attString.DrawString(new RectangleF(
                                         imagesize.Width / 2 - boundingRect.Size.Width / 2,
                                         imagesize.Height / 2 - boundingRect.Size.Height / 2,
                                         imagesize.Width,
                                         imagesize.Height));
                image = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();

                if (image != null && iconcolor != _defaultColor)
                {
                    image = image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
                }
            }
            return(Task.FromResult(image));
        }
        public override UIImage GenerateBackgroundImage()
        {
            UIImage resultImage;

            UIGraphics.BeginImageContextWithOptions(BackBounds.Size, false, UIScreen.MainScreen.Scale);
            var center = new CGPoint(BackBounds.GetMidX(), BackBounds.GetMidY());

            using (var context = UIGraphics.GetCurrentContext())
                using (var circlePath = UIBezierPath.FromOval(new CGRect(CGPoint.Empty, BackBounds.Size)))
                    using (var borderBagelPath = BezierPathGenerator.Bagel(center, StartBorderRadius, EndBorderRadius, 0f, FullCircleAngle))
                        using (var innerBorderBagelPath = BezierPathGenerator.Bagel(center, StartBorderRadius + BorderPadding, EndBorderRadius - BorderPadding, 0f, FullCircleAngle)) {
                            context.SaveState();
                            context.SetFillColor(BackCircleBackgroundColor);
                            circlePath.Fill();
                            context.RestoreState();

                            context.SaveState();
                            context.SetFillColor(BackBorderColor);
                            borderBagelPath.Fill();
                            context.RestoreState();

                            context.SaveState();
                            context.SetFillColor(BackInnerBorderColor);
                            innerBorderBagelPath.Fill();
                            context.RestoreState();

                            resultImage = UIGraphics.GetImageFromCurrentImageContext();
                        }

            return(resultImage);
        }
Esempio n. 6
0
        public static UIImage DrawResults(UIImage img, MultiboxGraph.Result result, float scoreThreshold)
        {
            Rectangle[] locations = ScaleLocation(result.DecodedLocations, (int)img.Size.Width, (int)img.Size.Height);

            UIGraphics.BeginImageContextWithOptions(img.Size, false, 0);
            var context = UIGraphics.GetCurrentContext();

            img.Draw(new CGPoint());
            context.SetStrokeColor(UIColor.Red.CGColor);
            context.SetLineWidth(2);
            for (int i = 0; i < result.Scores.Length; i++)
            {
                if (result.Scores[i] > scoreThreshold)
                {
                    Rectangle rect   = locations[result.Indices[i]];
                    CGRect    cgRect = new CGRect(rect.X, rect.Y, rect.Width, rect.Height);
                    context.AddRect(cgRect);
                    context.DrawPath(CGPathDrawingMode.Stroke);
                }
            }
            UIImage imgWithRect = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();
            return(imgWithRect);
        }
Esempio n. 7
0
        public Task <UIImage> LoadImageAsync(ImageSource imagesource, CancellationToken cancelationToken = default, float scale = 1)
        {
            UIImage image = null;

            if (imagesource is IconImageSource iconsource && FontRegistry.HasFont(iconsource.Name, out var font))
            {
                // This will allow lookup from the Embedded Fonts
                var glyph        = font.GetGlyph(iconsource.Name);
                var cleansedname = FontExtensions.CleanseFontName(font.Alias);
                var uifont       = UIFont.FromName(cleansedname ?? string.Empty, (float)iconsource.Size) ??
                                   UIFont.SystemFontOfSize((float)iconsource.Size);
                var iconcolor = iconsource.Color.IsDefault ? _defaultColor : iconsource.Color;
                var attString = new NSAttributedString(glyph, font: uifont, foregroundColor: iconcolor.ToUIColor());
                var imagesize = ((NSString)glyph).GetSizeUsingAttributes(attString.GetUIKitAttributes(0, out _));

                UIGraphics.BeginImageContextWithOptions(imagesize, false, 0f);
                var ctx          = new NSStringDrawingContext();
                var boundingRect = attString.GetBoundingRect(imagesize, (NSStringDrawingOptions)0, ctx);
                attString.DrawString(new RectangleF(
                                         (imagesize.Width / 2) - (boundingRect.Size.Width / 2),
                                         (imagesize.Height / 2) - (boundingRect.Size.Height / 2),
                                         imagesize.Width,
                                         imagesize.Height));
                image = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();

                if (image != null && iconcolor != _defaultColor)
                {
                    image = image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
                }
            }
            return(Task.FromResult(image));
        }
Esempio n. 8
0
        private UIImage CropCircle(UIImage image)
        {
            // stretches the image if it's not square
            var newSize = image.Size;
            var minEdge = Math.Min(newSize.Height, newSize.Width);

            var size = new CGSize(width: minEdge, height: minEdge);

            UIGraphics.BeginImageContextWithOptions(size, false, 0.0f);
            var context = UIGraphics.GetCurrentContext();

            image.Draw(new CGRect(CGPoint.Empty, size), CGBlendMode.Copy, alpha: 1.0f);

            context.SetBlendMode(CGBlendMode.Copy);
            context.SetFillColor(UIColor.Clear.CGColor);

            var rect       = new CGRect(CGPoint.Empty, size: size);
            var rectPath   = UIBezierPath.FromRect(rect);
            var circlePath = UIBezierPath.FromOval(new CGRect(CGPoint.Empty, size: size));

            rectPath.AppendPath(circlePath);
            rectPath.UsesEvenOddFillRule = true;
            rectPath.Fill();

            var result = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            return(result);
        }
Esempio n. 9
0
        public void SetCustomView(Xamarin.Forms.View backgroundView, bool isForDestructive = false)
        {
            GetDefaultValuesIfNeeded();

            //Convert Xamarin.Forms.View to a Native UIView
            var defaultSize = new CGRect(0, 0, backgroundView.WidthRequest > 0 ? backgroundView.WidthRequest : 80, backgroundView.HeightRequest > 0 ? backgroundView.WidthRequest : 120);
            var renderer    = Platform.CreateRenderer(backgroundView);

            renderer.NativeView.Frame            = defaultSize;
            renderer.NativeView.AutoresizingMask = UIViewAutoresizing.All;
            renderer.NativeView.ContentMode      = UIViewContentMode.ScaleToFill;
            renderer.Element.Layout(defaultSize.ToRectangle());
            var nativeView = renderer.NativeView;

            nativeView.SetNeedsLayout();

            //Convert UIView into a UIImage
            UIGraphics.BeginImageContextWithOptions(nativeView.Bounds.Size, nativeView.Opaque, 0.0f);
            nativeView.Layer.RenderInContext(UIGraphics.GetCurrentContext());
            var nativeImage = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            SetNativeView(nativeImage, isForDestructive);
        }
Esempio n. 10
0
        public static UIImage ImageToOrientedMapIcon(string imagePath, double degrees, bool bigIcon = true)
        {
            var image = UIImage.FromFile(imagePath);

            var rect = new CGRect(0f, 0f, image.Size.Width, image.Size.Height);

            UIGraphics.BeginImageContextWithOptions(rect.Size, false, 0f);
            var context = UIGraphics.GetCurrentContext();

            // Step 1: translate/flip the graphics context (for transforming from CG* coords to UI* coords)
            context.TranslateCTM(image.Size.Width / 2, image.Size.Height / 2);

            // Step 2: Rotate the image context
            context.RotateCTM((nfloat)(((degrees > 180 ? (degrees - 180) : (degrees + 180)) * Math.PI / 180)));

            // Step 3: Now, draw the rotated/scaled image into the context
            context.DrawImage(new CGRect(-image.Size.Width / 2, -image.Size.Height / 2, image.Size.Width, image.Size.Height), image.CGImage);

            var resultImage = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            image = null;

            return(resultImage);
        }
        private static UIImage drawToken(string projectName, UIColor projectColor, UIFont font)
        {
            var stringToDraw = new NSAttributedString(
                projectName, new UIStringAttributes {
                Font = font, ForegroundColor = projectColor
            });

            var size = CalculateSize(stringToDraw, circleWidth);

            UIGraphics.BeginImageContextWithOptions(size, false, 0.0f);
            using (var context = UIGraphics.GetCurrentContext())
            {
                var tokenPath = CalculateTokenPath(size);
                context.AddPath(tokenPath.CGPath);
                context.SetFillColor(projectColor.ColorWithAlpha(0.12f).CGColor);
                context.FillPath();

                var dot = UIBezierPath.FromRoundedRect(new CGRect(
                                                           x: dotPadding + TokenMargin,
                                                           y: dotYOffset,
                                                           width: dotDiameter,
                                                           height: dotDiameter
                                                           ), dotRadius);
                context.AddPath(dot.CGPath);
                context.SetFillColor(projectColor.CGColor);
                context.FillPath();

                var textOffset = (TokenHeight - font.LineHeight) / 2;
                stringToDraw.DrawString(new CGPoint(TokenMargin + TokenPadding + circleWidth, textOffset));

                var image = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();
                return(image);
            }
        }
Esempio n. 12
0
        public static UIImage ApplyColorToImage(string imagePath, UIColor color)
        {
            var image = UIImage.FromFile(imagePath);

            var rect = new CGRect(0f, 0f, image.Size.Width, image.Size.Height);

            UIGraphics.BeginImageContextWithOptions(rect.Size, false, 0f);
            var context = UIGraphics.GetCurrentContext();

            image.Draw(rect);

            // translate/flip the graphics context (for transforming from CG* coords to UI* coords)
            context.TranslateCTM(0, image.Size.Height);
            context.ScaleCTM(1, -1);

            // apply clip to mask
            context.ClipToMask(rect, image.CGImage);

            context.SetFillColor(color.CGColor);
            context.SetBlendMode(CGBlendMode.Normal);

            context.FillRect(rect);

            // translate/flip the graphics context (for transforming from UI* coords back to CG* coords)
            context.TranslateCTM(0, image.Size.Height);
            context.ScaleCTM(1, -1);

            var resultImage = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            image = null;

            return(resultImage);
        }
Esempio n. 13
0
        private UIImage ChangeImageColor(UIImage image, nfloat alpha, UIColor color)
        {
            var alphaColor = color.ColorWithAlpha(alpha);

            UIGraphics.BeginImageContextWithOptions(image.Size, false, UIScreen.MainScreen.Scale);

            var context = UIGraphics.GetCurrentContext();

            alphaColor.SetFill();

            context.TranslateCTM(0, image.Size.Height);
            context.ScaleCTM(new nfloat(1.0), new nfloat(-1.0));
            context.SetBlendMode(CGBlendMode.Lighten);

            var rect = new CGRect(0, 0, image.Size.Width, image.Size.Height);

            context.DrawImage(rect, image.CGImage);

            context.SetBlendMode(CGBlendMode.SourceAtop);
            context.AddRect(rect);
            context.DrawPath(CGPathDrawingMode.Fill);

            image = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();

            return(image);
        }
        protected override UIImage Convert(UIImageView value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var coloredImg = new UIImage();

            try
            {
                UIColor color = (parameter as UIColor);

                UIGraphics.BeginImageContextWithOptions(value.Image.Size, false, 0);
                var context = UIGraphics.GetCurrentContext();

                color.SetFill();

                context.TranslateCTM(0, value.Image.Size.Height);
                context.ScaleCTM(1, -1);

                context.SetBlendMode(CGBlendMode.ColorBurn);
                var rect = new CGRect(0, 0, value.Image.Size.Width, value.Image.Size.Height);
                context.DrawImage(rect, value.Image.CGImage);

                context.SetBlendMode(CGBlendMode.SourceIn);
                context.AddRect(rect);
                context.DrawPath(CGPathDrawingMode.Fill);

                coloredImg = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();
            }
            catch {}

            return(coloredImg);
        }
Esempio n. 15
0
        public static UIImage CropByX(this UIImage image, nfloat x)
        {
            UIGraphics.BeginImageContextWithOptions(new CGSize(image.Size.Width - x, image.Size.Height), false, 0);

            UIImage result = null;

            using (CGContext context = UIGraphics.GetCurrentContext())
            {
                context.TranslateCTM(0, image.Size.Height);
                context.ScaleCTM(1, -1);

                context.DrawImage(new CGRect(CGPoint.Empty, image.Size), image.CGImage);

                using (CGImage img = context.AsBitmapContext().ToImage())
                {
                    result = new UIImage(img, image.CurrentScale, UIImageOrientation.Up);
                    img.Dispose();
                }

                context.Dispose();
                UIGraphics.EndImageContext();
            }

            return(result);
        }
Esempio n. 16
0
        private UIImage GetGradientImage(CGSize size)
        {
            var c1 = (Element as GradientLabel).TextColor1.ToCGColor();
            var c2 = (Element as GradientLabel).TextColor2.ToCGColor();

            UIGraphics.BeginImageContextWithOptions(size, false, 0);

            var context = UIGraphics.GetCurrentContext();

            if (context == null)
            {
                return(null);
            }

            context.SetFillColor(UIColor.Blue.CGColor);
            context.FillRect(new RectangleF(new PointF(0, 0), new SizeF((float)size.Width, (float)size.Height)));

            var left       = new CGPoint(0, 0);
            var right      = new CGPoint(size.Width, 0);
            var colorspace = CGColorSpace.CreateDeviceRGB();

            var gradient = new CGGradient(colorspace, new CGColor[] { c1, c2 }, new nfloat[] { 0f, 1f });

            context.DrawLinearGradient(gradient, left, right, CGGradientDrawingOptions.DrawsAfterEndLocation);

            var img = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();
            return(img);
        }
Esempio n. 17
0
        UIImage DrawCloseButtonImageWithColor(UIColor color)
        {
            UIGraphics.BeginImageContextWithOptions(new SizeF(BUTTON_WIDTH, BUTTON_WIDTH), false, 0.0f);

            var context = UIGraphics.GetCurrentContext();

            context.SetStrokeColor(color.CGColor);
            context.SetFillColor(color.CGColor);
            context.SetLineWidth(1.25f);

            var inset = 20.5f;

            context.MoveTo(inset, inset);
            context.AddLineToPoint(BUTTON_WIDTH - inset, BUTTON_WIDTH - inset);
            context.StrokePath();

            context.MoveTo(BUTTON_WIDTH - inset, inset);
            context.AddLineToPoint(inset, BUTTON_WIDTH - inset);
            context.StrokePath();

            var result = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            return(result);
        }
Esempio n. 18
0
        public Task <UIImage> LoadImageAsync(
            ImageSource imagesource,
            CancellationToken cancelationToken = default(CancellationToken),
            float scale = 1f)
        {
            UIImage image      = null;
            var     fontsource = imagesource as FontImageSource;

            if (fontsource != null)
            {
                var iconcolor = fontsource.Color != Color.Default ? fontsource.Color : Color.White;
                var imagesize = new SizeF((float)fontsource.Size, (float)fontsource.Size);
                var font      = UIFont.FromName(fontsource.FontFamily ?? string.Empty, (float)fontsource.Size) ??
                                UIFont.SystemFontOfSize((float)fontsource.Size);

                UIGraphics.BeginImageContextWithOptions(imagesize, false, 0f);
                var attString    = new NSAttributedString(fontsource.Glyph, font: font, foregroundColor: iconcolor.ToUIColor());
                var ctx          = new NSStringDrawingContext();
                var boundingRect = attString.GetBoundingRect(imagesize, (NSStringDrawingOptions)0, ctx);
                attString.DrawString(new RectangleF(
                                         imagesize.Width / 2 - boundingRect.Size.Width / 2,
                                         imagesize.Height / 2 - boundingRect.Size.Height / 2,
                                         imagesize.Width,
                                         imagesize.Height));
                image = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();

                if (iconcolor != Color.Default)
                {
                    image = image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
                }
            }
            return(Task.FromResult(image));
        }
Esempio n. 19
0
        public TokenTextAttachment(
            NSAttributedString tokenStringToDraw,
            nfloat textVerticalOffset,
            nfloat fontDescender,
            int leftMargin,
            int rightMargin)
        {
            var size = new CGSize(
                tokenStringToDraw.Size.Width + leftMargin + rightMargin + (tokenPadding * 2),
                lineHeight
                );

            UIGraphics.BeginImageContextWithOptions(size, false, 0.0f);
            using (var context = UIGraphics.GetCurrentContext())
            {
                var tokenPath = UIBezierPath.FromRoundedRect(new CGRect(
                                                                 x: leftMargin,
                                                                 y: tokenVerticallOffset,
                                                                 width: size.Width - leftMargin - rightMargin,
                                                                 height: tokenHeight
                                                                 ), tokenCornerRadius);
                context.AddPath(tokenPath.CGPath);
                context.SetStrokeColor(borderColor.CGColor);
                context.StrokePath();

                tokenStringToDraw.DrawString(new CGPoint(leftMargin + tokenPadding, textVerticalOffset));

                var image = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();
                Image = image;
            }

            FontDescender = fontDescender;
        }
Esempio n. 20
0
        private static UIImage CreateImage(string color)
        {
            try
            {
                var red   = color.Substring(0, 2);
                var green = color.Substring(2, 2);
                var blue  = color.Substring(4, 2);

                var redB   = System.Convert.ToByte(red, 16);
                var greenB = System.Convert.ToByte(green, 16);
                var blueB  = System.Convert.ToByte(blue, 16);

                var size    = new CGSize(28f, 28f);
                var cgColor = UIColor.FromRGB(redB, greenB, blueB).CGColor;

                UIGraphics.BeginImageContextWithOptions(size, false, 0);
                var ctx = UIGraphics.GetCurrentContext();
                ctx.SetLineWidth(1.0f);
                ctx.SetStrokeColor(cgColor);
                ctx.AddEllipseInRect(new CGRect(0, 0, size.Width, size.Height));
                ctx.SetFillColor(cgColor);
                ctx.FillPath();

                var image = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();
                return(image);
            }
            catch
            {
                return(null);
            }
        }
Esempio n. 21
0
        private UIImage CreatePieSegment(CGSize size, nfloat endAngle)
        {
            // Add the arc
            var arc = new CGPath();

            arc.MoveToPoint(size.Width / 2.0f, size.Height / 2.0f);
            arc.AddLineToPoint(size.Width / 2.0f, 0);
            arc.AddArc(size.Width / 2.0f, size.Height / 2.0f, size.Width / 2.0f, _startAngle, endAngle, false);
            arc.AddLineToPoint(size.Width / 2.0f, size.Height / 2.0f);

            // Stroke the arc
            UIGraphics.BeginImageContextWithOptions(size, false, 0);

            var context = UIGraphics.GetCurrentContext();

            context.AddPath(arc);
            context.SetFillColor(UIColor.FromRGBA(0f, 0f, 0f, 1f).CGColor);
            context.FillPath();

            // Get the mask image
            var image = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            return(image);
        }
Esempio n. 22
0
        private static UIImage RenderImage(CGPDFPage page)
        {
            var rect         = page.GetBoxRect(CGPDFBox.Crop);
            var pageRotation = page.RotationAngle;
            var size         = rect.Size;

            UIGraphics.BeginImageContextWithOptions(size, false, (nfloat)(1));

            var context = UIGraphics.GetCurrentContext();

            context.SaveState();
            context.TranslateCTM((nfloat)0.0, size.Height);
            context.ScaleCTM(One, (nfloat)(-1.0));

            context.SetFillColor(One, One);
            context.FillRect(rect);

            var transform = page.GetDrawingTransform(CGPDFBox.Crop, rect, 0, true);

            context.ConcatCTM(transform);
            context.DrawPDFPage(page);

            var image = UIGraphics.GetImageFromCurrentImageContext();

            context.RestoreState();
            UIGraphics.EndImageContext();

            return(image);
        }
        public static UIImage ToUIImageRounded(this UIColor color, float?cornerRadius = null)
        {
            if (cornerRadius == null || !cornerRadius.HasValue)
            {
                return(ToUIImage(color));
            }

            var scale = UIScreen.MainScreen.Scale;

            int width  = (int)(cornerRadius * scale) + (int)scale;
            int height = (int)(cornerRadius * scale);

            var view = new UIView().WithFrame(0, 0, width, height)
                       .WithBackground(color)
                       .WithTune(tune => tune.Layer.CornerRadius = cornerRadius.Value);

            UIImage result = null;

            UIGraphics.BeginImageContextWithOptions(view.Frame.Size, false, 0);
            {
                view.Layer.RenderInContext(UIGraphics.GetCurrentContext());
                result = UIGraphics.GetImageFromCurrentImageContext();
            }
            UIGraphics.EndImageContext();

            return(result.CreateResizableImage(new UIEdgeInsets(0, cornerRadius.Value, 0, cornerRadius.Value)));
        }
Esempio n. 24
0
        private UIImage GetImageInternal(CGSize scale, CGRect signatureBounds, CGSize imageSize, float strokeWidth, UIColor strokeColor, UIColor backgroundColor)
        {
            UIGraphics.BeginImageContextWithOptions(imageSize, false, InkPresenter.ScreenDensity);

            // create context and set the desired options
            var context = UIGraphics.GetCurrentContext();

            // background
            context.SetFillColor(backgroundColor.CGColor);
            context.FillRect(new CGRect(CGPoint.Empty, imageSize));

            // cropping / scaling
            context.ScaleCTM(scale.Width, scale.Height);
            context.TranslateCTM(-signatureBounds.Left, -signatureBounds.Top);

            // strokes
            context.SetStrokeColor(strokeColor.CGColor);
            context.SetLineWidth(strokeWidth);
            context.SetLineCap(CGLineCap.Round);
            context.SetLineJoin(CGLineJoin.Round);
            foreach (var path in inkPresenter.GetStrokes())
            {
                context.AddPath(path.Path.CGPath);
            }
            context.StrokePath();

            // get the image
            var image = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            return(image);
        }
Esempio n. 25
0
        protected override UIImage GenerateFullProgressImage()
        {
            UIImage resultImage;

            UIGraphics.BeginImageContextWithOptions(Bounds.Size, false, UIScreen.MainScreen.Scale);

            using (var context = UIGraphics.GetCurrentContext())
                using (var path = BezierPathGenerator.Bagel(CenterPoint, startRadius, endRadius, 0f, FullCircleAngle))
                {
                    context.SaveState();
                    context.SetFillColor(Colors.CGColor);
                    context.AddPath(path.CGPath);
                    context.FillPath();

                    context.RestoreState();

                    context.SaveState();
                    context.AddPath(path.CGPath);
                    context.Clip();

                    context.RestoreState();

                    resultImage = UIGraphics.GetImageFromCurrentImageContext();
                }

            return(resultImage);
        }
        public static UIImage WithAlpha(this UIImage image, nfloat alpha)
        {
            if (image == null)
            {
                return(null);
            }

            // Use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
            UIGraphics.BeginImageContextWithOptions(image.Size, false, 0);
            CGContext context = UIGraphics.GetCurrentContext();

            CGRect rect = new CGRect(0, 0, image.Size.Width, image.Size.Height);

            // Flip the context because UIKit coordinate system is upside down to Quartz coordinate system
            context.ScaleCTM(1, -1);
            context.TranslateCTM(0, -rect.Size.Height);

            context.SetBlendMode(CGBlendMode.Multiply);
            context.SetAlpha(alpha);

            context.DrawImage(rect, image.CGImage);

            UIImage imageFromCurrentImageContext = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            return(imageFromCurrentImageContext);
        }
Esempio n. 27
0
        public ProjectTextAttachment(NSAttributedString stringToDraw, UIColor projectColor, nfloat textVerticalOffset, nfloat fontDescender)
            : base(fontDescender)
        {
            var size = CalculateSize(stringToDraw, circleWidth);

            UIGraphics.BeginImageContextWithOptions(size, false, 0.0f);
            using (var context = UIGraphics.GetCurrentContext())
            {
                var tokenPath = CalculateTokenPath(size);
                context.AddPath(tokenPath.CGPath);
                context.SetFillColor(projectColor.ColorWithAlpha(0.12f).CGColor);
                context.FillPath();

                var dot = UIBezierPath.FromRoundedRect(new CGRect(
                                                           x: dotPadding + TokenMargin,
                                                           y: dotYOffset,
                                                           width: dotDiameter,
                                                           height: dotDiameter
                                                           ), dotRadius);
                context.AddPath(dot.CGPath);
                context.SetFillColor(projectColor.CGColor);
                context.FillPath();

                stringToDraw.DrawString(new CGPoint(circleWidth + TokenMargin + TokenPadding, textVerticalOffset));

                var image = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();
                Image = image;
            }
        }
Esempio n. 28
0
        public static UIImage GetBackgroundImage(this UIView control, Brush brush)
        {
            if (control == null || brush == null || brush.IsEmpty)
            {
                return(null);
            }

            var backgroundLayer = control.GetBackgroundLayer(brush);

            if (backgroundLayer == null)
            {
                return(null);
            }

            UIGraphics.BeginImageContextWithOptions(backgroundLayer.Bounds.Size, false, UIScreen.MainScreen.Scale);

            if (UIGraphics.GetCurrentContext() == null)
            {
                return(null);
            }

            backgroundLayer.RenderInContext(UIGraphics.GetCurrentContext());
            UIImage gradientImage = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            return(gradientImage);
        }
Esempio n. 29
0
        /// <summary>
        /// Changes the color of the image.
        /// </summary>
        /// <returns>The image color.</returns>
        /// <param name="img">Image.</param>
        /// <param name="color">Color.</param>
        public static UIImage ChangeImageColor(UIImage img, UIColor color)
        {
            if (color != null)
            {
                UIGraphics.BeginImageContextWithOptions(img.Size, false, (nfloat)img.CurrentScale);

                var context = UIGraphics.GetCurrentContext();

                context.TranslateCTM(0, img.Size.Height);
                context.ScaleCTM(1.0f, -1.0f);
                context.SetBlendMode(CGBlendMode.Normal);

                CGRect rect = new CGRect(0, 0, img.Size.Width, img.Size.Height);

                context.ClipToMask(rect, img.CGImage);


                color.SetFill();

                context.FillRect(rect);


                UIImage newImage = UIGraphics.GetImageFromCurrentImageContext();

                UIGraphics.EndImageContext();

                return(newImage);
            }


            return(img);
        }
Esempio n. 30
0
        public NSData ScreenshotImageData()
        {
            CGSize imageSize = new CGSize();
            UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;

            if (UIInterfaceOrientation.Portrait == orientation)
            {
                imageSize = UIScreen.MainScreen.Bounds.Size;
            }
            else
            {
                imageSize = new CGSize(UIScreen.MainScreen.Bounds.Size.Height, UIScreen.MainScreen.Bounds.Size.Width);
            }


            UIGraphics.BeginImageContextWithOptions(imageSize, false, 0);

            CGContext context = UIGraphics.GetCurrentContext();

            foreach (var window in UIApplication.SharedApplication.Windows)
            {
                context.SaveState();
                context.TranslateCTM(window.Center.X, window.Center.Y);
                context.ConcatCTM(window.Transform);
                context.TranslateCTM(-window.Bounds.Size.Width * window.Layer.AnchorPoint.X, -window.Bounds.Size.Height * window.Layer.AnchorPoint.Y);
                if (orientation == UIInterfaceOrientation.LandscapeLeft)
                {
                    context.RotateCTM((nfloat)1.57079632679489661923132169163975144);
                    context.TranslateCTM(0, -imageSize.Width);
                }
                else if (orientation == UIInterfaceOrientation.LandscapeRight)
                {
                    context.RotateCTM((nfloat)1.57079632679489661923132169163975144);
                    context.TranslateCTM(0, -imageSize.Height);
                }
                else if (orientation == UIInterfaceOrientation.PortraitUpsideDown)
                {
                    context.RotateCTM((nfloat)3.14159265358979323846264338327950288);
                    context.TranslateCTM(-imageSize.Width, -imageSize.Height);
                }

                if (window.RespondsToSelector(new ObjCRuntime.Selector("window.DrawViewHierarchy")))
                {
                    window.DrawViewHierarchy(window.Bounds, true);
                }
                else
                {
                    window.Layer.RenderInContext(context);
                }

                context.RestoreState();
            }
            UIImage image = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();
            // AsPNG() == UIImagePNGRepresentation(image);
            NSData imgData = image.AsPNG();

            return(imgData);
        }