Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / CommonUI / System / Drawing / IconConverter.cs / 1305376 / 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;
using System.Runtime.Versioning;
///
///
/// 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 {
///
///
/// 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 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 to the given destination type 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);
}
///
///
/// Converts the given object to the converter's native type.
///
[SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
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 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")]
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
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
- XPathDescendantIterator.cs
- DocobjHost.cs
- AtomServiceDocumentSerializer.cs
- PersonalizationProvider.cs
- OdbcConnectionOpen.cs
- ConnectionStringSettingsCollection.cs
- TargetControlTypeAttribute.cs
- Point4D.cs
- MatrixTransform3D.cs
- Baml2006SchemaContext.cs
- WebPartDisplayModeCollection.cs
- ListSourceHelper.cs
- PersonalizationStateInfo.cs
- NumericUpDownAcceleration.cs
- HashCodeCombiner.cs
- BaseResourcesBuildProvider.cs
- TextComposition.cs
- CodeTypeDeclarationCollection.cs
- PageHandlerFactory.cs
- DataGridCell.cs
- Expressions.cs
- DesignerUtility.cs
- RepeaterCommandEventArgs.cs
- MouseWheelEventArgs.cs
- UrlPath.cs
- QilInvokeLateBound.cs
- RoleGroupCollection.cs
- CodeEventReferenceExpression.cs
- ErrorRuntimeConfig.cs
- CompoundFileIOPermission.cs
- SpellerError.cs
- LineSegment.cs
- ObjectMaterializedEventArgs.cs
- OptimisticConcurrencyException.cs
- EmptyQuery.cs
- SchemaComplexType.cs
- PermissionListSet.cs
- ListViewPagedDataSource.cs
- ReliabilityContractAttribute.cs
- ParseHttpDate.cs
- ValidatorCollection.cs
- ControllableStoryboardAction.cs
- webeventbuffer.cs
- StringAnimationBase.cs
- StreamFormatter.cs
- Application.cs
- FileFormatException.cs
- RelationshipDetailsCollection.cs
- NavigationExpr.cs
- GeneralTransform.cs
- IncrementalReadDecoders.cs
- PopupControlService.cs
- PenThread.cs
- SqlTypeSystemProvider.cs
- OleDbFactory.cs
- CompensationParticipant.cs
- RadialGradientBrush.cs
- MSAAWinEventWrap.cs
- ServiceModelConfigurationSection.cs
- DesignerVerb.cs
- EventLogStatus.cs
- SoapObjectInfo.cs
- Blend.cs
- FullTextState.cs
- RowCache.cs
- FixedSOMImage.cs
- GenericEnumerator.cs
- RoleManagerEventArgs.cs
- CodeTypeReferenceExpression.cs
- PropertyInfoSet.cs
- AsymmetricKeyExchangeFormatter.cs
- InvalidFilterCriteriaException.cs
- ExecutionEngineException.cs
- UInt32Storage.cs
- InputEventArgs.cs
- FixedSOMImage.cs
- SQLDouble.cs
- SuppressMessageAttribute.cs
- DynamicUpdateCommand.cs
- GenericNameHandler.cs
- TraceData.cs
- LingerOption.cs
- XamlTemplateSerializer.cs
- ItemAutomationPeer.cs
- WebPartDescriptionCollection.cs
- DynamicUpdateCommand.cs
- XamlSerializerUtil.cs
- MissingMethodException.cs
- InternalCache.cs
- ScrollItemProviderWrapper.cs
- CaseCqlBlock.cs
- MarkerProperties.cs
- SystemIPv4InterfaceProperties.cs
- WindowsAuthenticationModule.cs
- HostedElements.cs
- WebServiceMethodData.cs
- Journaling.cs
- WeakReferenceList.cs
- CheckBoxPopupAdapter.cs
- ArcSegment.cs