protected override SizeF CalculateInnerDesiredSize(SizeF totalSize) { ImageSourceState allocatedSource = GetLoadedSource(false); if (allocatedSource == null) { _lastImageSourceSize = SizeF.Empty; return(new SizeF(10, 10)); } SizeF imageSize = allocatedSource.ImageSource.SourceSize; float sourceFrameRatio = imageSize.Width / imageSize.Height; // Adaptions when available size is not specified in any direction(s) if (double.IsNaN(totalSize.Width) && double.IsNaN(totalSize.Height)) { totalSize = imageSize; } else if (double.IsNaN(totalSize.Height)) { totalSize.Height = totalSize.Width / sourceFrameRatio; } else if (double.IsNaN(totalSize.Width)) { totalSize.Width = totalSize.Height * sourceFrameRatio; } _lastImageSourceSize = imageSize; return(allocatedSource.ImageSource.StretchSource(totalSize, imageSize, Stretch, StretchDirection)); }
protected ImageSourceState GetLoadedSource(bool invalidateLayout) { if (_loadImageSource) { DisposeImageSources(); _fallbackSourceInUse = false; _loadImageSource = false; } ImageSourceState allocatedSource = null; // Find a new image source if (_sourceState.ImageSource == null) { _sourceState.ImageSource = LoadImageSource(Source, true); _sourceState.Setup = false; } if (_sourceState.ImageSource != null && !_sourceState.ImageSource.IsAllocated) { _sourceState.Setup = false; _sourceState.ImageSource.Allocate(); } if (_sourceState.ImageSource != null && _sourceState.ImageSource.IsAllocated) { allocatedSource = _sourceState; } else { // If the source image could not load yet, try fallback image if (_fallbackSourceState.ImageSource == null) { _fallbackSourceState.ImageSource = LoadImageSource(FallbackSource, false); _fallbackSourceState.Setup = false; } if (_fallbackSourceState.ImageSource != null && !_fallbackSourceState.ImageSource.IsAllocated) { _fallbackSourceState.Setup = false; _fallbackSourceState.ImageSource.Allocate(); } if (_fallbackSourceState.ImageSource != null && _fallbackSourceState.ImageSource.IsAllocated) { _fallbackSourceInUse = true; allocatedSource = _fallbackSourceState; } } if (invalidateLayout && allocatedSource != null && allocatedSource.ImageSource.SourceSize != _lastImageSourceSize) { InvalidateLayout(true, true); } HasImage = allocatedSource != null; return(allocatedSource); }
public override void RenderOverride(RenderContext localRenderContext) { ImageSourceState allocatedSource = GetLoadedSource(true); if (allocatedSource == null) { base.RenderOverride(localRenderContext); } else { // Update source geometry if necessary (source has changed, layout has changed). if (!allocatedSource.Setup) { allocatedSource.ImageSource.Setup(_innerRect, localRenderContext.ZOrder, SkinNeutralAR); allocatedSource.Setup = true; } base.RenderOverride(localRenderContext); allocatedSource.ImageSource.Render(localRenderContext, Stretch, StretchDirection); } }