public static ImageSource FromResource(string resource)
        {
            ImageSource imageSource;
            WeakReference<ImageSource> imageSourceWr;

            if (!ImageSourceWrCache.TryGetValue(resource, out imageSourceWr) ||
                !imageSourceWr.TryGetTarget(out imageSource))
            {
                var imgArrayLazyTask = new Lazy<Task<byte[]>>(
                    async () =>
                    {
                        var assembly = typeof(ImageSourceEx).GetTypeInfo().Assembly;

                        using (var inputStream = assembly.GetManifestResourceStream(resource))
                        using (var outputStream = new MemoryStream())
                        {
                            await inputStream.CopyToAsync(outputStream);
                            return outputStream.ToArray();
                        }
                    },
                    isThreadSafe: true);

                imageSource = new StreamImageSource() { Stream = async _ => new MemoryStream(await imgArrayLazyTask.Value) };
                ImageSourceWrCache[resource] = new WeakReference<ImageSource>(imageSource);
            }

            return imageSource;
        }
Пример #2
0
        public async Task InitializeAsync()
        {
            //Using the built-in FileImageSource would be much less code than manually creating
            //a StreamImageSource. However, the FileImageSource doesn't appear to release the
            //lock on the file that it displays, so attempting to delete the file causes an
            //"Access Denied" error in the UWP app. So here we copy the file into memory and
            //immediately dispose the FileStream.

            var file = await FileSystem.Current.GetFileFromPathAsync(FileReference.FileName);
            byte[] fileBytes;
            using (var fileStream = await file.OpenAsync(FileAccess.Read))
            {
                fileBytes = new byte[fileStream.Length];
                fileStream.Read(fileBytes, 0, (int)(fileStream.Length));
            }
            Func<System.Threading.CancellationToken, Task<System.IO.Stream>> getStreamFunc =
                ct => Task.FromResult((System.IO.Stream)(new System.IO.MemoryStream(fileBytes)));
            ImageSource = new StreamImageSource() { Stream = getStreamFunc };
        }
Пример #3
0
        async void OnPickPhotoButtonClicked(object sender, EventArgs e)
        {
            (sender as Button).IsEnabled = false;
            Xamarin.Forms.Image image = new Image();

            Stream stream = await DependencyService.Get <CameraInterface>().GetImageStreamAsync();

            if (stream != null)
            {
                image.Source = ImageSource.FromStream(() => stream);
                if (image.Source is Xamarin.Forms.StreamImageSource)
                {
                    Xamarin.Forms.StreamImageSource objFileImageSource = (Xamarin.Forms.StreamImageSource)image.Source;
                    //
                    // Access the file that was specified:-
                    string strFileName = objFileImageSource.ToString();
                }
            }

            (sender as Button).IsEnabled = true;
        }
Пример #4
0
 private async Task<Stream> GetStreamFromImageSourceAsync(StreamImageSource imageSource, CancellationToken canellationToken = default(CancellationToken))
 {
     if (imageSource.Stream != null)
     {
         return await imageSource.Stream(canellationToken);
     }
     return null;
 }