Пример #1
0
        public Task <IBitmap> Load(Stream sourceStream, float?desiredWidth, float?desiredHeight)
        {
            var data = NSData.FromStream(sourceStream);

            var tcs = new TaskCompletionSource <IBitmap>();

#if UIKIT
            NSRunLoop.InvokeInBackground(() =>
            {
                try
                {
                    var bitmap = UIImage.LoadFromData(data);
                    if (bitmap == null)
                    {
                        throw new Exception("Failed to load image");
                    }

                    tcs.TrySetResult(new CocoaBitmap(bitmap));
                }
                catch (Exception ex)
                {
                    LogHost.Default.Debug(ex, "Unable to parse the known colour name.");
                    tcs.TrySetException(ex);
                }
            });
#else
            tcs.TrySetResult(new CocoaBitmap(new UIImage(data)));
#endif
            return(tcs.Task);
        }
Пример #2
0
        /// <inheritdoc />
        public Task <IBitmap> LoadFromResource(string source, float?desiredWidth, float?desiredHeight)
        {
            var tcs = new TaskCompletionSource <IBitmap>();

            NSRunLoop.Main.BeginInvokeOnMainThread(() =>
            {
                try
                {
#if UIKIT
                    var bitmap = UIImage.FromBundle(source);
#else
                    var bitmap = UIImage.ImageNamed(source);
#endif
                    if (bitmap == null)
                    {
                        throw new Exception("Failed to load image from resource: " + source);
                    }

                    tcs.TrySetResult(new CocoaBitmap(bitmap));
                }
                catch (Exception ex)
                {
                    tcs.TrySetException(ex);
                }
            });
            return(tcs.Task);
        }
Пример #3
0
        /// <inheritdoc />
        public Task <IBitmap> Load(Stream sourceStream, float?desiredWidth, float?desiredHeight)
        {
            var data = NSData.FromStream(sourceStream);

            var tcs = new TaskCompletionSource <IBitmap>();

            NSRunLoop.Main.BeginInvokeOnMainThread(() =>
            {
                try
                {
#if UIKIT
                    var bitmap = UIImage.LoadFromData(data);
                    if (bitmap == null)
                    {
                        throw new Exception("Failed to load image");
                    }

                    tcs.TrySetResult(new CocoaBitmap(bitmap));
#else
                    tcs.TrySetResult(new CocoaBitmap(new UIImage(data)));
#endif
                }
                catch (Exception ex)
                {
                    tcs.TrySetException(ex);
                }
            });
            return(tcs.Task);
        }
Пример #4
0
        /// <summary>
        /// Converts a <see cref="UIImage"/> to a splat <see cref="IBitmap"/>.
        /// </summary>
        /// <param name="value">The native bitmap to convert from.</param>
        /// <param name="copy">Whether to copy the android bitmap or not.</param>
        /// <returns>A <see cref="IBitmap"/> bitmap.</returns>
        public static IBitmap FromNative(this UIImage value, bool copy = false)
        {
            if (copy)
            {
                return(new CocoaBitmap((UIImage)value.Copy()));
            }

            return(new CocoaBitmap(value));
        }
Пример #5
0
        /// <summary>
        /// Converts a <see cref="UIImage"/> to a splat <see cref="IBitmap"/>.
        /// </summary>
        /// <param name="value">The native bitmap to convert from.</param>
        /// <param name="copy">Whether to copy the android bitmap or not.</param>
        /// <returns>A <see cref="IBitmap"/> bitmap.</returns>
        public static IBitmap FromNative(this UIImage value, bool copy = false)
        {
            if (value is null)
            {
                throw new System.ArgumentNullException(nameof(value));
            }

            if (copy)
            {
                return(new CocoaBitmap((UIImage)value.Copy()));
            }

            return(new CocoaBitmap(value));
        }
        public object GenerateGlyph(ITextViewLine line, IGlyphTag tag)
        {
            if (!(tag is T))
            {
                return(null);
            }
#if MAC
            if (nsImageCache == null)
            {
                nsImageCache = (AppKit.NSImage)imageService.GetImage(imageId);
            }
            var imageView = AppKit.NSImageView.FromImage(nsImageCache);
            imageView.SetFrameSize(imageView.FittingSize);
            return(imageView);
#else
            throw new NotImplementedException();
#endif
        }
