Ejemplo n.º 1
0
        public void ResetTransform()
        {
            // Since there is no context.SetCTM, only ConcatCTM
            // get the current transform, invert it, and concat this to
            // obtain the identity.   Then we concatenate the value passed
            var transform = context.GetCTM();

            transform.Invert();
            context.ConcatCTM(transform);
        }
Ejemplo n.º 2
0
        // test draw pattern
        protected void DrawTexture(CGContext context)
        {
            var destRect = new CGRect(0, 0, textureImage.Width, textureImage.Height);

            context.DrawImage(destRect, textureImage.NativeCGImage);

            if (wrapMode == WrapMode.TileFlipX)
            {
                context.ConcatCTM(CGAffineTransform.MakeScale(-1, 1));
                context.DrawImage(destRect, textureImage.NativeCGImage);
            }

            if (wrapMode == WrapMode.TileFlipY)
            {
                var transformY = new CGAffineTransform(1, 0, 0, -1,
                                                       textureImage.Width,
                                                       textureImage.Height);
                context.ConcatCTM(transformY);
                context.DrawImage(destRect, textureImage.NativeCGImage);
            }


            if (wrapMode == WrapMode.TileFlipXY)
            {
                // draw the original
                var transform = new CGAffineTransform(1, 0, 0, 1,
                                                      0, textureImage.Height);
                context.ConcatCTM(transform);
                context.DrawImage(destRect, textureImage.NativeCGImage);

                // reset the transform
                context.ConcatCTM(context.GetCTM().Invert());

                // draw next to original one that is flipped by x axis
                transform = new CGAffineTransform(-1, 0, 0, 1,
                                                  textureImage.Width * 2, textureImage.Height);
                context.ConcatCTM(transform);
                context.DrawImage(destRect, textureImage.NativeCGImage);


                // reset the transform
                context.ConcatCTM(context.GetCTM().Invert());

                // draw one that is flipped by Y axis under the oricinal
                transform = new CGAffineTransform(1, 0, 0, -1,
                                                  0, textureImage.Height);
                context.ConcatCTM(transform);
                context.DrawImage(destRect, textureImage.NativeCGImage);

                // draw the last one of the quadrant which is flipped by both the y and x axis
                context.ConcatCTM(context.GetCTM().Invert());
                transform = new CGAffineTransform(-1, 0, 0, -1,
                                                  textureImage.Width * 2, textureImage.Height);
                context.ConcatCTM(transform);
                context.DrawImage(destRect, textureImage.NativeCGImage);
            }
        }
Ejemplo n.º 3
0
        public void FillPath(Color color, GraphicsPath path)
        {
            StartDrawing();

            if (!Flipped)
            {
                context.ConcatCTM(new CGAffineTransform(1, 0, 0, -1, 0, ViewHeight));
            }
            context.BeginPath();
            context.AddPath(path.ControlObject as CGPath);
            context.ClosePath();
            context.SetFillColor(Generator.Convert(color));
            context.FillPath();
            EndDrawing();
        }
Ejemplo n.º 4
0
        static UIImage imageWithPDFPage(CGPDFPage page, float scale, CGAffineTransform t)
        {
            if (page == null)
            {
                return(null);
            }

            RectangleF box = page.GetBoxRect(CGPDFBox.Crop);

            t.Scale(scale, scale);
            box = new RectangleF(box.Location, new SizeF(box.Size.Width * scale, box.Size.Height * scale));

            var          pixelWidth = box.Size.Width;
            CGColorSpace cs         = CGColorSpace.CreateDeviceRGB();
            //DebugAssert( cs ) ;
            var _buffer = Marshal.AllocHGlobal((int)(box.Width * box.Height));

            UIGraphics.BeginImageContext(box.Size);
            CGContext c = UIGraphics.GetCurrentContext();

            c.SaveState();
            c.TranslateCTM(0f, box.Height);
            c.ScaleCTM(1f, -1f);
            cs.Dispose();
            c.ConcatCTM(t);
            c.DrawPDFPage(page);

            c.RestoreState();
            var image = UIGraphics.GetImageFromCurrentImageContext();

            return(image);
        }
Ejemplo n.º 5
0
        // rect changes depending on if the whole view is being redrawn, or just a section
        public override void Draw(RectangleF rect)
        {
            Console.WriteLine("Draw() Called");
            base.Draw(rect);

            // get a reference to the context
            using (CGContext context = UIGraphics.GetCurrentContext()) {
                // convert to View space
                CGAffineTransform affineTransform = CGAffineTransform.MakeIdentity();
                // invert the y axis
                affineTransform.Scale(1, -1);
                // move the y axis up
                affineTransform.Translate(0, Frame.Height);
                context.ConcatCTM(affineTransform);

                // draw our coordinates for reference
                DrawCoordinateSpace(context);

                // draw our flag
                DrawFlag(context);

                // add a label
                DrawCenteredTextAtPoint(context, 384, 700, "Stars and Stripes", 60);
            }
        }
Ejemplo n.º 6
0
            void DrawPattern(CGContext context)
            {
                var destRect = new sd.RectangleF(0, 0, image.Width, image.Height);

                context.ConcatCTM(new CGAffineTransform(1, 0, 0, -1, 0, image.Height));
                context.DrawImage(destRect, image);
            }
Ejemplo n.º 7
0
        public static void FlipCoordinateSystem(CGContext context)
        {
            // flip the coordinate system once for all
            var flipVertical = new CGAffineTransform(xx: 1, yx: 0, xy: 0, yy: -1, x0: 0, y0: context.GetClipBoundingBox().Height);

            context.ConcatCTM(flipVertical);
        }
Ejemplo n.º 8
0
 public void Transform(Transform transform)
 {
     context.ConcatCTM(new CGAffineTransform(
                           (nfloat)transform.A, (nfloat)transform.B,
                           (nfloat)transform.C, (nfloat)transform.D,
                           (nfloat)transform.E, (nfloat)transform.F));
 }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
        //========================================================================

        #endregion
        //========================================================================

        /// <summary>
        /// rect changes depending on if the whole view is being redrawn, or just a section
        /// </summary>
        /// <param name="rect">
        /// A <see cref="RectangleF"/>
        /// </param>
        public override void Draw(RectangleF rect)
        {
            Console.WriteLine("Draw() Called");
            base.Draw(rect);

            using (CGContext context = UIGraphics.GetCurrentContext())
            {
                CGAffineTransform affineTransform = context.GetCTM();
                //affineTransform.Scale (1, -1);
                affineTransform.Translate(1, -1);
                context.ConcatCTM(affineTransform);

                //---- fill the background with white
                // set fill color
                UIColor.White.SetFill();
                //context.SetRGBFillColor (1, 1, 1, 1f);
                // paint
                context.FillRect(rect);

                PointF[] myStarPoints = { new PointF(5f,                 5f)
                                          ,                 new PointF(10f,   15f), new PointF(10f, 15f)
                                          ,                 new PointF(15f,    5f), new PointF(15f, 5f)
                                          ,                 new PointF(12f,    5f), new PointF(15f, 5f)
                                          ,                 new PointF(2.5f,  11f), new PointF(2.5f, 11f)
                                          ,                 new PointF(16.5f, 11f),
                                          new PointF(16.5f,                 11f)
                                          ,                 new PointF(5f, 5f) };

                //---- create the layer
                using (CGLayer starLayer = CGLayer.Create(context, rect.Size))
                {
                    //---- set fill to blue
                    starLayer.Context.SetRGBFillColor(0f, 0f, 1f, 1f);
                    starLayer.Context.AddLines(myStarPoints);
                    starLayer.Context.FillPath();

                    //---- draw the layer onto our screen
                    float starYPos = 5;
                    float starXPos = 5;

                    for (int row = 0; row < 50; row++)
                    {
                        //---- reset the x position for each row
                        starXPos = 5;
                        //----
                        for (int col = 0; col < 30; col++)
                        {
                            context.DrawLayer(starLayer, new PointF(starXPos, starYPos));
                            starXPos += 20;
                        }
                        starYPos += 20;
                    }
                }
            }
        }
