Beispiel #1
0
        public static void CopyPath(CGPath cgpath)
        {
            var pdfpath = Path.GetTempPath() + ".pdf";
            var context = new CGContextPDF(NSUrl.FromFilename(pdfpath));

            CGRect rc = new CGRect(0, 0, 500, 500);

            context.BeginPage(rc);

            using (var gc = NSGraphicsContext.FromGraphicsPort(context, false))
            {
                var ctx = gc.CGContext;
                //ctx.SetStrokeColor(new CGColor(0, 0, 100));
                //ctx.SetLineWidth(2);
                //ctx.SetFillColor(new CGColor(100, 0, 0));

                ctx.AddPath(cgpath);
                ctx.DrawPath(CGPathDrawingMode.FillStroke);
            }

            context.EndPage();
            context.Close();

            var data = NSData.FromFile(pdfpath);

            NSPasteboard.GeneralPasteboard.ClearContents();
            NSPasteboard.GeneralPasteboard.SetDataForType(data, NSPasteboard.NSPdfType);
        }
Beispiel #2
0
        public GraphicsHandler(NSView view)
        {
            this.view       = view;
            graphicsContext = NSGraphicsContext.FromWindow(view.Window);
            graphicsContext = graphicsContext.IsFlipped ? graphicsContext : NSGraphicsContext.FromGraphicsPort(graphicsContext.GraphicsPortHandle, true);
            disposeContext  = true;
            Control         = graphicsContext.GraphicsPort;

            view.PostsFrameChangedNotifications = true;
            AddObserver(NSView.FrameChangedNotification, FrameDidChange, view);

            // if control is in a scrollview, we need to trap when it's scrolled as well
            var parent = view.Superview;

            while (parent != null)
            {
                var scroll = parent as NSScrollView;
                if (scroll != null)
                {
                    scroll.ContentView.PostsBoundsChangedNotifications = true;
                    AddObserver(NSView.BoundsChangedNotification, FrameDidChange, scroll.ContentView);
                }
                parent = parent.Superview;
            }

            SetDefaults();
            InitializeContext(view.IsFlipped);
        }
Beispiel #3
0
        public void CreateFromImage(Bitmap image)
        {
            var handler = image.Handler as BitmapHandler;

            SourceImage = image;
#if OSX
            var rep = handler.Control.Representations().OfType <NSBitmapImageRep>().FirstOrDefault();
            if (rep.BitsPerPixel != 32)
            {
                // CoreGraphics only supports drawing to 32bpp, create a new 32-bpp image and copy back when disposed or flushed.
                DrawingImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppRgb);
                handler      = DrawingImage.Handler as BitmapHandler;
                rep          = handler.Control.Representations().OfType <NSBitmapImageRep>().FirstOrDefault();
            }
            graphicsContext = NSGraphicsContext.FromBitmap(rep);

            graphicsContext = graphicsContext.IsFlipped ? graphicsContext : NSGraphicsContext.FromGraphicsPort(graphicsContext.GraphicsPortHandle, true);
            disposeContext  = true;
            Control         = graphicsContext.GraphicsPort;
            PointsPerPixel  = (float)(rep.PixelsWide / handler.Control.Size.Width);
#elif IOS
            var cgimage = handler.Control.CGImage;
            Control        = new CGBitmapContext(handler.Data.MutableBytes, cgimage.Width, cgimage.Height, cgimage.BitsPerComponent, cgimage.BytesPerRow, cgimage.ColorSpace, cgimage.BitmapInfo);
            PointsPerPixel = (float)(cgimage.Width / handler.Control.Size.Width);
#endif

            height = image.Size.Height;
            SetDefaults();
            InitializeContext(true);
            if (DrawingImage != null && SourceImage != null)
            {
                // draw source image onto context, when source is incompatible for CoreGraphics drawing.
                DrawImage(SourceImage, 0, 0);
            }
        }
