Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / 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 {
///
///
/// 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")]
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")]
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 {
///
///
/// 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")]
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")]
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
- TypeConverterMarkupExtension.cs
- AttributeQuery.cs
- FormClosingEvent.cs
- XmlTextEncoder.cs
- DrawingAttributesDefaultValueFactory.cs
- MenuItem.cs
- GPPOINT.cs
- ToolboxDataAttribute.cs
- MediaElementAutomationPeer.cs
- Encoder.cs
- HostUtils.cs
- OpenFileDialog.cs
- DbConvert.cs
- ScriptControl.cs
- GridViewAutomationPeer.cs
- LinqDataSource.cs
- ServiceBehaviorElement.cs
- DocumentOrderComparer.cs
- ItemPager.cs
- CompoundFileReference.cs
- RenderingBiasValidation.cs
- SSmlParser.cs
- WriteStateInfoBase.cs
- TemplateXamlParser.cs
- ApplicationManager.cs
- DesignerDataConnection.cs
- NonClientArea.cs
- DocumentXPathNavigator.cs
- dbdatarecord.cs
- MatrixTransform3D.cs
- LayoutDump.cs
- RoleGroupCollection.cs
- LazyLoadBehavior.cs
- ViewgenGatekeeper.cs
- ScrollPattern.cs
- TableCell.cs
- LocalizationComments.cs
- EventSourceCreationData.cs
- HttpFileCollectionWrapper.cs
- MaskedTextProvider.cs
- _ConnectStream.cs
- AffineTransform3D.cs
- MergeFailedEvent.cs
- AsymmetricKeyExchangeDeformatter.cs
- XmlEntity.cs
- GridViewColumnCollectionChangedEventArgs.cs
- IndependentlyAnimatedPropertyMetadata.cs
- XComponentModel.cs
- TextModifier.cs
- SymmetricAlgorithm.cs
- DocComment.cs
- DataTableMappingCollection.cs
- SafeTokenHandle.cs
- PreservationFileReader.cs
- CellLabel.cs
- keycontainerpermission.cs
- AutoGeneratedField.cs
- GZipDecoder.cs
- LinkDesigner.cs
- SmiTypedGetterSetter.cs
- KeyConverter.cs
- ParameterModifier.cs
- MarginsConverter.cs
- PolicyValidationException.cs
- ByteAnimationBase.cs
- __Filters.cs
- TimeSpanHelper.cs
- ProfileSettings.cs
- RolePrincipal.cs
- Array.cs
- XmlSchemaAttribute.cs
- X509Utils.cs
- AuthenticateEventArgs.cs
- WebDescriptionAttribute.cs
- EventProvider.cs
- WebMessageEncodingBindingElement.cs
- XmlBinaryWriter.cs
- DataGridViewComboBoxColumnDesigner.cs
- NameValuePermission.cs
- SQLInt64.cs
- UnmanagedMemoryAccessor.cs
- PageBreakRecord.cs
- DesignerOptionService.cs
- ExcCanonicalXml.cs
- Crypto.cs
- Sorting.cs
- SmtpLoginAuthenticationModule.cs
- AppModelKnownContentFactory.cs
- CodeCastExpression.cs
- ControlUtil.cs
- ContentPlaceHolder.cs
- ProfileService.cs
- COM2IPerPropertyBrowsingHandler.cs
- DataGridCellsPresenter.cs
- FlagsAttribute.cs
- MediaCommands.cs
- SqlConnectionString.cs
- StandardToolWindows.cs
- WindowsFont.cs
- AnimatedTypeHelpers.cs