private void SetImage() { if (ImageBitmap != null) { SetImageDrawable(mImageBitmap.ToNative()); SetScaleType(ScaleType.FitXy); } else if (!String.IsNullOrEmpty(mImageUrl)) { DestroyDrawingCache(); this.SetUrlDrawable(mImageUrl, Resources.GetDrawable(Android.Resource.Color.Transparent)); //SetScaleType(ScaleType.FitXy); } else if (!string.IsNullOrEmpty(PathImage)) { Drawable drawable = Drawable.CreateFromPath(PathImage); SetImageDrawable(drawable); //SetScaleType(ScaleType.FitXy); } else { SetImageResource(Android.Resource.Color.Transparent); } }
private async Task GetThumbnailAsync() { Uri artworkUrl = this.soundCloudModel.ArtworkUrl; if (artworkUrl == null) { return; } // Get a non-shitty resolution <c> // https: //developers.soundcloud.com/docs/api/reference#tracks <c/> artworkUrl = new Uri(artworkUrl.ToString().Replace("large", "t300x300")); this.IsLoadingThumbnail = true; try { IBitmap image = await BlobCache.InMemory.LoadImageFromUrl(artworkUrl.ToString(), absoluteExpiration : DateTimeOffset.Now + TimeSpan.FromMinutes(60)); this.Thumbnail = image.ToNative(); } catch (Exception ex) { this.Log().ErrorException("Failed to download SoundCloud artwork", ex); } this.IsLoadingThumbnail = false; }
private void updateView() { if (adapter != null) { adapter.NotifyDataSetChanged(); } if (headerView != null && today != null && profile != null) { headerView.FindViewById <TextView> (Resource.Id.textView1).Text = profile.user.full_name; } if (totalTextView != null && today != null) { totalTextView.StartAnimation(fadeoutAnimation); totalTextView.Text = today.challenge_profile.total_score.ToString(); totalTextView.StartAnimation(fadeinAnimation); } if (profileImageView != null && profileImage != null) { profileImageView.StartAnimation(fadeoutAnimation); profileImageView.SetImageDrawable((BitmapDrawable)profileImage.ToNative()); profileImageView.StartAnimation(fadeinAnimation); } }
public XImage CreateImage(double x, double y, double width, double height, byte[] data) { Image image = new Image(); // enable high quality image scaling RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.HighQuality); // store original image data is Tag property image.Tag = data; // opacity mask is used for determining selection state image.OpacityMask = NormalBrush; //using(var ms = new System.IO.MemoryStream(data)) //{ // image = Image.FromStream(ms); //} using (var ms = new System.IO.MemoryStream(data)) { IBitmap profileImage = BitmapLoader.Current.Load(ms, null, null).Result; image.Source = profileImage.ToNative(); } image.Width = width; image.Height = height; Canvas.SetLeft(image, x); Canvas.SetTop(image, y); var ximage = new XImage(image); return(ximage); }
private async Task <BitmapSource> LoadArtworkAsync(string key) { try { IBitmap img = await ArtworkCache.Instance.Retrieve(key, 50, orderHint); return(img.ToNative()); } catch (KeyNotFoundException ex) { this.Log().WarnException(String.Format("Could not find key {0} of album cover. This reeks like a threading problem", key), ex); return(null); } catch (ArtworkCacheException ex) { this.Log().ErrorException(String.Format("Unable to load artwork with key {0} from cache", key), ex); return(null); } catch (Exception ex) { this.Log().InfoException(String.Format("Akavache threw an error on artist cover loading for key {0}", key), ex); return(null); } }
public override void ShowImage(IBitmap image, string message, int timeoutMillis) { var activity = this.TopActivityFunc(); activity.RunOnUiThread(() => AndHUD.Shared.ShowImage(activity, image.ToNative(), message, AndroidHUD.MaskType.Black, TimeSpan.FromMilliseconds(timeoutMillis)) ); }
public static async void SetImageURL(this ImageView imageView, string url) { using (IBitmap bmp = await App1.Shared.ImageDownloadService.DownloadImage(url, imageView.Width, imageView.Height)) { if (bmp != null) { using (var nativeBmp = bmp.ToNative()) imageView.SetImageDrawable(nativeBmp); } } }
private async Task <BitmapSource> LoadArtworkAsync(string key) { try { IBitmap img = await ArtworkCache.Instance.Retrieve(key, 50, orderHint); return(img.ToNative()); } catch (KeyNotFoundException ex) { this.Log().WarnException("Could not find key \{key} of album cover. This reeks like a threading problem", ex); return(null); }
protected override async Task <BitmapHolder> TransformAsync(BitmapHolder bitmap, IList <ITransformation> transformations, string path, ImageSource source, bool isPlaceholder) { await StaticLocks.DecodingLock.WaitAsync(CancellationTokenSource.Token).ConfigureAwait(false); // Applying transformations is both CPU and memory intensive ThrowIfCancellationRequested(); try { foreach (var transformation in transformations) { ThrowIfCancellationRequested(); var old = bitmap; try { IBitmap bitmapHolder = transformation.Transform(bitmap, path, source, isPlaceholder, Key); bitmap = bitmapHolder.ToNative(); } catch (Exception ex) { Logger.Error(string.Format("Transformation failed: {0}", transformation.Key), ex); throw; } finally { if (old != null && old != bitmap && old.PixelData != bitmap.PixelData) { old.FreePixels(); old = null; } } } } finally { StaticLocks.DecodingLock.Release(); } return(bitmap); }
private async Task GetThumbnailAsync() { Uri thumbnailUrl = ((YoutubeSong)this.Model).ThumbnailSource; thumbnailUrl = new Uri(thumbnailUrl.ToString().Replace("default", "hqdefault")); this.IsLoadingThumbnail = true; try { IBitmap image = await BlobCache.LocalMachine.LoadImageFromUrl(thumbnailUrl.ToString(), absoluteExpiration : DateTimeOffset.Now + TimeSpan.FromMinutes(60)); this.Thumbnail = image.ToNative(); } catch (Exception ex) { this.Log().ErrorException("Failed to download YouTube artwork", ex); } this.IsLoadingThumbnail = false; }
protected virtual ImageView GetIcon(IBitmap icon) { var layout = new LinearLayout.LayoutParams(this.DpToPixels(24), this.DpToPixels(24)) { TopMargin = this.DpToPixels(8), BottomMargin = this.DpToPixels(8), LeftMargin = this.DpToPixels(16), RightMargin = this.DpToPixels(16), Gravity = GravityFlags.Center }; var img = new ImageView(this.Activity) { LayoutParameters = layout }; if (icon != null) { img.SetImageDrawable(icon.ToNative()); } return(img); }
protected virtual void Show(IBitmap image, string message, Color bgColor, int timeoutMillis) { var stack = new StackPanel { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Opacity = 0.7 }; if (image != null) { var source = image.ToNative(); stack.Children.Add(new Image { Source = source }); } stack.Children.Add(new TextBlock { Text = message, FontSize = 24f, TextAlignment = TextAlignment.Center, FontWeight = FontWeights.Bold }); var cd = new ContentDialog { Background = new SolidColorBrush(bgColor.ToNative()), BorderBrush = new SolidColorBrush(bgColor.ToNative()), Content = stack }; stack.Tapped += (sender, args) => cd.Hide(); this.Dispatch(() => cd.ShowAsync()); Task.Delay(TimeSpan.FromMilliseconds(timeoutMillis)) .ContinueWith(x => this.Dispatch(cd.Hide)); }
protected override async Task <BitmapHolder> TransformAsync(BitmapHolder bitmap, IList <ITransformation> transformations, string path, ImageSource source, bool isPlaceholder) { await StaticLocks.DecodingLock.WaitAsync(base.CancellationTokenSource.Token).ConfigureAwait(continueOnCapturedContext: false); ThrowIfCancellationRequested(); try { foreach (ITransformation transformation in transformations) { ThrowIfCancellationRequested(); BitmapHolder old = bitmap; try { IBitmap bitmapHolder = transformation.Transform(bitmap, path, source, isPlaceholder, base.Key); bitmap = bitmapHolder.ToNative(); } catch (Exception ex2) { Exception ex = ex2; base.Logger.Error($"Transformation failed: {transformation.Key}", ex); throw; } finally { if (old != null && old != bitmap && old.PixelData != bitmap.PixelData) { old.FreePixels(); } } } } finally { StaticLocks.DecodingLock.Release(); } return(bitmap); }
public IBitmap Transform(IBitmap source) { return(new BitmapHolder(Transform(source.ToNative()))); }
public override void ShowImage(IBitmap image, string message, int timeoutMillis) { UIApplication.SharedApplication.InvokeOnMainThread(() => BTProgressHUD.ShowImage(image.ToNative(), message, timeoutMillis) ); }
public override void ShowImage(IBitmap image, string message, int timeoutMillis) => BTProgressHUD.ShowImage(image.ToNative(), message, timeoutMillis);
protected async override Task <WriteableBitmap> GenerateImageAsync(string path, ImageSource source, Stream imageData, ImageInformation imageInformation, bool enableTransformations, bool isPlaceholder) { BitmapHolder imageIn = null; if (imageData == null) { throw new ArgumentNullException(nameof(imageData)); } ThrowIfCancellationRequested(); try { // Special case to handle WebP decoding on Windows string ext = null; if (!string.IsNullOrWhiteSpace(path) && Uri.IsWellFormedUriString(path, UriKind.RelativeOrAbsolute)) { if (source == ImageSource.Url) { ext = Path.GetExtension(new Uri(path).LocalPath).ToLowerInvariant(); } else { ext = Path.GetExtension(path).ToLowerInvariant(); } } bool allowUpscale = Parameters.AllowUpscale ?? Configuration.AllowUpscale; if (source != ImageSource.Stream && ext == ".webp") { throw new NotImplementedException("Webp is not implemented on Windows"); } else if (enableTransformations && Parameters.Transformations != null && Parameters.Transformations.Count > 0) { imageIn = await imageData.ToBitmapHolderAsync(Parameters.DownSampleSize, Parameters.DownSampleUseDipUnits, Parameters.DownSampleInterpolationMode, allowUpscale, imageInformation).ConfigureAwait(false); } else { return(await imageData.ToBitmapImageAsync(Parameters.DownSampleSize, Parameters.DownSampleUseDipUnits, Parameters.DownSampleInterpolationMode, allowUpscale, imageInformation).ConfigureAwait(false)); } } finally { imageData?.Dispose(); } ThrowIfCancellationRequested(); if (enableTransformations && Parameters.Transformations != null && Parameters.Transformations.Count > 0) { var transformations = Parameters.Transformations.ToList(); await _decodingLock.WaitAsync().ConfigureAwait(false); // Applying transformations is both CPU and memory intensive try { foreach (var transformation in transformations) { ThrowIfCancellationRequested(); var old = imageIn; try { IBitmap bitmapHolder = transformation.Transform(imageIn, path, source, isPlaceholder, Key); imageIn = bitmapHolder.ToNative(); } catch (Exception ex) { Logger.Error(string.Format("Transformation failed: {0}", transformation.Key), ex); throw; } finally { if (old != null && old != imageIn && old.PixelData != imageIn.PixelData) { old.FreePixels(); old = null; } } } } finally { _decodingLock.Release(); } } try { return(await imageIn.ToBitmapImageAsync()); } finally { imageIn.FreePixels(); imageIn = null; } }
public IBitmap Transform(IBitmap source) { return new BitmapHolder(Transform(source.ToNative())); }
protected async override Task <WriteableBitmap> GenerateImageAsync(string path, ImageSource source, Stream imageData, ImageInformation imageInformation, bool enableTransformations, bool isPlaceholder) { BitmapHolder imageIn = null; if (imageData == null) { throw new ArgumentNullException(nameof(imageData)); } ThrowIfCancellationRequested(); try { bool allowUpscale = Parameters.AllowUpscale ?? Configuration.AllowUpscale; if (source != ImageSource.Stream && imageInformation.Type == ImageInformation.ImageType.WEBP) { throw new NotImplementedException("Webp is not implemented on Windows"); } else if (enableTransformations && Parameters.Transformations != null && Parameters.Transformations.Count > 0) { imageIn = await imageData.ToBitmapHolderAsync(Parameters.DownSampleSize, Parameters.DownSampleUseDipUnits, Parameters.DownSampleInterpolationMode, allowUpscale, imageInformation).ConfigureAwait(false); } else { return(await imageData.ToBitmapImageAsync(Parameters.DownSampleSize, Parameters.DownSampleUseDipUnits, Parameters.DownSampleInterpolationMode, allowUpscale, imageInformation).ConfigureAwait(false)); } } finally { imageData.TryDispose(); } ThrowIfCancellationRequested(); if (enableTransformations && Parameters.Transformations != null && Parameters.Transformations.Count > 0) { var transformations = Parameters.Transformations.ToList(); await _decodingLock.WaitAsync(CancellationTokenSource.Token).ConfigureAwait(false); // Applying transformations is both CPU and memory intensive ThrowIfCancellationRequested(); try { foreach (var transformation in transformations) { ThrowIfCancellationRequested(); var old = imageIn; try { IBitmap bitmapHolder = transformation.Transform(imageIn, path, source, isPlaceholder, Key); imageIn = bitmapHolder.ToNative(); } catch (Exception ex) { Logger.Error(string.Format("Transformation failed: {0}", transformation.Key), ex); throw; } finally { if (old != null && old != imageIn && old.PixelData != imageIn.PixelData) { old.FreePixels(); old = null; } } } } finally { _decodingLock.Release(); } } try { return(await imageIn.ToBitmapImageAsync()); } finally { imageIn.FreePixels(); imageIn = null; } }
static BitmapSource GetBitMapFromMemoryStream(IBitmap bitMap) => bitMap.ToNative();
protected virtual async Task <WithLoadingResult <WriteableBitmap> > GetImageAsync(string path, ImageSource source, bool isPlaceholder, Stream originalStream = null) { if (IsCancelled) { return(new WithLoadingResult <WriteableBitmap>(LoadingResult.Canceled)); } if (IsCancelled) { return(new WithLoadingResult <WriteableBitmap>(LoadingResult.Canceled)); } Stream stream = null; WithLoadingResult <Stream> streamWithResult; if (originalStream != null) { streamWithResult = new WithLoadingResult <Stream>(originalStream, LoadingResult.Stream); } else { streamWithResult = await GetStreamAsync(path, source).ConfigureAwait(false); } if (streamWithResult.HasError) { if (streamWithResult.Result == LoadingResult.NotFound) { Logger.Error(string.Format("Not found: {0} from {1}", path, source.ToString())); } return(new WithLoadingResult <WriteableBitmap>(streamWithResult.Result)); } stream = streamWithResult.Item; if (IsCancelled) { return(new WithLoadingResult <WriteableBitmap>(LoadingResult.Canceled)); } try { try { if (stream.Position != 0 && !stream.CanSeek) { if (originalStream != null) { // If we cannot seek the original stream then there's not much we can do return(new WithLoadingResult <WriteableBitmap>(LoadingResult.Failed)); } else { // Assets stream can't be seeked to origin position stream.Dispose(); streamWithResult = await GetStreamAsync(path, source).ConfigureAwait(false); if (streamWithResult.HasError) { return(new WithLoadingResult <WriteableBitmap>(streamWithResult.Result)); } stream = streamWithResult.Item; } } else { stream.Seek(0, SeekOrigin.Begin); } if (IsCancelled) { return(new WithLoadingResult <WriteableBitmap>(LoadingResult.Canceled)); } } catch (Exception ex) { Logger.Error("Something wrong happened while asynchronously retrieving image size from file: " + path, ex); return(new WithLoadingResult <WriteableBitmap>(LoadingResult.Failed)); } WriteableBitmap writableBitmap = null; // Special case to handle WebP decoding if (path.ToLowerInvariant().EndsWith(".webp")) { //TODO Logger.Error("Webp is not implemented on Windows"); return(new WithLoadingResult <WriteableBitmap>(LoadingResult.Failed)); } // Setting image informations var imageInformation = streamWithResult.ImageInformation ?? new ImageInformation(); imageInformation.SetKey(path == "Stream" ? GetKey() : GetKey(path), Parameters.CustomCacheKey); bool transformPlaceholdersEnabled = Parameters.TransformPlaceholdersEnabled.HasValue ? Parameters.TransformPlaceholdersEnabled.Value : ImageService.Instance.Config.TransformPlaceholders; if (Parameters.Transformations != null && Parameters.Transformations.Count > 0 && (!isPlaceholder || (isPlaceholder && transformPlaceholdersEnabled))) { BitmapHolder imageIn = null; try { imageIn = await stream.ToBitmapHolderAsync(Parameters.DownSampleSize, Parameters.DownSampleUseDipUnits, Parameters.DownSampleInterpolationMode, imageInformation).ConfigureAwait(false); } catch (Exception ex) { Logger.Error("Something wrong happened while asynchronously loading/decoding image: " + path, ex); return(new WithLoadingResult <WriteableBitmap>(LoadingResult.Failed)); } foreach (var transformation in Parameters.Transformations.ToList() /* to prevent concurrency issues */) { if (IsCancelled) { return(new WithLoadingResult <WriteableBitmap>(LoadingResult.Canceled)); } try { var old = imageIn; IBitmap bitmapHolder = transformation.Transform(imageIn); imageIn = bitmapHolder.ToNative(); if (old != null && old != imageIn && old.Pixels != imageIn.Pixels) { old.FreePixels(); old = null; } } catch (Exception ex) { Logger.Error("Can't apply transformation " + transformation.Key + " to image " + path, ex); } } writableBitmap = await imageIn.ToBitmapImageAsync(); imageIn.FreePixels(); imageIn = null; } else { try { writableBitmap = await stream.ToBitmapImageAsync(Parameters.DownSampleSize, Parameters.DownSampleUseDipUnits, Parameters.DownSampleInterpolationMode, imageInformation); } catch (Exception ex) { Logger.Error("Something wrong happened while asynchronously loading/decoding image: " + path, ex); return(new WithLoadingResult <WriteableBitmap>(LoadingResult.Failed)); } } return(WithLoadingResult.Encapsulate(writableBitmap, streamWithResult.Result, imageInformation)); } finally { if (stream != null) { stream.Dispose(); } } }
public override void ShowImage(IBitmap image, string message, int timeoutMillis) { Utils.RequestMainThread(() => AndHUD.Shared.ShowImage(this.GetTopActivity(), image.ToNative(), message, AndroidHUD.MaskType.Black, TimeSpan.FromMilliseconds(timeoutMillis)) ); }
public IBitmap Transform(IBitmap bitmapHolder, string path, ImageSource source, bool isPlaceholder, string key) { var sourceBitmap = bitmapHolder.ToNative(); return(new BitmapHolder(Transform(sourceBitmap, path, source, isPlaceholder, key))); }
public IBitmap Transform(IBitmap source) { return Transform(source.ToNative()); }
public override void ShowImage(IBitmap image, string message, int timeoutMillis) { BTProgressHUD.ShowImage(image.ToNative(), message, timeoutMillis); //this.ShowWithOverlay(timeoutMillis, () => BTProgressHUD.ShowImage(image.ToNative(), message, timeoutMillis)); }
protected virtual async Task <WithLoadingResult <WriteableBitmap> > GetImageAsync(string sourcePath, ImageSource source, bool isPlaceholder, Stream originalStream = null) { if (CancellationToken.IsCancellationRequested) { return(null); } byte[] bytes = null; string path = sourcePath; LoadingResult?result = null; try { if (originalStream != null) { using (var ms = new MemoryStream()) { await originalStream.CopyToAsync(ms).ConfigureAwait(false); bytes = ms.ToArray(); path = sourcePath; result = LoadingResult.Stream; } } else { using (var resolver = DataResolverFactory.GetResolver(source, Parameters, DownloadCache)) { var data = await resolver.GetData(path, CancellationToken.Token).ConfigureAwait(false); if (data == null) { return(null); } bytes = data.Data; path = data.ResultIdentifier; result = data.Result; } } } catch (OperationCanceledException) { Logger.Debug(string.Format("Image request for {0} got cancelled.", path)); return(null); } catch (Exception ex) { var message = String.Format("Unable to retrieve image data from source: {0}", sourcePath); Logger.Error(message, ex); Parameters.OnError(ex); return(null); } if (bytes == null) { return(null); } var image = await Task.Run(async() => { if (CancellationToken.IsCancellationRequested) { return(null); } WriteableBitmap writableBitmap = null; // Special case to handle WebP decoding if (sourcePath.ToLowerInvariant().EndsWith(".webp")) { //TODO throw new NotImplementedException("Webp is not implemented on Windows"); } bool transformPlaceholdersEnabled = Parameters.TransformPlaceholdersEnabled.HasValue ? Parameters.TransformPlaceholdersEnabled.Value : ImageService.Config.TransformPlaceholders; if (Parameters.Transformations != null && Parameters.Transformations.Count > 0 && (!isPlaceholder || (isPlaceholder && transformPlaceholdersEnabled))) { BitmapHolder imageIn = await bytes.ToBitmapHolderAsync(Parameters.DownSampleSize, Parameters.DownSampleInterpolationMode).ConfigureAwait(false); foreach (var transformation in Parameters.Transformations.ToList() /* to prevent concurrency issues */) { if (CancellationToken.IsCancellationRequested) { return(null); } try { var old = imageIn; IBitmap bitmapHolder = transformation.Transform(imageIn); imageIn = bitmapHolder.ToNative(); // old = null; // PLEASE NOTE! In Windows we could reuse int array (less memory usage) so we don't need that } catch (Exception ex) { Logger.Error("Can't apply transformation " + transformation.Key + " to image " + path, ex); } } await MainThreadDispatcher.PostAsync(async() => { writableBitmap = await imageIn.ToBitmapImageAsync(); }).ConfigureAwait(false); } else { writableBitmap = await bytes.ToBitmapImageAsync(Parameters.DownSampleSize, Parameters.DownSampleInterpolationMode).ConfigureAwait(false); } return(writableBitmap); }).ConfigureAwait(false); return(WithLoadingResult.Encapsulate(image, result.Value)); }
public override void ShowImage(IBitmap image, string message, int timeoutMillis) { this.ShowWithOverlay(timeoutMillis, () => BTProgressHUD.ShowImage(image.ToNative(), message, timeoutMillis)); }
public IBitmap Transform(IBitmap bitmapHolder, string path, ImageSource source, bool isPlaceholder, string key) { var nativeHolder = bitmapHolder.ToNative(); return(Transform(nativeHolder, path, source, isPlaceholder, key)); }
protected async override Task <WriteableBitmap> GenerateImageAsync(string path, ImageSource source, Stream imageData, ImageInformation imageInformation, bool enableTransformations, bool isPlaceholder) { BitmapHolder imageIn = null; if (imageData == null) { throw new ArgumentNullException(nameof(imageData)); } ThrowIfCancellationRequested(); try { // Special case to handle WebP decoding on Windows if (source != ImageSource.Stream && path.ToLowerInvariant().EndsWith(".webp", StringComparison.OrdinalIgnoreCase)) { throw new NotImplementedException("Webp is not implemented on Windows"); } else if (enableTransformations && Parameters.Transformations != null && Parameters.Transformations.Count > 0) { imageIn = await imageData.ToBitmapHolderAsync(Parameters.DownSampleSize, Parameters.DownSampleUseDipUnits, Parameters.DownSampleInterpolationMode, imageInformation).ConfigureAwait(false); } else { return(await imageData.ToBitmapImageAsync(Parameters.DownSampleSize, Parameters.DownSampleUseDipUnits, Parameters.DownSampleInterpolationMode, imageInformation).ConfigureAwait(false)); } } finally { imageData?.Dispose(); } ThrowIfCancellationRequested(); if (enableTransformations && Parameters.Transformations != null && Parameters.Transformations.Count > 0) { var transformations = Parameters.Transformations.ToList(); await _decodingLock.WaitAsync().ConfigureAwait(false); // Applying transformations is both CPU and memory intensive try { foreach (var transformation in transformations) { ThrowIfCancellationRequested(); var old = imageIn; try { IBitmap bitmapHolder = transformation.Transform(imageIn); imageIn = bitmapHolder.ToNative(); } catch (Exception ex) { Logger.Error(string.Format("Transformation failed: {0}", transformation.Key), ex); throw; } finally { if (old != null && old != imageIn && old.Pixels != imageIn.Pixels) { old.FreePixels(); old = null; } } } } finally { _decodingLock.Release(); } } try { return(await imageIn.ToBitmapImageAsync()); } finally { imageIn.FreePixels(); imageIn = null; } }