Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Framework / System / Windows / Controls / Image.cs / 2 / Image.cs
//---------------------------------------------------------------------------- // // Copyright (C) Microsoft Corporation. All rights reserved. // // File: Image.cs // // Description: Contains the Image class. // Layout Spec at [....]/layout/Specs/Image.xml // // History: // 06/02/2003 : [....] - Added to WCP branch, layout functionality (was Image.cs in old branch) // //--------------------------------------------------------------------------- using MS.Internal; using MS.Internal.PresentationFramework; using MS.Utility; using System.Diagnostics; using System.ComponentModel; #if OLD_AUTOMATION using System.Windows.Automation.Provider; #endif using System.Windows.Threading; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Documents; using System.Windows.Markup; using System.Windows.Navigation; using System; namespace System.Windows.Controls { ////// [Localizability(LocalizationCategory.None, Readability = Readability.Unreadable)] #if OLD_AUTOMATION [Automation(AccessibilityControlType = "Image")] #endif public class Image : FrameworkElement, IUriContext, IProvidePropertyFallback { //------------------------------------------------------------------- // // Constructors // //------------------------------------------------------------------- #region Constructors ////// Default DependencyObject constructor /// ////// Automatic determination of current Dispatcher. Use alternative constructor /// that accepts a Dispatcher for best performance. /// public Image() : base() { } #endregion //-------------------------------------------------------------------- // // Public Methods // //------------------------------------------------------------------- //-------------------------------------------------------------------- // // Public Properties // //-------------------------------------------------------------------- #region Public Properties ////// Gets/Sets the Source on this Image. /// /// The Source property is the ImageSource that holds the actual image drawn. /// public ImageSource Source { get { return (ImageSource) GetValue(SourceProperty); } set { SetValue(SourceProperty, value); } } ////// Gets/Sets the Stretch on this Image. /// The Stretch property determines how large the Image will be drawn. /// ///public Stretch Stretch { get { return (Stretch) GetValue(StretchProperty); } set { SetValue(StretchProperty, value); } } /// /// Gets/Sets the stretch direction of the Viewbox, which determines the restrictions on /// scaling that are applied to the content inside the Viewbox. For instance, this property /// can be used to prevent the content from being smaller than its native size or larger than /// its native size. /// ///public StretchDirection StretchDirection { get { return (StretchDirection)GetValue(StretchDirectionProperty); } set { SetValue(StretchDirectionProperty, value); } } /// /// DependencyProperty for Image Source property. /// ///[CommonDependencyProperty] public static readonly DependencyProperty SourceProperty = DependencyProperty.Register( "Source", typeof(ImageSource), typeof(Image), new FrameworkPropertyMetadata( null, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnSourceChanged), null), null); /// /// DependencyProperty for Stretch property. /// ///[CommonDependencyProperty] public static readonly DependencyProperty StretchProperty = Viewbox.StretchProperty.AddOwner(typeof(Image)); /// /// DependencyProperty for StretchDirection property. /// ///public static readonly DependencyProperty StretchDirectionProperty = Viewbox.StretchDirectionProperty.AddOwner(typeof(Image)); /// /// ImageFailedEvent is a routed event. /// public static readonly RoutedEvent ImageFailedEvent = EventManager.RegisterRoutedEvent( "ImageFailed", RoutingStrategy.Bubble, typeof(EventHandler), typeof(Image)); /// /// Raised when there is a failure in image. /// public event EventHandlerImageFailed { add { AddHandler(ImageFailedEvent, value); } remove { RemoveHandler(ImageFailedEvent, value); } } #endregion //------------------------------------------------------------------- // // Protected Methods // //-------------------------------------------------------------------- #region Protected Methods /// /// Creates AutomationPeer ( protected override System.Windows.Automation.Peers.AutomationPeer OnCreateAutomationPeer() { return new System.Windows.Automation.Peers.ImageAutomationPeer(this); } ///) /// /// Updates DesiredSize of the Image. Called by parent UIElement. This is the first pass of layout. /// ////// Image will always return its natural size, if it fits within the constraint. If not, it will return /// as large a size as it can. Remember that image can later arrange at any size and stretch/align. /// /// Constraint size is an "upper limit" that Image should not exceed. ///Image's desired size. protected override Size MeasureOverride(Size constraint) { return MeasureArrangeHelper(constraint); } ////// Override for protected override Size ArrangeOverride(Size arrangeSize) { return MeasureArrangeHelper(arrangeSize); } // // protected override void OnArrange(Size arrangeSize) // Because Image does not have children and it is inexpensive to compute it's alignment/size, // it does not need an OnArrange override. It will simply use its own RenderSize (set when its // Arrange is called) in OnRender. // ///. /// /// The Stretch property determines how large the Image will be drawn. The values for Stretch are: /// protected override void OnRender(DrawingContext dc) { ImageSource imageSource = Source; if (imageSource == null) { return; } //computed from the ArrangeOverride return size dc.DrawImage(imageSource, new Rect(new Point(), RenderSize)); } #endregion Protected Methods #region IUriContext implementation ////// AlignmentX and AlignmentY properties are used to position the Image when its size /// is different the RenderSize. ///
- None
- Image draws at natural size and will clip / overdraw if too large.
///- Fill
- Image will draw at RenderSize. Aspect Ratio may be distorted.
///- Uniform
- Image will scale uniformly up or down to fit within RenderSize.
///- UniformFill
- Image will scale uniformly to fill RenderSize, clipping / overdrawing in the larger dimension.
/// Accessor for the base uri of the Image /// Uri IUriContext.BaseUri { get { return BaseUri; } set { BaseUri = value; } } ////// Implementation for BaseUri /// protected virtual Uri BaseUri { get { return (Uri)GetValue(BaseUriHelper.BaseUriProperty); } set { SetValue(BaseUriHelper.BaseUriProperty, value); } } #endregion IUriContext implementation //------------------------------------------------------------------- // // Private Methods // //------------------------------------------------------------------- #region Private Methods ////// Contains the code common for MeasureOverride and ArrangeOverride. /// /// input size is the parent-provided space that Image should use to "fit in", according to other properties. ///Image's desired size. private Size MeasureArrangeHelper(Size inputSize) { ImageSource imageSource = Source; Size naturalSize = new Size(); if (imageSource == null) { return naturalSize; } try { UpdateBaseUri(this, imageSource); naturalSize = imageSource.Size; } catch(Exception e) { Source = null; RaiseEvent(new ExceptionRoutedEventArgs(ImageFailedEvent, this, e)); } //get computed scale factor Size scaleFactor = Viewbox.ComputeScaleFactor(inputSize, naturalSize, this.Stretch, this.StretchDirection); // Returns our minimum size & sets DesiredSize. return new Size(naturalSize.Width * scaleFactor.Width, naturalSize.Height * scaleFactor.Height); } // // This property // 1. Finds the correct initial size for the _effectiveValues store on the current DependencyObject // 2. This is a performance optimization // internal override int EffectiveValuesInitialSize { get { return 19; } } #endregion Private Methods #region private event handlers #endregion //------------------------------------------------------------------- // // Private Fields // //-------------------------------------------------------------------- #region Private Fields private EventHandler _sourceDownloaded; private EventHandler_sourceFailed; #endregion //------------------------------------------------------------------- // // Static Constructors & Delegates // //-------------------------------------------------------------------- #region Static Constructors & Delegates static Image() { Style style = CreateDefaultStyles(); StyleProperty.OverrideMetadata(typeof(Image), new FrameworkPropertyMetadata(style)); // // The Stretch & StretchDirection properties are AddOwner'ed from a class which is not // base class for Image so the metadata with flags get lost. We need to override them // here to make it work again. // StretchProperty.OverrideMetadata( typeof(Image), new FrameworkPropertyMetadata( Stretch.Uniform, FrameworkPropertyMetadataOptions.AffectsMeasure ) ); StretchDirectionProperty.OverrideMetadata( typeof(Image), new FrameworkPropertyMetadata( StretchDirection.Both, FrameworkPropertyMetadataOptions.AffectsMeasure ) ); } private static Style CreateDefaultStyles() { Style style = new Style(typeof(Image), null); style.Setters.Add (new Setter(FlowDirectionProperty, FlowDirection.LeftToRight)); style.Seal(); return style; } private void OnSourceDownloaded(object sender, EventArgs e) { InvalidateMeasure(); InvalidateVisual(); //ensure re-rendering } private void OnSourceFailed(object sender, ExceptionEventArgs e) { Source = null; RaiseEvent(new ExceptionRoutedEventArgs(ImageFailedEvent, this, e.ErrorException)); } private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (!e.IsASubPropertyChange) { Image image = (Image)d; ImageSource oldValue = (ImageSource)e.OldValue; ImageSource newValue = (ImageSource)e.NewValue; UpdateBaseUri(d, newValue); BitmapSource oldBitmapSource = oldValue as BitmapSource; if (oldBitmapSource != null && image._sourceDownloaded != null && !oldBitmapSource.IsFrozen) { oldBitmapSource.DownloadCompleted -= image._sourceDownloaded; oldBitmapSource.DownloadFailed -= image._sourceFailed; } BitmapSource newBitmapSource = newValue as BitmapSource; if (newBitmapSource != null && !newBitmapSource.IsFrozen) { image._sourceDownloaded = new EventHandler(image.OnSourceDownloaded); newBitmapSource.DownloadCompleted += image._sourceDownloaded; image._sourceFailed = new EventHandler (image.OnSourceFailed); newBitmapSource.DownloadFailed += image._sourceFailed; newBitmapSource.DecodeFailed += image._sourceFailed; } } } private static void UpdateBaseUri(DependencyObject d, ImageSource source) { if ((source is IUriContext) && (!source.IsFrozen) && (((IUriContext)source).BaseUri == null)) { Uri baseUri = BaseUriHelper.GetBaseUriCore(d); if (baseUri != null) { ((IUriContext)source).BaseUri = BaseUriHelper.GetBaseUriCore(d); } } } #endregion #region IProvidePropertyFallback /// /// Says if the type can provide fallback value for the given property /// bool IProvidePropertyFallback.CanProvidePropertyFallback(string property) { if (String.CompareOrdinal(property, "Source") == 0) { return true; } return false; } ////// Returns the fallback value for the given property. /// object IProvidePropertyFallback.ProvidePropertyFallback(string property, Exception cause) { if (String.CompareOrdinal(property, "Source") == 0) { RaiseEvent(new ExceptionRoutedEventArgs(ImageFailedEvent, this, cause)); } // For now we do not have a static that represents a bad-image, so just return a null. return null; } #endregion IProvidePropertyFallback } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- IUnknownConstantAttribute.cs
- UserControlParser.cs
- IdentityNotMappedException.cs
- GenericUI.cs
- Selection.cs
- _ScatterGatherBuffers.cs
- PasswordBoxAutomationPeer.cs
- HttpWebResponse.cs
- SelectionEditor.cs
- MemoryPressure.cs
- HostProtectionException.cs
- SafeHandle.cs
- SiteMapPathDesigner.cs
- DataControlFieldsEditor.cs
- BounceEase.cs
- TriState.cs
- RegisteredExpandoAttribute.cs
- XmlCountingReader.cs
- ComponentConverter.cs
- DefinitionUpdate.cs
- MobileControl.cs
- BuildResult.cs
- XmlSchemaInclude.cs
- CodeFieldReferenceExpression.cs
- ContextMarshalException.cs
- HandlerMappingMemo.cs
- cookiecollection.cs
- SplitterEvent.cs
- WebZone.cs
- DirectoryObjectSecurity.cs
- Context.cs
- XmlWriterSettings.cs
- FormatConvertedBitmap.cs
- HostingPreferredMapPath.cs
- BackStopAuthenticationModule.cs
- RegistryDataKey.cs
- SafeNativeMethods.cs
- BaseValidator.cs
- IArgumentProvider.cs
- DependencyPropertyHelper.cs
- UpDownBase.cs
- ForAllOperator.cs
- RightsManagementResourceHelper.cs
- KnownBoxes.cs
- Compilation.cs
- PenThread.cs
- DeferredElementTreeState.cs
- HierarchicalDataSourceConverter.cs
- CustomCredentialPolicy.cs
- NodeInfo.cs
- Span.cs
- MdImport.cs
- TabControl.cs
- CompositeActivityDesigner.cs
- MILUtilities.cs
- KnownIds.cs
- DataGridViewCellParsingEventArgs.cs
- EditCommandColumn.cs
- HttpRequest.cs
- _ScatterGatherBuffers.cs
- SoapSchemaExporter.cs
- MembershipUser.cs
- SQLInt32Storage.cs
- CurrencyManager.cs
- WebSysDefaultValueAttribute.cs
- ResXDataNode.cs
- FreeFormPanel.cs
- SecurityCriticalDataForSet.cs
- LightweightCodeGenerator.cs
- ScrollableControl.cs
- OutputCacheSettings.cs
- HtmlShimManager.cs
- PaginationProgressEventArgs.cs
- ActiveDesignSurfaceEvent.cs
- ListBase.cs
- Rethrow.cs
- ImageBrush.cs
- WebContext.cs
- DaylightTime.cs
- ColumnResizeUndoUnit.cs
- ExceptionWrapper.cs
- Propagator.ExtentPlaceholderCreator.cs
- VerificationAttribute.cs
- ModulesEntry.cs
- ElementsClipboardData.cs
- EventLogConfiguration.cs
- XmlAttributeAttribute.cs
- PrintEvent.cs
- TextRange.cs
- MarshalByValueComponent.cs
- ContractMapping.cs
- MSAAWinEventWrap.cs
- GC.cs
- X509CertificateCollection.cs
- DockEditor.cs
- DefaultWorkflowLoaderService.cs
- SafeNativeMethodsOther.cs
- DataSourceXmlElementAttribute.cs
- M3DUtil.cs
- XmlQueryStaticData.cs