2 // System.Drawing.Design.ImageEditor.cs
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) 2003 Martin Willemoes Hansen
9 // (C) 2003 Andreas Nahr
14 using System.ComponentModel;
15 using System.Windows.Forms;
17 namespace System.Drawing.Design
19 public class ImageEditor : UITypeEditor
22 private OpenFileDialog openDialog;
28 public override object EditValue (ITypeDescriptorContext context,
29 IServiceProvider provider, object value)
31 openDialog = new OpenFileDialog();
32 // FIXME: Add multilanguage support
33 openDialog.Title = "Open image file";
34 openDialog.CheckFileExists = true;
35 openDialog.CheckPathExists = true;
36 openDialog.Filter = CreateFilterEntry (this);
37 openDialog.Multiselect = false;
40 DialogResult result = openDialog.ShowDialog();
42 // Check the result and create a new image from the file
43 if (result == DialogResult.OK)
45 return LoadFromStream (openDialog.OpenFile());
51 public override UITypeEditorEditStyle GetEditStyle (ITypeDescriptorContext context)
53 return UITypeEditorEditStyle.Modal;
56 public override bool GetPaintValueSupported (ITypeDescriptorContext context)
61 public override void PaintValue (PaintValueEventArgs e)
63 Graphics G = e.Graphics;
66 Image I = (Image) e.Value;
67 G.DrawImage (I, e.Bounds);
69 G.DrawRectangle (Pens.Black, e.Bounds);
72 protected static string CreateExtensionsString (string[] extensions, string sep)
74 if (extensions.Length > 0)
76 string Ext = extensions[0];
77 for (int x = 1; x < extensions.Length - 1; x++)
78 Ext = string.Concat(Ext, sep, extensions[x]);
87 protected static string CreateFilterEntry (ImageEditor e)
89 string ExtStr = CreateExtensionsString (e.GetExtensions(), ";");
90 string Desc = e.GetFileDialogDescription() + " (" + ExtStr + ")";
91 return String.Concat (Desc, "|", ExtStr);
94 protected virtual string[] GetExtensions()
96 return new string[] {"*.bmp", "*.gif", "*.jpg", "*.jpeg", "*.png", "*.ico", "*.emf", "*.wmf"};
99 protected virtual string GetFileDialogDescription()
101 // FIXME: Add multilanguage support
102 return "All image files";
105 protected virtual Image LoadFromStream (Stream stream)
107 return new Bitmap (stream);