Beispiel #4
0
        public void DrawLayer(CALayer layer, CoreGraphics.CGContext context)
        {
            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext graphicsContext = NSGraphicsContext.FromGraphicsPort(context, true);

            NSGraphicsContext.CurrentContext = graphicsContext;

            NSBezierPath path = new NSBezierPath();
            //ベジェ曲線
            var x1 = this.Frame.Left;
            var y1 = this.Frame.Top;
            var x2 = this.Frame.Right;
            var y2 = this.Frame.Bottom;

            path.MoveTo(new CoreGraphics.CGPoint(x1, y1));
            path.CurveTo(new CoreGraphics.CGPoint(x2, y1),
                         new CoreGraphics.CGPoint(x1, y2),
                         new CoreGraphics.CGPoint(x2, y2));
            //背景は白
            NSColor.White.Set();
            path.Fill();
            //線は青
            NSColor.Blue.Set();
            //線の太さ
            path.LineWidth = 2;
            path.Stroke();

            NSGraphicsContext.GlobalRestoreGraphicsState();
        }
Beispiel #5
0
        Graphics(NSGraphicsContext context)
        {
            var gc = context;

            if (gc.IsFlipped)
            {
                gc = NSGraphicsContext.FromGraphicsPort(gc.GraphicsPort, false);
            }

            screenScale  = 1;
            nativeObject = gc;
            isFlipped    = gc.IsFlipped;
            InitializeContext(gc.GraphicsPort);
        }
Beispiel #6
0
        public void CreateFromImage(Bitmap image)
        {
            var handler = image.Handler as BitmapHandler;

#if OSX
            var rep = handler.Control.Representations().OfType <NSBitmapImageRep>().FirstOrDefault();
            graphicsContext = NSGraphicsContext.FromBitmap(rep);
            graphicsContext = graphicsContext.IsFlipped ? graphicsContext : NSGraphicsContext.FromGraphicsPort(graphicsContext.GraphicsPortHandle, true);
            disposeContext  = true;
            Control         = graphicsContext.GraphicsPort;
            scale           = (float)(rep.PixelsWide / handler.Control.Size.Width);
#elif IOS
            var cgimage = handler.Control.CGImage;
            Control = new CGBitmapContext(handler.Data.MutableBytes, cgimage.Width, cgimage.Height, cgimage.BitsPerComponent, cgimage.BytesPerRow, cgimage.ColorSpace, cgimage.BitmapInfo);
            scale   = cgimage.Width / handler.Control.Size.Width;
#endif

            height = image.Size.Height;
            SetDefaults();
            InitializeContext(true);
        }
Beispiel #7
0
        public void DrawText(string text)
        {
            Ensure();

            var gc = NSGraphicsContext.FromGraphicsPort(context, false);

            gc.SaveGraphicsState();
            NSGraphicsContext.CurrentContext = gc;

            var str  = new NSAttributedString(text, foregroundColor: Color, font: Font);
            var size = str.Size;

            var box = Bounds;

            switch (HAlign)
            {
            case NSTextAlignment.Right:
                box.X += box.Width - size.Width;
                break;

            case NSTextAlignment.Center:
                box.X += (box.Width - size.Width) / 2;
                break;
            }

            switch (VAlign)
            {
            case NSTextAlignment.Left:
                box.Y += box.Height - size.Height;
                break;

            case NSTextAlignment.Center:
                box.Y += (box.Height - size.Height) / 2;
                break;
            }

            str.DrawString(box.Location);

            NSGraphicsContext.GlobalRestoreGraphicsState();
        }
Beispiel #8
0
 public GraphicsHandler(NSView view, NSGraphicsContext graphicsContext, float height, bool flipped)
 {
     DisplayView          = view;
     this.height          = height;
     this.graphicsContext = flipped != graphicsContext.IsFlipped ? graphicsContext : NSGraphicsContext.FromGraphicsPort(graphicsContext.GraphicsPortHandle, true);
     Control = this.graphicsContext.GraphicsPort;
     SetDefaults();
     InitializeContext(!flipped);
 }