Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2817-BinPackingSpeedup/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Diagram elements/Shapes/ImageShape.cs

Last change on this file was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 6.4 KB
Line 
1using System;
2using System.Diagnostics;
3using System.Drawing;
4using System.IO;
5using System.Reflection;
6using System.Windows.Forms;
7
8namespace 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  }
208
209}
210
211
212
213
214
215
216
217
Note: See TracBrowser for help on using the repository browser.