Пример #7
0
        public Task <IBitmap> LoadFromResource(string source, float?desiredWidth, float?desiredHeight)
        {
            var tcs = new TaskCompletionSource <IBitmap>();

#if UIKIT
            NSRunLoop.InvokeInBackground(() =>
            {
                try
                {
                    var bitmap = UIImage.FromBundle(source);
                    if (bitmap == null)
                    {
                        throw new Exception("Failed to load image from resource: " + source);
                    }

                    tcs.TrySetResult(new CocoaBitmap(bitmap));
                }
                catch (Exception ex)
                {
                    LogHost.Default.Debug(ex, "Unable to parse the known colour name.");
                    tcs.TrySetException(ex);
                }
            });
#else
            NSRunLoop.Main.BeginInvokeOnMainThread(() =>
            {
                try
                {
                    var bitmap = UIImage.ImageNamed(source);
                    if (bitmap == null)
                    {
                        throw new Exception("Failed to load image from resource: " + source);
                    }

                    tcs.TrySetResult(new CocoaBitmap(bitmap));
                }
                catch (Exception ex)
                {
                    tcs.TrySetException(ex);
                }
            });
#endif
            return(tcs.Task);
        }
Пример #8
0
        private protected override bool TryOpenSourceSync(out _NativeImage image)
        {
            // Convert RGB colorspace.
            var bgraBuffer = _buffer.Data;
            var rgbaBuffer = new byte[bgraBuffer.Length];

            for (var i = 0; i < bgraBuffer.Length; i += 4)
            {
                rgbaBuffer[i + 3] = bgraBuffer[i + 3];                 // a
                rgbaBuffer[i + 0] = bgraBuffer[i + 2];                 // r
                rgbaBuffer[i + 1] = bgraBuffer[i + 1];                 // g
                rgbaBuffer[i + 2] = bgraBuffer[i + 0];                 // b
            }

            using (var dataProvider = new CGDataProvider(rgbaBuffer, 0, rgbaBuffer.Length))
                using (var colorSpace = CGColorSpace.CreateDeviceRGB())
                {
                    const int bitsPerComponent = 8;
                    const int bytesPerPixel    = 4;

                    var img = new CGImage(
                        PixelWidth,
                        PixelHeight,
                        bitsPerComponent: bitsPerComponent,
                        bitsPerPixel: bitsPerComponent * bytesPerPixel,
                        bytesPerRow: bytesPerPixel * PixelWidth,
                        colorSpace,
                        CGImageAlphaInfo.Last,
                        dataProvider,
                        decode: null,
                        shouldInterpolate: false,
                        CGColorRenderingIntent.Default);

#if __MACOS__
                    image = new _NativeImage(img, new CGSize(PixelWidth, PixelHeight));
#else
                    image = _NativeImage.FromImage(img);
#endif
                    return(true);
                }
        }
Пример #9
0
        public static void TestCapture()
        {
            Foundation.NSNumber mainScreen = (Foundation.NSNumber)AppKit.NSScreen.MainScreen.DeviceDescription["NSScreenNumber"];

            using (CoreGraphics.CGImage cgImage = CreateImage(mainScreen.UInt32Value))
            {
                // https://stackoverflow.com/questions/17334786/get-pixel-from-the-screen-screenshot-in-max-osx/17343305#17343305

                // Get byte-array from CGImage
                // https://gist.github.com/zhangao0086/5fafb1e1c0b5d629eb76

                AppKit.NSBitmapImageRep bitmapRep = new AppKit.NSBitmapImageRep(cgImage);

                // var imageData = bitmapRep.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [:])
                Foundation.NSData imageData = bitmapRep.RepresentationUsingTypeProperties(AppKit.NSBitmapImageFileType.Png);

                long   len   = imageData.Length;
                byte[] bytes = new byte[len];
                System.Runtime.InteropServices.GCHandle pinnedArray = System.Runtime.InteropServices.GCHandle.Alloc(bytes, System.Runtime.InteropServices.GCHandleType.Pinned);
                System.IntPtr pointer = pinnedArray.AddrOfPinnedObject();
                // Do your stuff...
                imageData.GetBytes(pointer, new System.IntPtr(len));
                pinnedArray.Free();

                using (AppKit.NSImage nsImage = new AppKit.NSImage(cgImage, new System.Drawing.SizeF(cgImage.Width, cgImage.Height)))
                {
                    // ImageView.Image = nsImage;
                    // And now ? How to get the image bytes ?

                    // https://theconfuzedsourcecode.wordpress.com/2016/02/24/convert-android-bitmap-image-and-ios-uiimage-to-byte-array-in-xamarin/
                    // https://stackoverflow.com/questions/5645157/nsimage-from-byte-array
                    // https://stackoverflow.com/questions/53060723/nsimage-source-from-byte-array-cocoa-app-xamarin-c-sharp
                    // https://gist.github.com/zhangao0086/5fafb1e1c0b5d629eb76
                    // https://www.quora.com/What-is-a-way-to-convert-UIImage-to-a-byte-array-in-Swift?share=1
                    // https://stackoverflow.com/questions/17112314/converting-uiimage-to-byte-array
                } // End Using nsImage
            }     // End Using cgImage
        }         // End Sub TestCapture
Пример #10
0
        public Image(AppKit.NSImage img)
        {
            var tmp = new CoreGraphics.CGRect();

            Init(img.AsCGImage(ref tmp, null, null));
        }