Code:
/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / ndp / fx / src / Designer / Drawing / System / Drawing / Design / ImageEditor.cs / 1 / ImageEditor.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Drawing.Design {
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Forms.ComponentModel;
using System.Windows.Forms.Design;
///
///
/// Provides an editor for visually picking an image.
///
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, Flags=System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)]
public class ImageEditor : UITypeEditor {
internal static Type[] imageExtenders = new Type[] { typeof(BitmapEditor), /*gpr typeof(Icon),*/ typeof(MetafileEditor)};
internal FileDialog fileDialog;
// VSWhidbey 95227: accessor needed into the static field so that derived classes
// can implement a different list of supported image types.
[SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")] // everything in this assembly is full trust.
protected virtual Type[] GetImageExtenders() {
return imageExtenders;
}
///
///
/// [To be supplied.]
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1818:DoNotConcatenateStringsInsideLoops")]
protected static string CreateExtensionsString(string[] extensions, string sep) {
if (extensions == null || extensions.Length == 0)
return null;
string text = null;
for (int i = 0; i < extensions.Length - 1; i++)
text = text + "*." + extensions[i] + sep;
text = text + "*." + extensions[extensions.Length-1];
return text;
}
///
///
/// [To be supplied.]
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly")] // previously shipped this way. Would be a breaking change.
protected static string CreateFilterEntry(ImageEditor e)
{
string desc = e.GetFileDialogDescription();
string exts = CreateExtensionsString(e.GetExtensions(),",");
string extsSemis = CreateExtensionsString(e.GetExtensions(),";");
return desc + "(" + exts + ")|" + extsSemis;
}
///
///
/// Edits the given object value using the editor style provided by
/// GetEditorStyle. A service provider is provided so that any
/// required editing services can be obtained.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1818:DoNotConcatenateStringsInsideLoops")]
[SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")] // everything in this assembly is full trust.
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {
if (provider != null) {
IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edSvc != null) {
if (fileDialog == null) {
fileDialog = new OpenFileDialog();
string filter = CreateFilterEntry(this);
for (int i = 0; i < GetImageExtenders().Length; i++) {
ImageEditor e = (ImageEditor) Activator.CreateInstance(GetImageExtenders()[i], BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, null, null); //.CreateInstance();
Type myClass = this.GetType();
Type editorClass = e.GetType();
if (!myClass.Equals(editorClass) && e != null && myClass.IsInstanceOfType(e))
filter += "|" + CreateFilterEntry(e);
}
fileDialog.Filter = filter;
}
IntPtr hwndFocus = UnsafeNativeMethods.GetFocus();
try {
if (fileDialog.ShowDialog() == DialogResult.OK) {
FileStream file = new FileStream(fileDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
value = LoadFromStream(file);
}
}
finally {
if (hwndFocus != IntPtr.Zero) {
UnsafeNativeMethods.SetFocus(new HandleRef(null, hwndFocus));
}
}
}
}
return value;
}
///
///
/// Retrieves the editing style of the Edit method. If the method
/// is not supported, this will return None.
///
[SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")] // everything in this assembly is full trust.
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) {
return UITypeEditorEditStyle.Modal;
}
///
///
/// [To be supplied.]
///
protected virtual string GetFileDialogDescription() {
return SR.GetString(SR.imageFileDescription);
}
///
///
/// [To be supplied.]
///
[SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")] // everything in this assembly is full trust.
protected virtual string[] GetExtensions() {
// We should probably smash them together...
ArrayList list = new ArrayList();
for (int i = 0; i < GetImageExtenders().Length; i++) {
ImageEditor e = (ImageEditor) Activator.CreateInstance(GetImageExtenders()[i], BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, null, null); //.CreateInstance();
if (!e.GetType().Equals(typeof(ImageEditor)))
list.AddRange(new ArrayList(e.GetExtensions()));
}
return(string[]) list.ToArray(typeof(string));
}
///
///
/// Determines if this editor supports the painting of a representation
/// of an object's value.
///
[SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")] // everything in this assembly is full trust.
public override bool GetPaintValueSupported(ITypeDescriptorContext context) {
return true;
}
///
///
/// [To be supplied.]
///
[SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")] // everything in this assembly is full trust.
protected virtual Image LoadFromStream(Stream stream) {
//Copy the original stream to a buffer, then wrap a
//memory stream around it. This way we can avoid
//locking the file
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
MemoryStream ms = new MemoryStream(buffer);
return Image.FromStream(ms);
}
///
///
///
/// Paints a representative value of the given object to the provided
/// canvas. Painting should be done within the boundaries of the
/// provided rectangle.
///
///
[SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers")] //Benign code
[SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")] // everything in this assembly is full trust.
public override void PaintValue(PaintValueEventArgs e) {
Image image = e.Value as Image;
if (image != null) {
Rectangle r = e.Bounds;
r.Width --;
r.Height--;
e.Graphics.DrawRectangle(SystemPens.WindowFrame, r);
e.Graphics.DrawImage(image, e.Bounds);
}
}
}
}
// 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
- CommandHelpers.cs
- PageParserFilter.cs
- SharedTcpTransportManager.cs
- ColorBuilder.cs
- AssemblyNameProxy.cs
- RowSpanVector.cs
- XmlSerializerAssemblyAttribute.cs
- ThreadStateException.cs
- Events.cs
- TrackingServices.cs
- ParseChildrenAsPropertiesAttribute.cs
- WmlPhoneCallAdapter.cs
- ParameterElementCollection.cs
- HuffCodec.cs
- Block.cs
- PackageFilter.cs
- WebResourceAttribute.cs
- DataGridViewTextBoxEditingControl.cs
- MembershipPasswordException.cs
- Matrix3DValueSerializer.cs
- GridViewColumnCollection.cs
- DetailsView.cs
- CultureInfoConverter.cs
- FactoryMaker.cs
- SettingsBindableAttribute.cs
- TemplateControlParser.cs
- New.cs
- IssuedSecurityTokenProvider.cs
- HwndSource.cs
- HwndTarget.cs
- ModelPropertyCollectionImpl.cs
- GacUtil.cs
- SparseMemoryStream.cs
- CodeMethodReturnStatement.cs
- SqlRowUpdatingEvent.cs
- ChangeConflicts.cs
- InlinedAggregationOperatorEnumerator.cs
- HtmlToClrEventProxy.cs
- StaticSiteMapProvider.cs
- UdpMessageProperty.cs
- TileBrush.cs
- SuppressMergeCheckAttribute.cs
- SqlParameterCollection.cs
- AttributeEmitter.cs
- SessionPageStatePersister.cs
- DataGridViewImageColumn.cs
- MoveSizeWinEventHandler.cs
- CharEntityEncoderFallback.cs
- DynamicDataRouteHandler.cs
- WindowsToolbar.cs
- AutoGeneratedFieldProperties.cs
- Brush.cs
- DataTableMappingCollection.cs
- DataServiceException.cs
- FieldMetadata.cs
- ParallelForEach.cs
- RuleDefinitions.cs
- LoadItemsEventArgs.cs
- PauseStoryboard.cs
- XmlWhitespace.cs
- MenuItemStyleCollection.cs
- ChtmlPhoneCallAdapter.cs
- Win32Interop.cs
- BinaryObjectWriter.cs
- FunctionUpdateCommand.cs
- InfoCardBinaryReader.cs
- SqlSupersetValidator.cs
- SimpleType.cs
- DataMisalignedException.cs
- DesignBindingPropertyDescriptor.cs
- UnsafeNativeMethods.cs
- CustomDictionarySources.cs
- MessageDecoder.cs
- SchemaCollectionCompiler.cs
- DataGridCellInfo.cs
- BindingNavigator.cs
- CryptographicAttribute.cs
- ScriptReferenceBase.cs
- ExclusiveCanonicalizationTransform.cs
- DocComment.cs
- Utilities.cs
- NameValueCollection.cs
- SQLSingleStorage.cs
- LinkAreaEditor.cs
- PolyQuadraticBezierSegment.cs
- WebBrowser.cs
- FactoryGenerator.cs
- EnterpriseServicesHelper.cs
- OrderedDictionary.cs
- VirtualizedItemPattern.cs
- ConnectionInterfaceCollection.cs
- Triplet.cs
- SettingsContext.cs
- HttpListenerPrefixCollection.cs
- Literal.cs
- DataGridRow.cs
- Brush.cs
- DependencyPropertyDescriptor.cs
- CodeParameterDeclarationExpression.cs
- HostingPreferredMapPath.cs