Changeset 4068 for trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Diagram elements/Shapes/ImageShape.cs
- Timestamp:
- 07/22/10 00:44:01 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Diagram elements/Shapes/ImageShape.cs
r2768 r4068 1 1 using System; 2 using System.Collections;3 using System.ComponentModel;4 2 using System.Diagnostics; 5 3 using System.Drawing; 6 using System.Drawing.Drawing2D;7 using System.Drawing.Printing;8 4 using System.IO; 9 using System.Drawing.Design;10 using System.Runtime.CompilerServices;11 using System.Runtime.Serialization;12 using System.Runtime.Serialization.Formatters.Binary;13 5 using System.Reflection; 14 using System.Threading;15 6 using System.Windows.Forms; 16 7 17 namespace Netron.Diagramming.Core 18 { 19 /// <summary> 20 /// This shape allows you to use any picture as inside a rectangular area. 21 /// </summary> 22 public partial class ImageShape : SimpleShapeBase 23 { 24 #region Fields 25 26 // ------------------------------------------------------------------ 27 /// <summary> 28 /// Implementation of IVersion - the current version of 29 /// ImageShape. 30 /// </summary> 31 // ------------------------------------------------------------------ 32 protected const double imageShapeVersion = 1.0; 33 34 #region the connectors 35 private Connector cBottom, cLeft, cRight, cTop; 36 private string mImagePath; 37 private Image mImage; 38 #endregion 39 #endregion 40 41 #region Properties 42 43 // ------------------------------------------------------------------ 44 /// <summary> 45 /// Gets the current version. 46 /// </summary> 47 // ------------------------------------------------------------------ 48 public override double Version 49 { 50 get 51 { 52 return imageShapeVersion; 53 } 54 } 55 56 /// <summary> 57 /// Gets the friendly name of the entity to be displayed in the UI 58 /// </summary> 59 /// <value></value> 60 public override string EntityName 61 { 62 get 63 { 64 return "Image shape"; 65 } 66 } 67 /// <summary> 68 /// Gets or sets the image path. 69 /// </summary> 70 /// <value>The image path.</value> 71 public string ImagePath 72 { 73 get{return mImagePath;} 74 set{ 75 if(value==string.Empty) return; 76 if(File.Exists(value)) 77 { 78 try 79 { 80 mImage = Image.FromFile(value); 81 Transform(Rectangle.X, Rectangle.Y, mImage.Width, mImage.Height); 82 } 83 catch(Exception exc) 84 { 85 Trace.WriteLine(exc.Message); 86 } 87 mImagePath = value; 88 } 89 else 90 MessageBox.Show("The specified file does not exist.","Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); 91 } 92 } 93 94 /// <summary> 95 /// Gets or sets the image. 96 /// </summary> 97 /// <value>The image.</value> 98 public Image Image 99 { 100 get{return mImage;} 101 set{ 102 if(value==null) return; 103 mImage = value; 104 mImagePath = string.Empty; 105 Transform(Rectangle.X, Rectangle.Y, mImage.Width, mImage.Height); 106 } 107 } 108 #endregion 109 110 #region Constructor 111 112 /// <summary> 113 /// Constructor 114 /// </summary> 115 public ImageShape() : base() 116 { 117 } 118 /// <summary> 119 /// This is the default constructor of the class. 120 /// </summary> 121 public ImageShape(IModel site) : base(site) 122 { 123 } 124 125 #endregion 126 127 #region Methods 128 129 // ----------------------------------------------------------------- 130 /// <summary> 131 /// Initializes this instance. 132 /// </summary> 133 // ----------------------------------------------------------------- 134 protected override void Initialize() 135 { 136 base.Initialize(); 137 //the initial size 138 Transform(0, 0, 200, 50); 139 #region Connectors 140 cTop = new Connector(new Point((int)(Rectangle.Left + Rectangle.Width / 2), Rectangle.Top), Model); 141 cTop.Name = "Top connector"; 142 cTop.Parent = this; 143 Connectors.Add(cTop); 144 145 cRight = new Connector(new Point(Rectangle.Right, (int)(Rectangle.Top + Rectangle.Height / 2)), Model); 146 cRight.Name = "Right connector"; 147 cRight.Parent = this; 148 Connectors.Add(cRight); 149 150 cBottom = new Connector(new Point((int)(Rectangle.Left + Rectangle.Width / 2), Rectangle.Bottom), Model); 151 cBottom.Name = "Bottom connector"; 152 cBottom.Parent = this; 153 Connectors.Add(cBottom); 154 155 cLeft = new Connector(new Point(Rectangle.Left, (int)(Rectangle.Top + Rectangle.Height / 2)), Model); 156 cLeft.Name = "Left connector"; 157 cLeft.Parent = this; 158 Connectors.Add(cLeft); 159 #endregion 160 } 161 162 /// <summary> 163 /// Adds additional stuff to the shape's menu 164 /// </summary> 165 /// <returns>ToolStripItem[]</returns> 166 public override ToolStripItem[] Menu() 167 { 168 169 ToolStripMenuItem item = new ToolStripMenuItem( 170 "Reset image size"); 171 172 item.Click += new EventHandler(OnResetImageSize); 173 174 ToolStripItem[] items = new ToolStripItem[] { item }; 175 176 return items; 177 } 178 /// <summary> 179 /// Resets the original image size 180 /// </summary> 181 /// <param name="sender"></param> 182 /// <param name="e"></param> 183 private void OnResetImageSize(object sender, EventArgs e) 184 { 185 Transform(Rectangle.X, Rectangle.Y, mImage.Width, mImage.Height); 186 this.Invalidate(); 187 } 188 189 /// <summary> 190 /// Overrides the default bitmap used in the shape viewer 191 /// </summary> 192 /// <returns></returns> 193 public Bitmap GetThumbnail() 194 { 195 Bitmap bmp=null; 196 try 197 { 198 Stream stream=Assembly.GetExecutingAssembly().GetManifestResourceStream("Netron.GraphLib.BasicShapes.Resources.ImageShape.gif"); 199 200 bmp= Bitmap.FromStream(stream) as Bitmap; 201 stream.Close(); 202 stream=null; 203 } 204 catch(Exception exc) 205 { 206 Trace.WriteLine(exc.Message,"ImageShape.GetThumbnail"); 207 } 208 return bmp; 209 } 210 211 /// <summary> 212 /// Paints the shape of the person object in the plex. Here you can let your imagination go. 213 /// MAKE IT PERFORMANT, this is a killer method called 200.000 times a minute! 214 /// </summary> 215 /// <param name="g">The graphics canvas onto which to paint</param> 216 public override void Paint(Graphics g) 217 { 218 base.Paint(g); 219 //if(RecalculateSize) 220 //{ 221 // Rectangle = new RectangleF(new PointF(Rectangle.X,Rectangle.Y), 222 // g.MeasureString(this.Text,Font)); 223 // Rectangle = System.Drawing.RectangleF.Inflate(Rectangle,10,10); 224 // RecalculateSize = false; //very important! 225 //} 226 if(mImage==null) 227 { 228 g.FillRectangle(Brush, Rectangle); 229 StringFormat sf = new StringFormat(); 230 sf.Alignment = StringAlignment.Center; 231 g.DrawString("Image shape", ArtPalette.DefaultFont, Brushes.Black, Rectangle.X + (Rectangle.Width / 2), Rectangle.Y + 3, sf); 232 } 233 else 234 g.DrawImage(mImage, Rectangle); 235 } 236 #endregion 237 } 8 namespace Netron.Diagramming.Core { 9 /// <summary> 10 /// This shape allows you to use any picture as inside a rectangular area. 11 /// </summary> 12 public partial class ImageShape : SimpleShapeBase { 13 #region Fields 14 15 // ------------------------------------------------------------------ 16 /// <summary> 17 /// Implementation of IVersion - the current version of 18 /// ImageShape. 19 /// </summary> 20 // ------------------------------------------------------------------ 21 protected const double imageShapeVersion = 1.0; 22 23 #region the connectors 24 private Connector cBottom, cLeft, cRight, cTop; 25 private string mImagePath; 26 private Image mImage; 27 #endregion 28 #endregion 29 30 #region Properties 31 32 // ------------------------------------------------------------------ 33 /// <summary> 34 /// Gets the current version. 35 /// </summary> 36 // ------------------------------------------------------------------ 37 public override double Version { 38 get { 39 return imageShapeVersion; 40 } 41 } 42 43 /// <summary> 44 /// Gets the friendly name of the entity to be displayed in the UI 45 /// </summary> 46 /// <value></value> 47 public override string EntityName { 48 get { 49 return "Image shape"; 50 } 51 } 52 /// <summary> 53 /// Gets or sets the image path. 54 /// </summary> 55 /// <value>The image path.</value> 56 public string ImagePath { 57 get { return mImagePath; } 58 set { 59 if (value == string.Empty) return; 60 if (File.Exists(value)) { 61 try { 62 mImage = Image.FromFile(value); 63 Transform(Rectangle.X, Rectangle.Y, mImage.Width, mImage.Height); 64 } 65 catch (Exception exc) { 66 Trace.WriteLine(exc.Message); 67 } 68 mImagePath = value; 69 } else 70 MessageBox.Show("The specified file does not exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); 71 } 72 } 73 74 /// <summary> 75 /// Gets or sets the image. 76 /// </summary> 77 /// <value>The image.</value> 78 public Image Image { 79 get { return mImage; } 80 set { 81 if (value == null) return; 82 mImage = value; 83 mImagePath = string.Empty; 84 Transform(Rectangle.X, Rectangle.Y, mImage.Width, mImage.Height); 85 } 86 } 87 #endregion 88 89 #region Constructor 90 91 /// <summary> 92 /// Constructor 93 /// </summary> 94 public ImageShape() 95 : base() { 96 } 97 /// <summary> 98 /// This is the default constructor of the class. 99 /// </summary> 100 public ImageShape(IModel site) 101 : base(site) { 102 } 103 104 #endregion 105 106 #region Methods 107 108 // ----------------------------------------------------------------- 109 /// <summary> 110 /// Initializes this instance. 111 /// </summary> 112 // ----------------------------------------------------------------- 113 protected override void Initialize() { 114 base.Initialize(); 115 //the initial size 116 Transform(0, 0, 200, 50); 117 #region Connectors 118 cTop = new Connector(new Point((int)(Rectangle.Left + Rectangle.Width / 2), Rectangle.Top), Model); 119 cTop.Name = "Top connector"; 120 cTop.Parent = this; 121 Connectors.Add(cTop); 122 123 cRight = new Connector(new Point(Rectangle.Right, (int)(Rectangle.Top + Rectangle.Height / 2)), Model); 124 cRight.Name = "Right connector"; 125 cRight.Parent = this; 126 Connectors.Add(cRight); 127 128 cBottom = new Connector(new Point((int)(Rectangle.Left + Rectangle.Width / 2), Rectangle.Bottom), Model); 129 cBottom.Name = "Bottom connector"; 130 cBottom.Parent = this; 131 Connectors.Add(cBottom); 132 133 cLeft = new Connector(new Point(Rectangle.Left, (int)(Rectangle.Top + Rectangle.Height / 2)), Model); 134 cLeft.Name = "Left connector"; 135 cLeft.Parent = this; 136 Connectors.Add(cLeft); 137 #endregion 138 } 139 140 /// <summary> 141 /// Adds additional stuff to the shape's menu 142 /// </summary> 143 /// <returns>ToolStripItem[]</returns> 144 public override ToolStripItem[] Menu() { 145 146 ToolStripMenuItem item = new ToolStripMenuItem( 147 "Reset image size"); 148 149 item.Click += new EventHandler(OnResetImageSize); 150 151 ToolStripItem[] items = new ToolStripItem[] { item }; 152 153 return items; 154 } 155 /// <summary> 156 /// Resets the original image size 157 /// </summary> 158 /// <param name="sender"></param> 159 /// <param name="e"></param> 160 private void OnResetImageSize(object sender, EventArgs e) { 161 Transform(Rectangle.X, Rectangle.Y, mImage.Width, mImage.Height); 162 this.Invalidate(); 163 } 164 165 /// <summary> 166 /// Overrides the default bitmap used in the shape viewer 167 /// </summary> 168 /// <returns></returns> 169 public Bitmap GetThumbnail() { 170 Bitmap bmp = null; 171 try { 172 Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Netron.GraphLib.BasicShapes.Resources.ImageShape.gif"); 173 174 bmp = Bitmap.FromStream(stream) as Bitmap; 175 stream.Close(); 176 stream = null; 177 } 178 catch (Exception exc) { 179 Trace.WriteLine(exc.Message, "ImageShape.GetThumbnail"); 180 } 181 return bmp; 182 } 183 184 /// <summary> 185 /// Paints the shape of the person object in the plex. Here you can let your imagination go. 186 /// MAKE IT PERFORMANT, this is a killer method called 200.000 times a minute! 187 /// </summary> 188 /// <param name="g">The graphics canvas onto which to paint</param> 189 public override void Paint(Graphics g) { 190 base.Paint(g); 191 //if(RecalculateSize) 192 //{ 193 // Rectangle = new RectangleF(new PointF(Rectangle.X,Rectangle.Y), 194 // g.MeasureString(this.Text,Font)); 195 // Rectangle = System.Drawing.RectangleF.Inflate(Rectangle,10,10); 196 // RecalculateSize = false; //very important! 197 //} 198 if (mImage == null) { 199 g.FillRectangle(Brush, Rectangle); 200 StringFormat sf = new StringFormat(); 201 sf.Alignment = StringAlignment.Center; 202 g.DrawString("Image shape", ArtPalette.DefaultFont, Brushes.Black, Rectangle.X + (Rectangle.Width / 2), Rectangle.Y + 3, sf); 203 } else 204 g.DrawImage(mImage, Rectangle); 205 } 206 #endregion 207 } 238 208 239 209 } … … 245 215 246 216 247 217
Note: See TracChangeset
for help on using the changeset viewer.