public async void Share(string subject, string message, SKBitmap image)
        {
            var intent = new Intent(Intent.ActionSend);

            //intent.PutExtra(Intent.ExtraSubject, subject);
            intent.PutExtra(Intent.ExtraText, message);
            intent.SetType("image/png");

            var imageSource = new SKBitmapImageSource {
                Bitmap = image
            };
            var handler = GetHandler(imageSource);
            var bitmap  = await handler.LoadImageAsync(imageSource, this);

            var path = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads
                                                                     + Java.IO.File.Separator + "shareImage.png");

            using (var stream = new System.IO.FileStream(path.AbsolutePath, System.IO.FileMode.Create))
            {
                bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
            }

            intent.PutExtra(Intent.ExtraStream, Android.Net.Uri.FromFile(path));
            Forms.Context.StartActivity(Intent.CreateChooser(intent, "Share Image"));
        }
示例#2
0
        public static Task <SKImage?> ToSKImageAsync(this ImageSource imageSource, CancellationToken cancellationToken = default)
        {
            if (imageSource == null)
            {
                throw new ArgumentNullException(nameof(imageSource));
            }

            return(imageSource switch
            {
                // 1. first try SkiaSharp sources
                SKImageImageSource iis => FromSkia(iis.Image),
                SKBitmapImageSource bis => FromSkia(SKImage.FromBitmap(bis.Bitmap)),
                SKPixmapImageSource xis => FromSkia(SKImage.FromPixels(xis.Pixmap)),
                SKPictureImageSource pis => FromSkia(SKImage.FromPicture(pis.Picture, pis.Dimensions)),

                // 2. then try Stream sources
                StreamImageSource stream => FromStream(stream.Stream.Invoke(cancellationToken)),
                UriImageSource uri => FromStream(uri.GetStreamAsync(cancellationToken)),

                // 3. finally, use the handlers
                FileImageSource file => FromHandler(PlatformToSKImageAsync(file, cancellationToken)),
                FontImageSource font => FromHandler(PlatformToSKImageAsync(font, cancellationToken)),

                // 4. all is lost
                _ => throw new ArgumentException("Unable to determine the type of image source.", nameof(imageSource))
            });
        public async Task BitmapReturnsSimilarImage()
        {
            using var bitmap = new SKBitmap(new SKImageInfo(100, 100));

            var source = new SKBitmapImageSource {
                Bitmap = bitmap
            };

            var result = await source.ToSKImageAsync();

            Assert.NotNull(result);

            var resultInfo = new SKImageInfo(result.Width, result.Height, result.ColorType, result.AlphaType, result.ColorSpace);

            Assert.Equal(bitmap.Info, resultInfo);
        }
            public static SKBitmapImageSource Load(string source, Aspect aspect, double width, double height)
            {
                // No image needed?
                if (string.IsNullOrWhiteSpace(source) || width <= 0 || height <= 0)
                {
                    return(null);
                }

                // Read the image
                string key = GetKey(source);

                SkiaSharp.Extended.Svg.SKSvg svg = LoadSVG(key);
                CalculateBounds(true, svg, aspect, width, height, out SKMatrix drawMatrix, out SKSizeI pixelSize);

                // Check the cache
                if (ImageSourceCache.TryGetValue(key, out Tuple <SKSizeI, SKBitmapImageSource> cacheEntry))
                {
                    if (cacheEntry.Item1.Width >= pixelSize.Width * (1 - CacheTolerance) &&
                        cacheEntry.Item1.Height >= pixelSize.Height * (1 - CacheTolerance))
                    {
                        // Already cached
                        return(cacheEntry.Item2);
                    }
                    else
                    {
                        // Dispose/remove current entry
                        cacheEntry.Item2.Bitmap?.Dispose();
                        ImageSourceCache.Remove(key);
                    }
                }

                // Convert to an SKBitmapImageSource
                using (SKImage image = SKImage.FromPicture(svg.Picture, pixelSize, drawMatrix))
                {
                    SKBitmapImageSource imageSource = new SKBitmapImageSource()
                    {
                        Bitmap = SKBitmap.FromImage(image)
                    };
                    ImageSourceCache.Add(key, Tuple.Create(pixelSize, imageSource));
                    return(imageSource);
                }
            }