Sometimes when you develop an application you need a straightforward way to represent something which cannot easily be represented in an icon- a “Visual shortcut” if you will. When it comes to presenting information in controls like Listviews, you have a few options- fonts, colours, and, most prominently, Item Icons. As an example, you may have a list of files loaded from some other source. You could show the actual file icon for the icon in a listview, but depending on what the program is intended for, it may be more useful for the icon to be used to represent other information, such as the file not being present, or perhaps a different size from a specified size. In those cases it can be useful to be able to use indicator icons/images using coloured icons. A plain square works, but a small psuedo 3-D cartoon-style “sphere” often looks much better.
As per usual, this is primarily aimed at Windows Forms, since that is how I wrote the original. doing something similar in WPF is actually quite straightforward and can be done right in Blend via XAML. Really we are drawing three things- a main ellipse, which has the main colour and the shadow, a highlight ellipse, which adds a shine to the top, and finally a small “Specular” shine blob, to make the item appear shiny.
And, of course, the source code to do this all, wrapped into a neat package to create a bitmap of any size:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
public class GummyImage { public static Image GetGummyImage(Color usecolor, Size RectDrawSize) { Bitmap DrawGummy = new Bitmap(RectDrawSize.Width, RectDrawSize.Height); Graphics GummyCanvas = Graphics.FromImage(DrawGummy); GummyCanvas.SmoothingMode = SmoothingMode.HighQuality; GummyCanvas.InterpolationMode = InterpolationMode.Bicubic; //first ellipse. // Base ellipse Rectangle BaseEllipse = new Rectangle(new Point(0, 0), new Size(RectDrawSize.Width, RectDrawSize.Height)); Rectangle ReflectionEllipse = new Rectangle(BaseEllipse.Location, new Size(BaseEllipse.Size.Width - 2, BaseEllipse.Size.Height - 2)); GraphicsPath ReflectionPath = new GraphicsPath(FillMode.Winding); ReflectionPath.AddEllipse(ReflectionEllipse); PathGradientBrush ReflectionGradient = new PathGradientBrush(ReflectionPath); ReflectionGradient.CenterColor = usecolor; ReflectionGradient.SurroundColors = new[] { Color.FromArgb(255, Color.Black) }; ReflectionGradient.CenterPoint = new PointF((float)(BaseEllipse.Width / 1.5), BaseEllipse.Top - Convert.ToInt16(BaseEllipse.Height * 2)); Blend ReflectionBlend = new Blend(5); ReflectionBlend.Factors = new[] { 0.5f, 1.0f, 1.0f, 1.0f, 1.0f }; ReflectionBlend.Positions = new[] { 0.0f, 0.05f, 0.5f, 0.75f, 1.0f }; ReflectionGradient.Blend = ReflectionBlend; GummyCanvas.FillPath(ReflectionGradient, ReflectionPath); ReflectionGradient.Dispose(); ReflectionPath.Dispose(); // 1st highlight ellipse int HighlightWidth = Convert.ToInt16(ReflectionEllipse.Width * 1f); int HighlightHeight = Convert.ToInt16(ReflectionEllipse.Height * 0.6); int HighlightX = (ReflectionEllipse.Width / 2) - (HighlightWidth / 2); int HighlightY = ReflectionEllipse.Top + 1; Rectangle HighlightEllipse = new Rectangle ( new Point(HighlightX, HighlightY), new Size(HighlightWidth, HighlightHeight)); Color HighlightColour = Color.White; Color HighlightFade = Color.Transparent; LinearGradientBrush HighlightBrush = new LinearGradientBrush(HighlightEllipse, HighlightColour, HighlightFade, 90); HighlightBrush.WrapMode = WrapMode.TileFlipX; GummyCanvas.FillEllipse(HighlightBrush, HighlightEllipse); HighlightBrush.Dispose(); // 2nd hilite ellipse int Highlight2Width = Convert.ToInt16(ReflectionEllipse.Width * 0.3); int Highlight2Height = Convert.ToInt16(ReflectionEllipse.Height * 0.2); int Highlight2X = (ReflectionEllipse.Width / 2) - (Highlight2Width / 2); int Highlight2Y = ReflectionEllipse.Top + Convert.ToInt16(ReflectionEllipse.Height * 0.2); Rectangle Highlight2Ellipse = new Rectangle ( new Point(-(Highlight2Width / 2), -(Highlight2Height / 2)), new Size(Highlight2Width, Highlight2Height)); LinearGradientBrush Specular = new LinearGradientBrush(Highlight2Ellipse, HighlightColour, HighlightFade, 90, true); GummyCanvas.TranslateTransform(Highlight2X, Highlight2Y); GummyCanvas.RotateTransform(-25); GummyCanvas.FillEllipse(Specular, Highlight2Ellipse); Specular.Dispose(); return DrawGummy; } } |
Have something to say about this post? Comment!