Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / CommonUI / System / Drawing / IconConverter.cs / 1 / IconConverter.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- /* */ namespace System.Drawing { using System.Runtime.Serialization.Formatters; using System.Runtime.InteropServices; using System.IO; using System.Diagnostics; using Microsoft.Win32; using System.Collections; using System.ComponentModel; using System.Globalization; using System.Reflection; using System.ComponentModel.Design.Serialization; using System.Diagnostics.CodeAnalysis; ////// /// IconConverter is a class that can be used to convert /// Icon from one data type to another. Access this /// class through the TypeDescriptor. /// public class IconConverter : ExpandableObjectConverter { ////// /// public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(byte[])) { return true; } if(sourceType == typeof(InstanceDescriptor)){ return false; } return base.CanConvertFrom(context, sourceType); } ///Gets a value indicating whether this converter can /// convert an object in the given source type to the native type of the converter /// using the context. ////// /// public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(Image) || destinationType == typeof(Bitmap)) { return true; } if (destinationType == typeof(byte[])) { return true; } return base.CanConvertTo(context, destinationType); } ///Gets a value indicating whether this converter can /// convert an object to the given destination type using the context. ////// /// [SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")] public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is byte[]) { MemoryStream ms = new MemoryStream((byte[])value); return new Icon(ms); } return base.ConvertFrom(context, culture, value); } ///Converts the given object to the converter's native type. ////// /// Converts the given object to another type. The most common types to convert /// are to and from a string object. The default implementation will make a call /// to ToString on the object if the object is valid and if the destination /// type is string. If this cannot convert to the desitnation type, this will /// throw a NotSupportedException. /// [SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")] [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")] public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == null) { throw new ArgumentNullException("destinationType"); } if (destinationType == typeof(Image) || destinationType == typeof(Bitmap)) { Icon icon = value as Icon; if( icon != null ) { return icon.ToBitmap(); } } if (destinationType == typeof(string)) { if (value != null) { return value.ToString(); } else { return SR.GetString(SR.toStringNone); } } if (destinationType == typeof(byte[])) { if (value != null) { MemoryStream ms = null; try { ms = new MemoryStream(); Icon icon = value as Icon; if( icon != null ) { icon.Save(ms); } } finally { if (ms != null) { ms.Close(); } } if (ms != null) { return ms.ToArray(); } else { return null; } } else { return new byte[0]; } } return base.ConvertTo(context, culture, value, destinationType); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- /* */ namespace System.Drawing { using System.Runtime.Serialization.Formatters; using System.Runtime.InteropServices; using System.IO; using System.Diagnostics; using Microsoft.Win32; using System.Collections; using System.ComponentModel; using System.Globalization; using System.Reflection; using System.ComponentModel.Design.Serialization; using System.Diagnostics.CodeAnalysis; ////// /// IconConverter is a class that can be used to convert /// Icon from one data type to another. Access this /// class through the TypeDescriptor. /// public class IconConverter : ExpandableObjectConverter { ////// /// public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(byte[])) { return true; } if(sourceType == typeof(InstanceDescriptor)){ return false; } return base.CanConvertFrom(context, sourceType); } ///Gets a value indicating whether this converter can /// convert an object in the given source type to the native type of the converter /// using the context. ////// /// public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(Image) || destinationType == typeof(Bitmap)) { return true; } if (destinationType == typeof(byte[])) { return true; } return base.CanConvertTo(context, destinationType); } ///Gets a value indicating whether this converter can /// convert an object to the given destination type using the context. ////// /// [SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")] public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is byte[]) { MemoryStream ms = new MemoryStream((byte[])value); return new Icon(ms); } return base.ConvertFrom(context, culture, value); } ///Converts the given object to the converter's native type. ////// /// Converts the given object to another type. The most common types to convert /// are to and from a string object. The default implementation will make a call /// to ToString on the object if the object is valid and if the destination /// type is string. If this cannot convert to the desitnation type, this will /// throw a NotSupportedException. /// [SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")] [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")] public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == null) { throw new ArgumentNullException("destinationType"); } if (destinationType == typeof(Image) || destinationType == typeof(Bitmap)) { Icon icon = value as Icon; if( icon != null ) { return icon.ToBitmap(); } } if (destinationType == typeof(string)) { if (value != null) { return value.ToString(); } else { return SR.GetString(SR.toStringNone); } } if (destinationType == typeof(byte[])) { if (value != null) { MemoryStream ms = null; try { ms = new MemoryStream(); Icon icon = value as Icon; if( icon != null ) { icon.Save(ms); } } finally { if (ms != null) { ms.Close(); } } if (ms != null) { return ms.ToArray(); } else { return null; } } else { return new byte[0]; } } return base.ConvertTo(context, culture, value, destinationType); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- UnauthorizedAccessException.cs
- ValueQuery.cs
- wgx_commands.cs
- FrameworkRichTextComposition.cs
- List.cs
- UserPreferenceChangingEventArgs.cs
- LoginUtil.cs
- Annotation.cs
- SqlBuilder.cs
- FileDialogCustomPlace.cs
- SoapMessage.cs
- ControlPager.cs
- LocatorManager.cs
- XmlEncoding.cs
- StreamWithDictionary.cs
- HttpRequestTraceRecord.cs
- TextBox.cs
- DataGridViewCellToolTipTextNeededEventArgs.cs
- DoubleLinkListEnumerator.cs
- VerificationAttribute.cs
- HandledMouseEvent.cs
- EventLogEntry.cs
- Tag.cs
- ShimAsPublicXamlType.cs
- SqlRetyper.cs
- PrintPageEvent.cs
- FlowDocumentPageViewerAutomationPeer.cs
- DataGridViewTopLeftHeaderCell.cs
- NumberSubstitution.cs
- ProfileProvider.cs
- SqlCommandBuilder.cs
- ObjectNavigationPropertyMapping.cs
- ToolStripDropDownItemDesigner.cs
- BufferModeSettings.cs
- ContentElement.cs
- xamlnodes.cs
- EntityParameterCollection.cs
- CompositeControl.cs
- SqlCharStream.cs
- ToolStripDropDownButton.cs
- ExternalException.cs
- newinstructionaction.cs
- StylusEventArgs.cs
- Rotation3D.cs
- ProgressPage.cs
- PerformanceCounterLib.cs
- EditorBrowsableAttribute.cs
- WindowsBrush.cs
- RequiredAttributeAttribute.cs
- DiscriminatorMap.cs
- HwndHost.cs
- BitStack.cs
- CorrelationValidator.cs
- DataControlLinkButton.cs
- Compilation.cs
- ChangeTracker.cs
- SqlResolver.cs
- CompoundFileDeflateTransform.cs
- CheckBoxAutomationPeer.cs
- Comparer.cs
- DataRowComparer.cs
- MethodBody.cs
- HttpFileCollectionBase.cs
- WeakEventTable.cs
- ConnectionPool.cs
- HttpGetClientProtocol.cs
- WeakReferenceEnumerator.cs
- SizeAnimationClockResource.cs
- TimeSpanValidator.cs
- ControlDesignerState.cs
- MemoryRecordBuffer.cs
- DiscoveryClientElement.cs
- DataTransferEventArgs.cs
- ArcSegment.cs
- CompiledIdentityConstraint.cs
- UTF8Encoding.cs
- PEFileEvidenceFactory.cs
- ProbeMatchesMessage11.cs
- XmlSchemaCollection.cs
- InputProcessorProfiles.cs
- MimeObjectFactory.cs
- StackOverflowException.cs
- ListBoxItemAutomationPeer.cs
- X509ScopedServiceCertificateElementCollection.cs
- AsnEncodedData.cs
- MobileUserControl.cs
- ObjectPersistData.cs
- JsonReaderDelegator.cs
- ServiceModelTimeSpanValidator.cs
- ProcessThread.cs
- CodeFieldReferenceExpression.cs
- ExpandCollapsePattern.cs
- HMACMD5.cs
- TableChangeProcessor.cs
- CustomPopupPlacement.cs
- WizardSideBarListControlItem.cs
- EventData.cs
- FileLogRecord.cs
- clipboard.cs
- KeyPullup.cs