Ejemplo n.º 11
0
 public override void DrawLayer(CALayer layer, CGContext context)
 {
     context.SaveState ();
     context.SetFillColor (1.0f, 1.0f, 1.0f, 1.0f);
     context.FillRect (context.GetClipBoundingBox ());
     context.TranslateCTM (0.0f, layer.Bounds.Size.Height);
     context.ScaleCTM (1.0f, -1.0f);
     context.ConcatCTM (this.oParentController.currentPDFPage.GetDrawingTransform (CGPDFBox.Crop, layer.Bounds, 0, true));
     context.DrawPDFPage (this.oParentController.currentPDFPage);
     context.RestoreState ();
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Rotates the image.
        /// </summary>
        /// <param name="image">Image.</param>
        private void RotateImage(ref UIImage image)
        {
            CGImage           imgRef    = image.CGImage;
            CGAffineTransform transform = CGAffineTransform.MakeIdentity();

            var imgHeight = imgRef.Height * _imgScale;
            var imgWidth  = imgRef.Width * _imgScale;

            CGRect             bounds    = new CGRect(0, 0, imgWidth, imgHeight);
            CGSize             imageSize = new CGSize(imgWidth, imgHeight);
            UIImageOrientation orient    = image.Orientation;

            switch (orient)
            {
            case UIImageOrientation.Up:
                transform = CGAffineTransform.MakeIdentity();
                break;

            case UIImageOrientation.Down:
                transform = CGAffineTransform.MakeTranslation(imageSize.Width, imageSize.Height);
                transform = CGAffineTransform.Rotate(transform, (float)Math.PI);
                break;

            case UIImageOrientation.Right:
                bounds.Size = new CGSize(bounds.Size.Height, bounds.Size.Width);
                transform   = CGAffineTransform.MakeTranslation(imageSize.Height, 0);
                transform   = CGAffineTransform.Rotate(transform, (float)Math.PI / 2.0f);
                break;

            default:
                throw new Exception("Invalid image orientation");
            }

            UIGraphics.BeginImageContext(bounds.Size);
            CGContext context = UIGraphics.GetCurrentContext();

            if (orient == UIImageOrientation.Right)
            {
                context.ScaleCTM(-1, 1);
                context.TranslateCTM(-imgHeight, 0);
            }
            else
            {
                context.ScaleCTM(1, -1);
                context.TranslateCTM(0, -imgHeight);
            }

            context.ConcatCTM(transform);

            context.DrawImage(new CGRect(0, 0, imgWidth, imgHeight), imgRef);
            image = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();
        }
Ejemplo n.º 13
0
        internal void Draw(CGContext context, float x, float y, UGColor color)
        {
#if __MACOS__
            NSStringAttributes attributes = null;
            using (var nsColor = color.ToNSColor())
            {
                attributes = new NSStringAttributes()
                {
                    Font            = _textFormat.Native,
                    ForegroundColor = nsColor,
                };
            }
#else
            UIStringAttributes attributes = null;
            using (var uiColor = color.ToUIColor())
            {
                attributes = new UIStringAttributes()
                {
                    Font            = _textFormat.Native,
                    ForegroundColor = uiColor,
                };
            }
#endif

            var bounds = LayoutBounds;
            var rect   = new CGRect(
                x + bounds.X,
                y + bounds.Y,
                bounds.Width,
                bounds.Height);
            var scaleMatrix = CGAffineTransformHelper.CreateScale(
                1F,
                -1F,
                (float)rect.X,
                (float)rect.Y);
            try
            {
                context.SaveState();
                context.ConcatCTM(scaleMatrix);
                context.TranslateCTM(0, -bounds.Height);
#if __MACOS__
                _native.DrawInRect(rect, attributes);
#else
                _native.DrawString(rect, attributes);
#endif
            }
            finally { context.RestoreState(); }
        }
Ejemplo n.º 14
0
        private void draw(CGContext context)
        {
            if (!PDFDocument.DocumentHasLoaded)
            {
                return;
            }

            context.SetFillColor(1.0f, 1.0f, 1.0f, 1.0f);
            using (CGPDFPage pdfPage = PDFDocument.GetPage(_pageNumber)) {
                context.TranslateCTM(0, Bounds.Height);
                context.ScaleCTM(1.0f, -1.0f);
                context.ConcatCTM(pdfPage.GetDrawingTransform(CGPDFBox.Crop, Bounds, 0, true));
                context.SetRenderingIntent(CGColorRenderingIntent.Default);
                context.InterpolationQuality = CGInterpolationQuality.Default;
                context.DrawPDFPage(pdfPage);
            }
        }
        static UIImage Invert(UIImage originalImage)
        {
            // Invert the image by applying an affine transformation
            UIGraphics.BeginImageContext(originalImage.Size);

            // Apply an affine transformation to the original image to generate a vertically flipped image
            CGContext context = UIGraphics.GetCurrentContext();
            var       affineTransformationInvert = new CGAffineTransform(1, 0, 0, -1, 0, originalImage.Size.Height);

            context.ConcatCTM(affineTransformationInvert);
            originalImage.Draw(PointF.Empty);

            UIImage invertedImage = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            return(invertedImage);
        }
Ejemplo n.º 16
0
        // rect changes depending on if the whole view is being redrawn, or just a section
        public override void Draw(RectangleF rect)
        {
            Console.WriteLine("Draw() Called");
            base.Draw(rect);

            // get a reference to the context
            using (CGContext context = UIGraphics.GetCurrentContext()) {
                // convert to View space
                CGAffineTransform affineTransform = CGAffineTransform.MakeIdentity();
                // invert the y axis
                affineTransform.Scale(1, -1);
                // move the y axis up
                affineTransform.Translate(0, Frame.Height);
                context.ConcatCTM(affineTransform);

                // draw some stars
                DrawStars(context);
            }
        }
Ejemplo n.º 17
0
        public static UIImage PDF2Image(CGPDFPage page, nfloat width, nfloat scale)
        {
            UIImage img = new UIImage();

            try
            {
                CGRect pageRect = page.GetBoxRect(CGPDFBox.Media);
                nfloat pdfScale = width / pageRect.Size.Width;
                pageRect.Size = new CGSize(pageRect.Size.Width * pdfScale, pageRect.Size.Height * pdfScale);

                UIGraphics.BeginImageContextWithOptions(pageRect.Size, true, scale);
                CGContext context = UIGraphics.GetCurrentContext();

                // White BG
                context.SetFillColor(1.0f, 1.0f, 1.0f, 1f);
                context.FillRect(pageRect);
                context.SaveState();

                //border
                context.SetStrokeColor(0f, 0f, 0f, 0.5f);
                context.StrokeRect(pageRect);

                // Next 3 lines makes the rotations so that the page look in the right direction
                context.TranslateCTM(0.0f, pageRect.Size.Height);
                context.ScaleCTM(1.0f, -1.0f);
                CGAffineTransform transform = page.GetDrawingTransform(CGPDFBox.Media, pageRect, 0, true);
                context.ConcatCTM(transform);

                context.DrawPDFPage(page);
                context.RestoreState();

                img = UIGraphics.GetImageFromCurrentImageContext();

                UIGraphics.EndImageContext();

                context.Dispose();
            }
            catch (Exception ex)
            {
            }
            return(img);
        }
Ejemplo n.º 18
0
        public override void Draw(CGRect rect)
        {
            base.Draw(rect);

            //flip the CTM so the PDF will be drawn upright
            using (CGContext g = UIGraphics.GetCurrentContext()) {
                g.TranslateCTM(0, Bounds.Height);
                g.ScaleCTM(1, -1);

                // render the first page of the PDF
                using (CGPDFPage pdfPage = pdfDoc.GetPage(1)) {
                    //get the affine transform that defines where the PDF is drawn
                    CGAffineTransform t = pdfPage.GetDrawingTransform(CGPDFBox.Crop, rect, 0, true);
                    //concatenate the pdf transform with the CTM for display in the view
                    g.ConcatCTM(t);
                    //draw the pdf page
                    g.DrawPDFPage(pdfPage);
                }
            }
        }
Ejemplo n.º 19
0
        public UIImage GetPDFImageForPage()
        {
            CGPDFPage pdfPg = _pdf.GetPage(PageNumber);
            nfloat    scale;

            PDFpageRect = pdfPg.GetBoxRect(CGPDFBox.Media);
            if (PDFpageRect.Height > PDFpageRect.Width)
            {
                scale = (this.View.Frame.Width - 80.0f) / PDFpageRect.Width;
            }
            else
            {
                scale = this.View.Frame.Height / PDFpageRect.Height;
            }

            PDFpageRect.Size = new CGSize(PDFpageRect.Width * scale, PDFpageRect.Height * scale);

            UIGraphics.BeginImageContext(PDFpageRect.Size);
            CGContext context = UIGraphics.GetCurrentContext();

            context.SetFillColor((nfloat)1.0, (nfloat)1.0, (nfloat)1.0, (nfloat)1.0);
            context.FillRect(PDFpageRect);

            context.SaveState();

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

            context.ConcatCTM(CGAffineTransform.MakeScale(scale, scale));


            context.DrawPDFPage(pdfPg);
            context.RestoreState();

            UIImage thm = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            return(thm);
        }
Ejemplo n.º 20
0
        internal void ConvertPagetoImage(int index)
        {
            if (this.PdfDocument == null || this.Element.ImageStreams.ContainsKey(index))
            {
                return;
            }

            float   scaleFactor = 1;
            UIImage image       = null;

            using (CGPDFPage pdfPage = this.PdfDocument.GetPage(index + 1))
            {
                if (pdfPage != null)
                {
                    CGRect rect   = pdfPage.GetBoxRect(CGPDFBox.Media);
                    nfloat factor = (nfloat)scaleFactor;
                    CGRect bounds = new CGRect(rect.X * factor, rect.Y * factor, rect.Width * factor, rect.Height * factor);
                    UIGraphics.BeginImageContext(bounds.Size);
                    CGContext context = UIGraphics.GetCurrentContext();
                    context.SetFillColor(1.0f, 1.0f, 1.0f, 1.0f);
                    context.FillRect(bounds);
                    context.TranslateCTM(0, bounds.Height);
                    context.ScaleCTM(factor, -factor);
                    context.ConcatCTM(pdfPage.GetDrawingTransform(CGPDFBox.Crop, rect, 0, true));
                    context.SetRenderingIntent(CGColorRenderingIntent.Default);
                    context.InterpolationQuality = CGInterpolationQuality.Default;
                    context.DrawPDFPage(pdfPage);
                    image = UIGraphics.GetImageFromCurrentImageContext();
                    UIGraphics.EndImageContext();
                }
            }

            if (image != null)
            {
                this.Element.ImageStreams.Add(index, image);
                this.Element.PDFViewModel.Items[index].PageData = image;
            }
        }
Ejemplo n.º 21
0
        public override void Draw(RectangleF rect)
        {
            base.Draw(rect);

            CGContext gctx = UIGraphics.GetCurrentContext();

            gctx.TranslateCTM(0, Bounds.Height);
            gctx.ScaleCTM(1, -1);

            using (CGPDFPage pdfPg = _pdf.GetPage(PageNumber)) {
                gctx.SaveState();

                RectangleF        r  = new RectangleF(Bounds.Left, Bounds.Top, Bounds.Width, Bounds.Height + 44f);
                CGAffineTransform tf = pdfPg.GetDrawingTransform(CGPDFBox.Crop, r, 0, true);
                gctx.ConcatCTM(tf);
                gctx.DrawPDFPage(pdfPg);

                gctx.RestoreState();
                gctx.TranslateCTM(0, Bounds.Height - 25);
                gctx.SelectFont("Helvetica", 25f, CGTextEncoding.MacRoman);
                gctx.ShowText(AnnotatedText);
            }
        }
Ejemplo n.º 22
0
    public override void DrawInContext(CGContext context)
    {
        // PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system
        // before we start drawing.
        context.TranslateCTM(0, Bounds.Height);
        context.ScaleCTM(1, -1);

        // Grab the first PDF page
        using (CGPDFPage page = doc.GetPage(1)) {
            // We're about to modify the context CTM to draw the PDF page where we want it, so save the graphics state in case we want to do more drawing
            context.SaveState();

            // CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any
            // base rotations necessary to display the PDF page correctly.

            CGAffineTransform pdfTransform = page.GetDrawingTransform(CGPDFBox.Crop, Bounds, 0, true);

            // And apply the transform.
            context.ConcatCTM(pdfTransform);
            // Finally, we draw the page and restore the graphics state for further manipulations!
            context.DrawPDFPage(page);
            context.RestoreState();
        }
    }
Ejemplo n.º 23
0
        public void EraseStart()
        {
            if (drawingBoard == null)
                return;

            UIGraphics.BeginImageContext (drawingBoard.Size);

            // erase lines
            ctx = UIGraphics.GetCurrentContext ();

            // Convert co-ordinate system to Cocoa's (origin in UL, not LL)
            ctx.TranslateCTM (0, drawingBoard.Size.Height);
            ctx.ConcatCTM (CGAffineTransform.MakeScale (1, -1));

            ctx.DrawImage (new RectangleF (0, 0, drawingBoard.Size.Width, drawingBoard.Size.Height),
                        drawingBoard.CGImage);
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Scales the and rotate image view.
        /// </summary>
        /// <returns>The and rotate image view.</returns>
        /// <param name="imageIn">Image in.</param>
        /// <param name="orIn">Or in.</param>
        public static UIImage ScaleAndRotateImageView(UIImage imageIn, UIImageOrientation orIn)
        {
            float   kMaxResolution = 1024;
            UIImage imageCopy      = imageIn;

            try
            {
                CGImage imgRef = imageIn.CGImage;
                imageIn.Dispose();
                imageIn = null;
                float width  = imgRef.Width;
                float height = imgRef.Height;
                Debug.WriteLine(string.Format("ScaleAndRotateImageView - line# {0}", 29));
                CGAffineTransform transform = CGAffineTransform.MakeIdentity();
                RectangleF        bounds    = new RectangleF(0, 0, width, height);

                if (width > kMaxResolution || height > kMaxResolution)
                {
                    float ratio = width / height;

                    if (ratio > 1)
                    {
                        bounds.Width  = kMaxResolution;
                        bounds.Height = bounds.Width / ratio;
                    }
                    else
                    {
                        bounds.Height = kMaxResolution;
                        bounds.Width  = bounds.Height * ratio;
                    }
                }

                float scaleRatio          = bounds.Width / width;
                SizeF imageSize           = new SizeF(width, height);
                UIImageOrientation orient = orIn;
                float boundHeight;
                Debug.WriteLine(string.Format("ScaleAndRotateImageView - line# {0}", 53));
                switch (orient)
                {
                case UIImageOrientation.Up:                                                                //EXIF = 1
                    transform = CGAffineTransform.MakeIdentity();
                    break;

                case UIImageOrientation.UpMirrored:                                                        //EXIF = 2
                    transform = CGAffineTransform.MakeTranslation(imageSize.Width, 0f);
                    transform = CGAffineTransform.MakeScale(-1.0f, 1.0f);
                    break;

                case UIImageOrientation.Down:                                                              //EXIF = 3
                    transform = CGAffineTransform.MakeTranslation(imageSize.Width, imageSize.Height);
                    transform = CGAffineTransform.Rotate(transform, (float)Math.PI);
                    break;

                case UIImageOrientation.DownMirrored:                                                      //EXIF = 4
                    transform = CGAffineTransform.MakeTranslation(0f, imageSize.Height);
                    transform = CGAffineTransform.MakeScale(1.0f, -1.0f);
                    break;

                case UIImageOrientation.LeftMirrored:                                                      //EXIF = 5
                    boundHeight   = bounds.Height;
                    bounds.Height = bounds.Width;
                    bounds.Width  = boundHeight;
                    transform     = CGAffineTransform.MakeTranslation(imageSize.Height, imageSize.Width);
                    transform     = CGAffineTransform.MakeScale(-1.0f, 1.0f);
                    transform     = CGAffineTransform.Rotate(transform, 3.0f * (float)Math.PI / 2.0f);
                    break;

                case UIImageOrientation.Left:                                                              //EXIF = 6
                    boundHeight   = bounds.Height;
                    bounds.Height = bounds.Width;
                    bounds.Width  = boundHeight;
                    transform     = CGAffineTransform.MakeTranslation(0.0f, imageSize.Width);
                    transform     = CGAffineTransform.Rotate(transform, 3.0f * (float)Math.PI / 2.0f);
                    break;

                case UIImageOrientation.RightMirrored:                                                     //EXIF = 7
                    boundHeight   = bounds.Height;
                    bounds.Height = bounds.Width;
                    bounds.Width  = boundHeight;
                    transform     = CGAffineTransform.MakeScale(-1.0f, 1.0f);
                    transform     = CGAffineTransform.Rotate(transform, (float)Math.PI / 2.0f);
                    break;

                case UIImageOrientation.Right:                                                             //EXIF = 8
                    boundHeight   = bounds.Height;
                    bounds.Height = bounds.Width;
                    bounds.Width  = boundHeight;
                    transform     = CGAffineTransform.MakeTranslation(imageSize.Height, 0.0f);
                    transform     = CGAffineTransform.Rotate(transform, (float)Math.PI / 2.0f);
                    break;

                default:
                    //throw new Exception("Invalid image orientation");
                    Debug.WriteLine(string.Format("ScaleAndRotateImageView Invalid image orientation - line# {0}", 110));
                    break;
                }

                try
                {
                    Debug.WriteLine(string.Format("ScaleAndRotateImageView - line# {0}", 115));
                    UIGraphics.BeginImageContext(bounds.Size);

                    CGContext context = UIGraphics.GetCurrentContext();

                    if (orient == UIImageOrientation.Right || orient == UIImageOrientation.Left)
                    {
                        context.ScaleCTM(-scaleRatio, scaleRatio);
                        context.TranslateCTM(-height, 0);
                    }
                    else
                    {
                        context.ScaleCTM(scaleRatio, -scaleRatio);
                        context.TranslateCTM(0, -height);
                    }
                    Debug.WriteLine(string.Format("ScaleAndRotateImageView - line# {0}", 130));
                    context.ConcatCTM(transform);
                    context.DrawImage(new RectangleF(0, 0, width, height), imgRef);


                    // added context dispose - to free memory used by picture image
                    imgRef.Dispose();
                    imgRef = null;

                    imageCopy.Dispose();
                    imageCopy = null;

                    imageCopy = UIGraphics.GetImageFromCurrentImageContext();

                    UIGraphics.EndImageContext();
                    // added context dispose - to free memory used by the graphics context
                    context.Dispose();
                    context = null;

                    imageIn = null;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Exception Occured in ScaleAndRotateImageView  - line # 164 method due to " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception Occured in ScaleAndRotateImageView - line # 169 method due to " + ex.Message);
            }

            return(imageCopy);
        }
Ejemplo n.º 25
0
        private List <UIImage> ConvertToImages( )
        {
            Images = new List <UIImage>();

            if (Doc == null)
            {
                throw new Exception("Could not load document");
            }

            for (Int32 i = 1; i <= Count; i++)
            {
                using (CGPDFPage Page = Doc.GetPage(i))
                {
                    CGRect PageRect = Page.GetBoxRect(CGPDFBox.Media);
                    //nfloat Scale = 1;//View.Frame.Height / PageRect.Height;
                    //PageRect.Size = new CGSize(PageRect.Height * Scale, PageRect.Width * Scale);

                    if (PageRect.Height > PageRect.Width)
                    {
                        PageRect.Size = new CGSize(PageRect.Height, PageRect.Width);
                    }

                    CGRect MediaBox = Page.GetBoxRect(CGPDFBox.Media);
                    CGRect CropBox  = Page.GetBoxRect(CGPDFBox.Crop);

                    nfloat TopMargin    = CropBox.GetMinY() - MediaBox.GetMinY();
                    nfloat BottomMargin = MediaBox.GetMaxY() - CropBox.GetMaxY();
                    nfloat LeftMargin   = CropBox.GetMinX() - MediaBox.GetMinX();
                    nfloat RightMargin  = MediaBox.GetMaxX() - CropBox.GetMaxX();

                    if (TopMargin + BottomMargin + LeftMargin + RightMargin > 0)
                    {
                        PageRect = new CGRect(
                            PageRect.Location,
                            new CGSize(
                                PageRect.Size.Width - (LeftMargin + RightMargin),
                                PageRect.Size.Height - (TopMargin + BottomMargin)
                                )
                            );
                    }

                    UIGraphics.BeginImageContext(PageRect.Size);

                    using (CGContext context = UIGraphics.GetCurrentContext())
                    {
                        context.SaveState();

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

                        context.ConcatCTM(
                            Page.GetDrawingTransform(CGPDFBox.Crop, PageRect, 0, true)
                            );

                        context.DrawPDFPage(Page);
                        context.RestoreState();

                        Images.Add(UIGraphics.GetImageFromCurrentImageContext());
                    }

                    UIGraphics.EndImageContext();
                }
            }
            return(Images);
        }
        UIImage ScaleAndRotateImage(UIImage imageIn, UIImageOrientation orIn)
        {
            int kMaxResolution = 2048;

            CGImage           imgRef    = imageIn.CGImage;
            float             width     = imgRef.Width;
            float             height    = imgRef.Height;
            CGAffineTransform transform = CGAffineTransform.MakeIdentity();

            System.Drawing.RectangleF bounds = new System.Drawing.RectangleF(0, 0, width, height);

            if (width > kMaxResolution || height > kMaxResolution)
            {
                float ratio = width / height;

                if (ratio > 1)
                {
                    bounds.Width  = kMaxResolution;
                    bounds.Height = bounds.Width / ratio;
                }
                else
                {
                    bounds.Height = kMaxResolution;
                    bounds.Width  = bounds.Height * ratio;
                }
            }

            float scaleRatio = bounds.Width / width;

            System.Drawing.SizeF imageSize = new System.Drawing.SizeF(width, height);
            UIImageOrientation   orient    = orIn;
            float boundHeight;

            switch (orient)
            {
            case UIImageOrientation.Up:                                                            //EXIF = 1
                transform = CGAffineTransform.MakeIdentity();
                break;

            case UIImageOrientation.UpMirrored:                                                    //EXIF = 2
                transform = CGAffineTransform.MakeTranslation(imageSize.Width, 0f);
                transform = CGAffineTransform.MakeScale(-1.0f, 1.0f);
                break;

            case UIImageOrientation.Down:                                                          //EXIF = 3
                transform = CGAffineTransform.MakeTranslation(imageSize.Width, imageSize.Height);
                transform = CGAffineTransform.Rotate(transform, (float)Math.PI);
                break;

            case UIImageOrientation.DownMirrored:                                                  //EXIF = 4
                transform = CGAffineTransform.MakeTranslation(0f, imageSize.Height);
                transform = CGAffineTransform.MakeScale(1.0f, -1.0f);
                break;

            case UIImageOrientation.LeftMirrored:                                                  //EXIF = 5
                boundHeight   = bounds.Height;
                bounds.Height = bounds.Width;
                bounds.Width  = boundHeight;
                transform     = CGAffineTransform.MakeTranslation(imageSize.Height, imageSize.Width);
                transform     = CGAffineTransform.MakeScale(-1.0f, 1.0f);
                transform     = CGAffineTransform.Rotate(transform, 3.0f * (float)Math.PI / 2.0f);
                break;

            case UIImageOrientation.Left:                                                          //EXIF = 6
                boundHeight   = bounds.Height;
                bounds.Height = bounds.Width;
                bounds.Width  = boundHeight;
                transform     = CGAffineTransform.MakeTranslation(0.0f, imageSize.Width);
                transform     = CGAffineTransform.Rotate(transform, 3.0f * (float)Math.PI / 2.0f);
                break;

            case UIImageOrientation.RightMirrored:                                                 //EXIF = 7
                boundHeight   = bounds.Height;
                bounds.Height = bounds.Width;
                bounds.Width  = boundHeight;
                transform     = CGAffineTransform.MakeScale(-1.0f, 1.0f);
                transform     = CGAffineTransform.Rotate(transform, (float)Math.PI / 2.0f);
                break;

            case UIImageOrientation.Right:                                                         //EXIF = 8
                boundHeight   = bounds.Height;
                bounds.Height = bounds.Width;
                bounds.Width  = boundHeight;
                transform     = CGAffineTransform.MakeTranslation(imageSize.Height, 0.0f);
                transform     = CGAffineTransform.Rotate(transform, (float)Math.PI / 2.0f);
                break;

            default:
                throw new Exception("Invalid image orientation");
            }

            UIGraphics.BeginImageContext(bounds.Size);

            CGContext context = UIGraphics.GetCurrentContext();

            if (orient == UIImageOrientation.Right || orient == UIImageOrientation.Left)
            {
                context.ScaleCTM(-scaleRatio, scaleRatio);
                context.TranslateCTM(-height, 0);
            }
            else
            {
                context.ScaleCTM(scaleRatio, -scaleRatio);
                context.TranslateCTM(0, -height);
            }

            context.ConcatCTM(transform);
            context.DrawImage(new RectangleF(0, 0, width, height), imgRef);

            UIImage imageCopy = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            return(imageCopy);
        }
Ejemplo n.º 27
0
        // test draw pattern
        private void DrawTexture(CGContext context)
        {
            var destRect = context.ConvertRectToUserSpace(new CGRect(0, 0, textureImage.Width, textureImage.Height));

            context.ConcatCTM(textureImage.imageTransform);
            context.DrawImage(destRect, textureImage.NativeCGImage);
            context.ConcatCTM(textureImage.imageTransform.Invert());

            if (wrapMode == WrapMode.TileFlipX || wrapMode == WrapMode.TileFlipXY)
            {
                context.ConcatCTM(CGAffineTransform.MakeScale(-1, 1));
                context.ConcatCTM(textureImage.imageTransform);
                context.DrawImage(destRect, textureImage.NativeCGImage);
                context.ConcatCTM(textureImage.imageTransform.Invert());
            }

            if (wrapMode == WrapMode.TileFlipY || wrapMode == WrapMode.TileFlipXY)
            {
                var transformY = new CGAffineTransform(1, 0, 0, -1,
                                                       destRect.Width,
                                                       destRect.Height);
                context.ConcatCTM(transformY);
                context.ConcatCTM(textureImage.imageTransform);
                context.DrawImage(destRect, textureImage.NativeCGImage);
                context.ConcatCTM(textureImage.imageTransform.Invert());
            }


            if (wrapMode == WrapMode.TileFlipXY)
            {
                // draw the last one of the quadrant which is flipped by both the y and x axis
                var transform = new CGAffineTransform(-1, 0, 0, -1,
                                                      destRect.Width * 2, destRect.Height);
                context.ConcatCTM(transform);
                context.ConcatCTM(textureImage.imageTransform);
                context.DrawImage(destRect, textureImage.NativeCGImage);
                context.ConcatCTM(textureImage.imageTransform.Invert());
            }
        }
Ejemplo n.º 28
0
        public override void DrawText(ScreenPoint p, string text, OxyColor fill, string fontFamily, double fontSize, double fontWeight, double rotate, HorizontalTextAlign halign, VerticalTextAlign valign, OxySize?maxSize)
        {
            //This method needs work not 100% around vertical alignment.
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            fontFamily = GetDefaultFont(fontFamily);

            if (fontWeight >= 700)
            {
                //fs = FontStyle.Bold;
            }

            //var textSize = MeasureText(text, fontFamily, fontSize, fontWeight);

            if (maxSize != null)
            {
//                if (size.Width > maxSize.Value.Width)
//                {
//                    size.Width = (float)maxSize.Value.Width;
//                }
//
//                if (size.Height > maxSize.Value.Height)
//                {
//                    size.Height = (float)maxSize.Value.Height;
//                }
            }

            gctx.SaveState();

            gctx.SelectFont(fontFamily, (float)fontSize, CGTextEncoding.MacRoman);
            ToColor(fill).SetFill();

            gctx.SetTextDrawingMode(CGTextDrawingMode.Fill);

            var      tfont = UIFont.FromName(fontFamily, (float)fontSize);
            NSString nsstr = new NSString(text);
            SizeF    sz    = nsstr.StringSize(tfont);

            float y = (float)(p.Y);
            float x = (float)(p.X);

            // Rotate the text here.
            var m = CGAffineTransform.MakeTranslation(-x, -y);

            m.Multiply(CGAffineTransform.MakeRotation((float)DegreesToRadians(rotate)));
            m.Multiply(CGAffineTransform.MakeTranslation(x, y));

            gctx.ConcatCTM(m);

            switch (halign)
            {
            case HorizontalTextAlign.Left:
                x = (float)(p.X);
                break;

            case HorizontalTextAlign.Right:
                x = (float)(p.X - sz.Width);
                break;

            case HorizontalTextAlign.Center:
                x = (float)(p.X - (sz.Width / 2));
                break;
            }

            switch (valign)
            {
            case VerticalTextAlign.Bottom:
                y -= (float)fontSize;
                break;

            case VerticalTextAlign.Top:
//				y += (float)fontSize;
                break;

            case VerticalTextAlign.Middle:
                y -= (float)(fontSize / 2);
                break;
            }


            RectangleF rect = new RectangleF(x, y, sz.Width, sz.Height);

            nsstr.DrawString(rect, tfont);

            gctx.RestoreState();
            //Console.WriteLine("X:{0:###} Y:{1:###} HA:{2}:{3:###} VA:{4}:{5:###} TW:{6:###} - {7}", p.X, p.Y, halign, x, valign, y, textSize.Width, text);
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Draws the document page
        /// </summary>		
        private void Draw(CGContext context)
        {
            if (!PDFDocument.DocumentHasLoaded) {
                return;
            }

            // Draw page
            context.SetFillColor(1.0f, 1.0f, 1.0f, 1.0f);
            using (CGPDFPage pdfPage = PDFDocument.GetPage(mPageNumber)) {
                context.TranslateCTM(0, Bounds.Height);
                context.ScaleCTM(1.0f, -1.0f);
                context.ConcatCTM(pdfPage.GetDrawingTransform(CGPDFBox.Crop, Bounds, 0, true));
                context.SetRenderingIntent(CGColorRenderingIntent.Default);
                context.InterpolationQuality = CGInterpolationQuality.Default;
                context.DrawPDFPage(pdfPage);
            }
        }
Ejemplo n.º 30
0
        // test draw pattern
        protected void DrawTexture(CGContext context)
        {
            var destRect = new CGRect (0,0,textureImage.Width,textureImage.Height);
            context.DrawImage(destRect, textureImage.NativeCGImage);

            if (wrapMode == WrapMode.TileFlipX)
            {
                context.ConcatCTM(CGAffineTransform.MakeScale(-1,1));
                context.DrawImage(destRect, textureImage.NativeCGImage);
            }

            if (wrapMode == WrapMode.TileFlipY)
            {
                var transformY = new CGAffineTransform(1, 0, 0, -1,
                                                       textureImage.Width,
                                                       textureImage.Height);
                context.ConcatCTM(transformY);
                context.DrawImage(destRect, textureImage.NativeCGImage);
            }

            if (wrapMode == WrapMode.TileFlipXY)
            {
                // draw the original
                var transform = new CGAffineTransform(1, 0, 0, 1,
                                                       0, textureImage.Height);
                context.ConcatCTM(transform);
                context.DrawImage(destRect, textureImage.NativeCGImage);

                // reset the transform
                context.ConcatCTM (context.GetCTM().Invert());

                // draw next to original one that is flipped by x axis
                transform = new CGAffineTransform(-1, 0, 0, 1,
                                                  textureImage.Width * 2, textureImage.Height);
                context.ConcatCTM(transform);
                context.DrawImage(destRect, textureImage.NativeCGImage);

                // reset the transform
                context.ConcatCTM (context.GetCTM().Invert());

                // draw one that is flipped by Y axis under the oricinal
                transform = new CGAffineTransform(1, 0, 0, -1,
                                                  0, textureImage.Height);
                context.ConcatCTM(transform);
                context.DrawImage(destRect, textureImage.NativeCGImage);

                // draw the last one of the quadrant which is flipped by both the y and x axis
                context.ConcatCTM (context.GetCTM().Invert());
                transform = new CGAffineTransform(-1, 0, 0, -1,
                                                  textureImage.Width * 2, textureImage.Height);
                context.ConcatCTM(transform);
                context.DrawImage(destRect, textureImage.NativeCGImage);
            }
        }
Ejemplo n.º 31
0
	public override void DrawInContext (CGContext context)
	{
		// PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system
		// before we start drawing.
		context.TranslateCTM (0, Bounds.Height);
		context.ScaleCTM (1, -1);
		
		// Grab the first PDF page
		using (CGPDFPage page = doc.GetPage (1)){
			// We're about to modify the context CTM to draw the PDF page where we want it, so save the graphics state in case we want to do more drawing
			context.SaveState ();
			
			// CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any
			// base rotations necessary to display the PDF page correctly.
			
			CGAffineTransform pdfTransform = page.GetDrawingTransform (CGPDFBox.Crop, Bounds, 0, true);

			// And apply the transform.
			context.ConcatCTM (pdfTransform);
			// Finally, we draw the page and restore the graphics state for further manipulations!
			context.DrawPDFPage (page);
			context.RestoreState();
		}
	}	
Ejemplo n.º 32
0
        /// <summary>
        /// Gets the rotate image.
        /// </summary>
        /// <param name="imagePath">The image path.</param>
        /// <returns>UIImage.</returns>
        private static UIImage GetRotateImage(String imagePath)
        {
            UIImage image  = UIImage.FromFile(imagePath);
            CGImage imgRef = image.CGImage;

            float width  = imgRef.Width;
            float height = imgRef.Height;

            CGAffineTransform  transform = CGAffineTransform.MakeIdentity();
            RectangleF         bounds    = new RectangleF(0, 0, 200, 200);
            SizeF              imageSize = new SizeF(200, 200);
            float              boundHeight;
            UIImageOrientation orient = image.Orientation;

            switch (orient)
            {
            case UIImageOrientation.Up:
                transform = CGAffineTransform.MakeIdentity();
                break;

            case UIImageOrientation.UpMirrored:
                transform = CGAffineTransform.MakeTranslation(imageSize.Width, 0.0f);
                transform.Scale(-1.0f, 1.0f);
                break;

            case UIImageOrientation.Down:
                transform.Rotate((float)Math.PI);
                transform.Translate(imageSize.Width, imageSize.Height);
                break;

            case UIImageOrientation.DownMirrored:
                transform = CGAffineTransform.MakeTranslation(0.0f, imageSize.Height);
                transform.Scale(1.0f, -1.0f);
                break;

            case UIImageOrientation.LeftMirrored:
                boundHeight   = bounds.Size.Height;
                bounds.Height = bounds.Size.Width;
                bounds.Width  = boundHeight;
                transform.Scale(-1.0f, 1.0f);
                transform.Rotate((float)Math.PI / 2.0f);
                break;

            case UIImageOrientation.Left:
                boundHeight   = bounds.Size.Height;
                bounds.Height = bounds.Size.Width;
                bounds.Width  = boundHeight;
                transform     = CGAffineTransform.MakeRotation((float)Math.PI / 2.0f);
                transform.Translate(imageSize.Height, 0.0f);
                break;

            case UIImageOrientation.RightMirrored:
                boundHeight   = bounds.Size.Height;
                bounds.Height = bounds.Size.Width;
                bounds.Width  = boundHeight;
                transform     = CGAffineTransform.MakeTranslation(imageSize.Height, imageSize.Width);
                transform.Scale(-1.0f, 1.0f);
                transform.Rotate(3.0f * (float)Math.PI / 2.0f);
                break;

            case UIImageOrientation.Right:
                boundHeight   = bounds.Size.Height;
                bounds.Height = bounds.Size.Width;
                bounds.Width  = boundHeight;
                transform     = CGAffineTransform.MakeRotation(-(float)Math.PI / 2.0f);
                transform.Translate(0.0f, imageSize.Width);
                break;
            }

            UIGraphics.BeginImageContext(bounds.Size);

            CGContext context = UIGraphics.GetCurrentContext();

            context.ConcatCTM(transform);

            context = UIGraphics.GetCurrentContext();
            context.DrawImage(new RectangleF(0, 0, width, height), imgRef);
            UIImage imageCopy = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            return(imageCopy);
        }
Ejemplo n.º 33
0
        public static UIImage ScaleAndRotateImage(this UIImage image, int maxResolution)
        {
            if (image.Size.Width < maxResolution && image.Size.Height < maxResolution)
            {
                return(image);
            }

            //CGImage imgRef = image.CGImage;
            using (CGImage imgRef = image.CGImage)
            {
                float width  = imgRef.Width;
                float height = imgRef.Height;

                //int kMaxResolution = (int)Math.Max (width, height); // use current size of picture (not scale or resize)
                int kMaxResolution = maxResolution;                 // based on customer's idea


                CGAffineTransform transform = CGAffineTransform.MakeIdentity();
                CGRect            bounds    = new CGRect(0, 0, width, height);
                if (width > kMaxResolution || height > kMaxResolution)
                {
                    float ratio = width / height;
                    if (ratio > 1)
                    {
                        //                  bounds.Size = new CGSize (kMaxResolution, bounds.Size.Width / ratio);
                        bounds.Size = new CGSize(kMaxResolution, kMaxResolution / ratio);
                    }
                    else
                    {
                        //                  bounds.Size = new CGSize (bounds.Size.Height * ratio, kMaxResolution);
                        bounds.Size = new CGSize(kMaxResolution * ratio, kMaxResolution);
                    }
                }

                float              scaleRatio = (float)bounds.Size.Width / width;
                CGSize             imageSize  = new CGSize(imgRef.Width, imgRef.Height);
                float              boundHeight;
                UIImageOrientation orient = image.Orientation;
                switch (orient)
                {
                case UIImageOrientation.Up:                         //EXIF = 1
                    transform = CGAffineTransform.MakeIdentity();
                    break;

                case UIImageOrientation.UpMirrored:                         //EXIF = 2
                    transform = CGAffineTransform.MakeTranslation(imageSize.Width, 0.0f);
                    transform = CGAffineTransform.Scale(transform, -1.0f, 1.0f);
                    break;

                case UIImageOrientation.Down:                         //EXIF = 3
                    transform = CGAffineTransform.MakeTranslation(imageSize.Width, imageSize.Height);
                    transform = CGAffineTransform.Rotate(transform, (float)Math.PI);
                    break;

                case UIImageOrientation.DownMirrored:                         //EXIF = 4
                    transform = CGAffineTransform.MakeTranslation(0.0f, imageSize.Height);
                    transform = CGAffineTransform.Scale(transform, 1.0f, -1.0f);
                    break;

                case UIImageOrientation.LeftMirrored:                         //EXIF = 5
                    boundHeight = (float)bounds.Size.Height;
                    bounds.Size = new CGSize(boundHeight, (float)bounds.Size.Width);
                    transform   = CGAffineTransform.MakeTranslation(imageSize.Height, imageSize.Width);
                    transform   = CGAffineTransform.Scale(transform, -1.0f, 1.0f);
                    transform   = CGAffineTransform.Rotate(transform, 3.0f * (float)Math.PI / 2.0f);
                    break;

                case UIImageOrientation.Left:                         //EXIF = 6
                    boundHeight = (float)bounds.Size.Height;
                    bounds.Size = new CGSize(boundHeight, (float)bounds.Size.Width);
                    transform   = CGAffineTransform.MakeTranslation(0.0f, imageSize.Width);
                    transform   = CGAffineTransform.Rotate(transform, 3.0f * (float)Math.PI / 2.0f);
                    break;

                case UIImageOrientation.RightMirrored:                         //EXIF = 7
                    boundHeight = (float)bounds.Size.Height;
                    bounds.Size = new CGSize(boundHeight, (float)bounds.Size.Width);
                    transform   = CGAffineTransform.MakeScale(-1.0f, 1.0f);
                    transform   = CGAffineTransform.Rotate(transform, (float)Math.PI / 2.0f);
                    break;

                case UIImageOrientation.Right:                         //EXIF = 8
                    boundHeight = (float)bounds.Size.Height;
                    bounds.Size = new CGSize(boundHeight, (float)bounds.Size.Width);
                    transform   = CGAffineTransform.MakeTranslation(imageSize.Height, 0.0f);
                    transform   = CGAffineTransform.Rotate(transform, (float)Math.PI / 2.0f);
                    break;

                default:
                    break;
                }

                UIGraphics.BeginImageContext(bounds.Size);

                using (CGContext context = UIGraphics.GetCurrentContext())
                {
                    if (orient == UIImageOrientation.Right || orient == UIImageOrientation.Left)
                    {
                        context.ScaleCTM(-scaleRatio, scaleRatio);
                        context.TranslateCTM(-height, 0);
                    }
                    else
                    {
                        context.ScaleCTM(scaleRatio, -scaleRatio);
                        context.TranslateCTM(0, -height);
                    }

                    context.ConcatCTM(transform);

                    context.DrawImage(new CGRect(0, 0, width, height), imgRef);
                    UIImage imageCopy = UIGraphics.GetImageFromCurrentImageContext();

                    UIGraphics.EndImageContext();
                    return(imageCopy);
                }
            }
        }
Ejemplo n.º 34
0
        UIImage ChangeOrientation(UIImage rotatedImage)
        {
            float             width      = rotatedImage.CGImage.Width;
            float             height     = rotatedImage.CGImage.Height;
            CGImage           imgRef     = rotatedImage.CGImage;
            CGAffineTransform transform  = CGAffineTransform.MakeIdentity();
            CGRect            bounds     = new CGRect(0, 0, width, height);
            float             scaleRatio = (float)(bounds.Size.Width / width);
            CGSize            imageSize  = new CGSize(imgRef.Width, imgRef.Height);

            var orient = rotatedImage.Orientation;

            switch (orient)
            {
            case UIImageOrientation.Right:
                bounds.Size = new CGSize(bounds.Size.Height, bounds.Size.Width);
                transform   = CGAffineTransform.MakeTranslation(imageSize.Height, 0.0f);
                transform   = CGAffineTransform.Rotate(transform, (System.nfloat)(Math.PI / 2.0));
                break;

            case UIImageOrientation.Up:     //EXIF = 1
                transform = CGAffineTransform.MakeIdentity();
                break;

            case UIImageOrientation.UpMirrored:     //EXIF = 2
                transform = CGAffineTransform.MakeTranslation(imageSize.Width, 0.0f);
                transform = CGAffineTransform.Scale(transform, -1.0f, 1.0f);
                break;

            case UIImageOrientation.Down:     //EXIF = 3
                transform = CGAffineTransform.MakeTranslation(imageSize.Width, imageSize.Height);
                transform = CGAffineTransform.Rotate(transform, (System.nfloat)Math.PI);
                break;

            case UIImageOrientation.DownMirrored:     //EXIF = 4
                transform = CGAffineTransform.MakeTranslation(0.0f, imageSize.Height);
                transform = CGAffineTransform.Scale(transform, 1.0f, -1.0f);
                break;

            case UIImageOrientation.LeftMirrored:     //EXIF = 5
                bounds.Size = new CGSize(bounds.Size.Height, bounds.Size.Width);
                transform   = CGAffineTransform.MakeTranslation(imageSize.Height, imageSize.Width);
                transform   = CGAffineTransform.Scale(transform, -1.0f, 1.0f);
                transform   = CGAffineTransform.Rotate(transform, (System.nfloat)(3.0 * Math.PI / 2.0));
                break;

            case UIImageOrientation.Left:     //EXIF = 6
                bounds.Size = new CGSize(bounds.Size.Height, bounds.Size.Width);
                transform   = CGAffineTransform.MakeTranslation(0.0f, imageSize.Width);
                transform   = CGAffineTransform.Rotate(transform, (System.nfloat)(3.0 * Math.PI / 2.0));
                break;

            case UIImageOrientation.RightMirrored:     //EXIF = 7
                bounds.Size = new CGSize(bounds.Size.Height, bounds.Size.Width);
                transform   = CGAffineTransform.MakeScale(-1.0f, 1.0f);
                transform   = CGAffineTransform.Rotate(transform, (System.nfloat)(Math.PI / 2.0));
                break;
            }

            UIGraphics.BeginImageContext(bounds.Size);
            CGContext context = UIGraphics.GetCurrentContext();

            if (orient == UIImageOrientation.Right || orient == UIImageOrientation.Left)
            {
                context.ScaleCTM(-scaleRatio, scaleRatio);
                context.TranslateCTM(-height, 0);
            }
            else
            {
                context.ScaleCTM(scaleRatio, -scaleRatio);
                context.TranslateCTM(0, -height);
            }

            context.ConcatCTM(transform);
            context.DrawImage(new CGRect(0, 0, width, height), imgRef);
            UIImage imageCopy = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();
            NSData str = imageCopy.AsPNG();

            return(imageCopy);
        }
Ejemplo n.º 35
0
        public UIImage RotateImage(UIImage sourceImage, NSMutableDictionary imageMetadata)
        {
            try
            {
                CGImage           imgRef    = sourceImage.CGImage;
                float             width     = imgRef.Width;
                float             height    = imgRef.Height;
                CGAffineTransform transform = CGAffineTransform.MakeIdentity();
                RectangleF        bounds    = new RectangleF(0, 0, width, height);
                SizeF             imageSize = new SizeF(width, height);
                float             boundHeight;

                var orientation = ((NSNumber)imageMetadata[ImageIO.CGImageProperties.Orientation]).Int32Value;
                imageMetadata.Remove(ImageIO.CGImageProperties.Orientation);

                switch (orientation)
                {
                case 1:                                            //EXIF = 1
                    return(sourceImage);

                    break;

                case 2:                                    //EXIF = 2
                    transform = CGAffineTransform.MakeTranslation(imageSize.Width, 0f);
                    transform = CGAffineTransform.MakeScale(-1.0f, 1.0f);
                    break;

                case 3:                                          //EXIF = 3
                    transform = CGAffineTransform.MakeTranslation(imageSize.Width, imageSize.Height);
                    transform = CGAffineTransform.Rotate(transform, (float)Math.PI);
                    break;

                case 4:                                  //EXIF = 4
                    transform = CGAffineTransform.MakeTranslation(0f, imageSize.Height);
                    transform = CGAffineTransform.MakeScale(1.0f, -1.0f);
                    break;

                case 5:                                  //EXIF = 5
                    boundHeight   = bounds.Height;
                    bounds.Height = bounds.Width;
                    bounds.Width  = boundHeight;
                    transform     = CGAffineTransform.MakeTranslation(imageSize.Height, imageSize.Width);
                    transform     = CGAffineTransform.MakeScale(-1.0f, 1.0f);
                    transform     = CGAffineTransform.Rotate(transform, 3.0f * (float)Math.PI / 2.0f);
                    break;

                case 6:                                          //EXIF = 6
                    boundHeight   = bounds.Height;
                    bounds.Height = bounds.Width;
                    bounds.Width  = boundHeight;
                    transform     = CGAffineTransform.MakeTranslation(imageSize.Height, 0.0f);
                    transform     = CGAffineTransform.Rotate(transform, (float)Math.PI / 2.0f);
                    break;

                case 7:                                 //EXIF = 7
                    boundHeight   = bounds.Height;
                    bounds.Height = bounds.Width;
                    bounds.Width  = boundHeight;
                    transform     = CGAffineTransform.MakeScale(-1.0f, 1.0f);
                    transform     = CGAffineTransform.Rotate(transform, (float)Math.PI / 2.0f);
                    break;

                case 8:                                         //EXIF = 8
                    boundHeight   = bounds.Height;
                    bounds.Height = bounds.Width;
                    bounds.Width  = boundHeight;
                    transform     = CGAffineTransform.MakeTranslation(0.0f, imageSize.Width);
                    transform     = CGAffineTransform.Rotate(transform, 3.0f * (float)Math.PI / 2.0f);
                    break;

                default:
                    return(sourceImage);

                    break;
                }

                UIGraphics.BeginImageContext(bounds.Size);

                CGContext context = UIGraphics.GetCurrentContext();

                if (orientation == 8 || orientation == 6)
                {
                    context.ScaleCTM(-1, 1);
                    context.TranslateCTM(-height, 0);
                }
                else
                {
                    context.ScaleCTM(1, -1);
                    context.TranslateCTM(0, -height);
                }

                context.ConcatCTM(transform);
                context.DrawImage(new RectangleF(0, 0, width, height), imgRef);

                UIImage imageCopy = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();

                return(imageCopy);
            }
            catch (Exception e)
            {
                return(sourceImage);
            }
        }