1 //-------------------------------------------------------------
2 // <copyright company=’Microsoft Corporation’>
3 // Copyright © Microsoft Corporation. All Rights Reserved.
5 //-------------------------------------------------------------
6 // @owner=alexgor, deliant
7 //=================================================================
8 // File: ImageAnnotation.cs
10 // Namespace: System.Web.UI.WebControls[Windows.Forms].Charting
12 // Classes: ImageAnnotation
14 // Purpose: Image annotation classes.
18 //===================================================================
20 #region Used namespace
22 using System
.Collections
;
23 using System
.Collections
.Specialized
;
24 using System
.ComponentModel
;
25 using System
.ComponentModel
.Design
;
28 using System
.Drawing
.Design
;
29 using System
.Drawing
.Text
;
30 using System
.Drawing
.Drawing2D
;
32 using System
.Windows
.Forms
.DataVisualization
.Charting
;
33 using System
.Windows
.Forms
.DataVisualization
.Charting
.Data
;
34 using System
.Windows
.Forms
.DataVisualization
.Charting
.ChartTypes
;
35 using System
.Windows
.Forms
.DataVisualization
.Charting
.Utilities
;
36 using System
.Windows
.Forms
.DataVisualization
.Charting
.Borders3D
;
41 using System
.Web
.UI
.DataVisualization
.Charting
;
42 using System
.Web
.UI
.DataVisualization
.Charting
.Data
;
43 using System
.Web
.UI
.DataVisualization
.Charting
.Utilities
;
44 using System
.Web
.UI
.DataVisualization
.Charting
.Borders3D
;
51 namespace System
.Windows
.Forms
.DataVisualization
.Charting
54 namespace System
.Web
.UI
.DataVisualization
.Charting
59 /// <b>ImageAnnotation</b> is a class that represents an image annotation.
62 SRDescription("DescriptionAttributeImageAnnotation_ImageAnnotation"),
65 [AspNetHostingPermission(System
.Security
.Permissions
.SecurityAction
.InheritanceDemand
, Level
= AspNetHostingPermissionLevel
.Minimal
)]
66 [AspNetHostingPermission(System
.Security
.Permissions
.SecurityAction
.LinkDemand
, Level
= AspNetHostingPermissionLevel
.Minimal
)]
68 public class ImageAnnotation
: Annotation
72 // Annotation image name
73 private string _imageName
= String
.Empty
;
75 // Image wrapping mode
76 private ChartImageWrapMode _imageWrapMode
= ChartImageWrapMode
.Scaled
;
78 // Image transparent color
79 private Color _imageTransparentColor
= Color
.Empty
;
83 #region Construction and Initialization
86 /// Default public constructor.
88 public ImageAnnotation()
97 #region Image properties
100 /// Gets or sets the name of an annotation's image.
101 /// <seealso cref="ImageTransparentColor"/>
104 /// A string value representing the name of an annotation's image.
107 /// The name can be a file name, URL for the web control or a name from
108 /// the <see cref="NamedImagesCollection"/> class.
111 SRCategory("CategoryAttributeImage"),
114 Editor(Editors
.ImageValueEditor
.Editor
, Editors
.ImageValueEditor
.Base
),
115 SRDescription("DescriptionAttributeImageAnnotation_Image"),
117 public virtual string Image
132 /// Gets or sets the drawing mode of the image.
135 /// A <see cref="ChartImageWrapMode"/> value that defines the drawing mode of the image.
138 SRCategory("CategoryAttributeImage"),
140 DefaultValue(ChartImageWrapMode
.Scaled
),
141 SRDescription("DescriptionAttributeImageWrapMode"),
143 public ChartImageWrapMode ImageWrapMode
147 return _imageWrapMode
;
151 _imageWrapMode
= value;
157 /// Gets or sets a color which will be replaced with a transparent color while drawing the image.
160 /// A <see cref="Color"/> value which will be replaced with a transparent color while drawing the image.
163 SRCategory("CategoryAttributeImage"),
165 DefaultValue(typeof(Color
), ""),
166 SRDescription("DescriptionAttributeImageTransparentColor"),
167 TypeConverter(typeof(ColorConverter
)),
168 Editor(Editors
.ChartColorEditor
.Editor
, Editors
.ChartColorEditor
.Base
)
170 public Color ImageTransparentColor
174 return _imageTransparentColor
;
178 _imageTransparentColor
= value;
184 /// Gets or sets an annotation's content alignment.
187 /// A <see cref="ContentAlignment"/> value that represents the content alignment.
190 /// This property is used to align text for <see cref="TextAnnotation"/>, <see cref="RectangleAnnotation"/>,
191 /// <see cref="EllipseAnnotation"/> and <see cref="CalloutAnnotation"/> objects, and to align
192 /// a non-scaled image inside an <see cref="ImageAnnotation"/> object.
195 SRCategory("CategoryAttributeImage"),
196 DefaultValue(typeof(ContentAlignment
), "MiddleCenter"),
197 SRDescription("DescriptionAttributeImageAnnotation_Alignment"),
199 override public ContentAlignment Alignment
203 return base.Alignment
;
207 base.Alignment
= value;
213 /// Gets or sets an annotation's text style.
214 /// <seealso cref="Font"/>
215 /// <seealso cref="ForeColor"/>
218 /// A <see cref="TextStyle"/> value used to draw an annotation's text.
221 [EditorBrowsable(EditorBrowsableState
.Never
)]
222 public override TextStyle TextStyle
226 return base.TextStyle
;
230 base.TextStyle
= value;
234 #endregion // Image properties
239 /// Gets or sets an annotation's type name.
242 /// This property is used to get the name of each annotation type
243 /// (e.g. Line, Rectangle, Ellipse).
245 /// This property is for internal use and is hidden at design and run time.
249 SRCategory("CategoryAttributeMisc"),
252 EditorBrowsableAttribute(EditorBrowsableState
.Never
),
253 DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility
.Hidden
),
254 SerializationVisibilityAttribute(SerializationVisibility
.Hidden
),
255 SRDescription("DescriptionAttributeAnnotationType"),
257 public override string AnnotationType
266 /// Gets or sets annotation selection points style.
269 /// A <see cref="SelectionPointsStyle"/> value that represents annotation
273 /// This property is for internal use and is hidden at design and run time.
276 SRCategory("CategoryAttributeAppearance"),
277 DefaultValue(SelectionPointsStyle
.Rectangle
),
278 ParenthesizePropertyNameAttribute(true),
280 EditorBrowsableAttribute(EditorBrowsableState
.Never
),
281 DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility
.Hidden
),
282 SerializationVisibilityAttribute(SerializationVisibility
.Hidden
),
283 SRDescription("DescriptionAttributeSelectionPointsStyle"),
285 override internal SelectionPointsStyle SelectionPointsStyle
289 return SelectionPointsStyle
.Rectangle
;
295 #region Non Applicable Annotation Appearance Attributes (set as Non-Browsable)
298 /// Not applicable to this type of annotation.
299 /// <seealso cref="Font"/>
302 /// A <see cref="Color"/> value.
305 SRCategory("CategoryAttributeAppearance"),
307 DefaultValue(typeof(Color
), "Black"),
308 TypeConverter(typeof(ColorConverter
)),
309 Editor(Editors
.ChartColorEditor
.Editor
, Editors
.ChartColorEditor
.Base
)
311 override public Color ForeColor
315 return base.ForeColor
;
319 base.ForeColor
= value;
324 /// Not applicable to this type of annotation.
327 /// A <see cref="Font"/> object.
330 SRCategory("CategoryAttributeAppearance"),
332 DefaultValue(typeof(Font
), "Microsoft Sans Serif, 8pt"),
334 override public Font Font
347 /// Not applicable to this annotation type.
350 SRCategory("CategoryAttributeAppearance"),
352 DefaultValue(typeof(Color
), ""),
353 NotifyParentPropertyAttribute(true),
354 TypeConverter(typeof(ColorConverter
)),
355 Editor(Editors
.ChartColorEditor
.Editor
, Editors
.ChartColorEditor
.Base
)
357 override public Color BackColor
361 return base.BackColor
;
365 base.BackColor
= value;
370 /// Not applicable to this annotation type.
373 SRCategory("CategoryAttributeAppearance"),
375 DefaultValue(ChartHatchStyle
.None
),
376 NotifyParentPropertyAttribute(true),
377 Editor(Editors
.HatchStyleEditor
.Editor
, Editors
.HatchStyleEditor
.Base
)
379 override public ChartHatchStyle BackHatchStyle
383 return base.BackHatchStyle
;
387 base.BackHatchStyle
= value;
392 /// Not applicable to this annotation type.
395 SRCategory("CategoryAttributeAppearance"),
397 DefaultValue(GradientStyle
.None
),
398 NotifyParentPropertyAttribute(true),
399 Editor(Editors
.GradientEditor
.Editor
, Editors
.GradientEditor
.Base
)
401 override public GradientStyle BackGradientStyle
405 return base.BackGradientStyle
;
409 base.BackGradientStyle
= value;
414 /// Not applicable to this annotation type.
417 SRCategory("CategoryAttributeAppearance"),
419 DefaultValue(typeof(Color
), ""),
420 NotifyParentPropertyAttribute(true),
421 TypeConverter(typeof(ColorConverter
)),
422 Editor(Editors
.ChartColorEditor
.Editor
, Editors
.ChartColorEditor
.Base
)
424 override public Color BackSecondaryColor
428 return base.BackSecondaryColor
;
432 base.BackSecondaryColor
= value;
437 /// Not applicable to this annotation type.
440 SRCategory("CategoryAttributeAppearance"),
442 DefaultValue(typeof(Color
), "Black"),
443 TypeConverter(typeof(ColorConverter
)),
444 Editor(Editors
.ChartColorEditor
.Editor
, Editors
.ChartColorEditor
.Base
)
446 override public Color LineColor
450 return base.LineColor
;
454 base.LineColor
= value;
459 /// Not applicable to this annotation type.
462 SRCategory("CategoryAttributeAppearance"),
465 SRDescription("DescriptionAttributeLineWidth"),
467 override public int LineWidth
471 return base.LineWidth
;
475 base.LineWidth
= value;
480 /// Not applicable to this annotation type.
483 SRCategory("CategoryAttributeAppearance"),
485 DefaultValue(ChartDashStyle
.Solid
),
486 SRDescription("DescriptionAttributeLineDashStyle"),
488 override public ChartDashStyle LineDashStyle
492 return base.LineDashStyle
;
496 base.LineDashStyle
= value;
510 /// Paints the annotation object on the specified graphics.
512 /// <param name="graphics">
513 /// A <see cref="ChartGraphics"/> object, used to paint the annotation object.
515 /// <param name="chart">
516 /// Reference to the <see cref="Chart"/> owner control.
518 override internal void Paint(Chart chart
, ChartGraphics graphics
)
520 // Get annotation position in relative coordinates
521 PointF firstPoint
= PointF
.Empty
;
522 PointF anchorPoint
= PointF
.Empty
;
523 SizeF size
= SizeF
.Empty
;
524 GetRelativePosition(out firstPoint
, out size
, out anchorPoint
);
525 PointF secondPoint
= new PointF(firstPoint
.X
+ size
.Width
, firstPoint
.Y
+ size
.Height
);
527 // Create selection rectangle
528 RectangleF selectionRect
= new RectangleF(firstPoint
, new SizeF(secondPoint
.X
- firstPoint
.X
, secondPoint
.Y
- firstPoint
.Y
));
531 RectangleF rectanglePosition
= new RectangleF(selectionRect
.Location
, selectionRect
.Size
);
532 if(rectanglePosition
.Width
< 0)
534 rectanglePosition
.X
= rectanglePosition
.Right
;
535 rectanglePosition
.Width
= -rectanglePosition
.Width
;
537 if(rectanglePosition
.Height
< 0)
539 rectanglePosition
.Y
= rectanglePosition
.Bottom
;
540 rectanglePosition
.Height
= -rectanglePosition
.Height
;
543 // Check if position is valid
544 if( float.IsNaN(rectanglePosition
.X
) ||
545 float.IsNaN(rectanglePosition
.Y
) ||
546 float.IsNaN(rectanglePosition
.Right
) ||
547 float.IsNaN(rectanglePosition
.Bottom
) )
552 if(this.Common
.ProcessModePaint
)
554 // Draw "empty" image at design time
555 if(this._imageName
.Length
== 0 && this.Chart
.IsDesignMode() )
557 graphics
.FillRectangleRel(
563 this._imageTransparentColor
,
564 GetImageAlignment(this.Alignment
),
565 this.BackGradientStyle
,
566 this.BackSecondaryColor
,
572 PenAlignment
.Center
);
575 using( Brush textBrush
= new SolidBrush(this.ForeColor
) )
577 using (StringFormat format
= new StringFormat(StringFormat
.GenericTypographic
))
579 format
.Alignment
= StringAlignment
.Center
;
580 format
.LineAlignment
= StringAlignment
.Center
;
581 format
.FormatFlags
= StringFormatFlags
.LineLimit
;
582 format
.Trimming
= StringTrimming
.EllipsisCharacter
;
583 graphics
.DrawStringRel(
595 graphics
.FillRectangleRel(
601 this._imageTransparentColor
,
602 GetImageAlignment(this.Alignment
),
603 this.BackGradientStyle
,
610 PenAlignment
.Center
);
614 if(this.Common
.ProcessModeRegions
)
617 this.Common
.HotRegionsList
.AddHotRegion(
619 ReplaceKeywords(this.ToolTip
),
620 #if Microsoft_CONTROL
624 #else // Microsoft_CONTROL
625 ReplaceKeywords(this.Url
),
626 ReplaceKeywords(this.MapAreaAttributes
),
627 ReplaceKeywords(this.PostBackValue
),
628 #endif // Microsoft_CONTROL
630 ChartElementType
.Annotation
,
634 // Paint selection handles
635 PaintSelectionHandles(graphics
, selectionRect
, null);
639 /// Coverts ContentAlignment enumeration to ChartImageAlignmentStyle enumeration.
641 /// <param name="alignment">Content alignment.</param>
642 /// <returns>Image content alignment.</returns>
643 private ChartImageAlignmentStyle
GetImageAlignment(ContentAlignment alignment
)
645 if(alignment
== ContentAlignment
.TopLeft
)
647 return ChartImageAlignmentStyle
.TopLeft
;
649 else if(alignment
== ContentAlignment
.TopCenter
)
651 return ChartImageAlignmentStyle
.Top
;
653 else if(alignment
== ContentAlignment
.TopRight
)
655 return ChartImageAlignmentStyle
.TopRight
;
657 else if(alignment
== ContentAlignment
.MiddleRight
)
659 return ChartImageAlignmentStyle
.Right
;
661 else if(alignment
== ContentAlignment
.BottomRight
)
663 return ChartImageAlignmentStyle
.BottomRight
;
665 else if(alignment
== ContentAlignment
.BottomCenter
)
667 return ChartImageAlignmentStyle
.Bottom
;
669 else if(alignment
== ContentAlignment
.BottomLeft
)
671 return ChartImageAlignmentStyle
.BottomLeft
;
673 else if(alignment
== ContentAlignment
.MiddleLeft
)
675 return ChartImageAlignmentStyle
.Left
;
677 return ChartImageAlignmentStyle
.Center
;
680 #endregion // Painting
685 /// Gets text annotation content size based on the text and font.
687 /// <returns>Annotation content position.</returns>
688 override internal RectangleF
GetContentPosition()
691 if(this.Image
.Length
> 0)
693 // Try loading image and getting its size
696 if(this.Chart
!= null)
698 ImageLoader imageLoader
= this.Common
.ImageLoader
;
700 if(imageLoader
!= null)
702 ChartGraphics chartGraphics
= this.GetGraphics();
704 if (chartGraphics
!= null)
706 SizeF absSize
= new SizeF();
708 if (imageLoader
.GetAdjustedImageSize(this.Image
, chartGraphics
.Graphics
, ref absSize
))
710 SizeF imageSize
= chartGraphics
.GetRelativeSize(absSize
);
711 return new RectangleF(float.NaN
, float.NaN
, imageSize
.Width
, imageSize
.Height
);
717 catch(ArgumentException
)
719 // ArgumentException is thrown by LoadImage in certain situations when it can't load the image
723 return new RectangleF(float.NaN
, float.NaN
, float.NaN
, float.NaN
);