Frage

Can somebody help me with the following code snippet to capture part of or the whole desktop on OSX ? I would like to specify the upper-left corner coordinates (x,y) and the width (w) and height (h) of the rectangle that defines the capture.

It's for a C# MonoMac application on OSX.

This is what I've done:

int windowNumber = 2;
System.Drawing.RectangleF bounds = new RectangleF(0,146,320,157);

CGImage screenImage = MonoMac.CoreGraphics.CGImage.ScreenImage(windowNumber,bounds);

MonoMac.Foundation.NSData bitmapData = screenImage.DataProvider.CopyData();

It looks like I have the bitmap data in 'bitmapData', but I'm not sure how I convert the NSData instance 'bitmapData' to an actual Bitmap; i.e. :

Bitmap screenCapture = ????

The documentation is really sparse and I've googled for examples without luck. So I'm hoping that there's a kind MonoMac expert out there who can point me in the right direction? - An example would be nice :o)

Thank you in advance!

War es hilfreich?

Lösung

This will give you the bytes of your capture in a .NET byte[], from where you can create a Bitmap or Image or whatever you want. Might not be exactly what you are looking for but should put you in the right direction.

        int windowNumber = 2; System.Drawing.RectangleF bounds = new RectangleF(0,146,320,157);

        CGImage screenImage = MonoMac.CoreGraphics.CGImage.ScreenImage(windowNumber,bounds);

        using(NSBitmapImageRep imageRep = new NSBitmapImageRep(screenImage))
        {
            NSDictionary properties = NSDictionary.FromObjectAndKey(new NSNumber(1.0), new NSString("NSImageCompressionFactor"));
            using(NSData tiffData = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Png, properties))
            {
                byte[] imageBytes;

                using(var ms = new MemoryStream())
                {
                    tiffData.AsStream().CopyTo(ms);
                    imageBytes = ms.ToArray();
                }
            }
        }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top