Exemplo n.º 1
0
            /// <summary>
            /// Returns a A8R8G8B8 (pow2 sized) bitmap with the name of the object drawn in it.
            /// </summary>
            /// <param name="Object"></param>
            /// <returns></returns>
            public static Bitmap Get(ObjectBase Object)
            {
                // calculate size of object name
                SizeF textSize = MEASURE.MeasureString(Object.Name, FONT);

                // get to next greater power of two
                int width  = (int)MathUtil.NextPowerOf2((uint)Math.Ceiling(textSize.Width));
                int height = (int)MathUtil.NextPowerOf2((uint)Math.Ceiling(textSize.Height));

                // get color to use for name based on objectflags
                Color color = Color.FromArgb((int)NameColors.GetColorFor(Object.Flags));

                // create bitmap to draw on
                Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);

                // draw name into bitmap
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    // currently tuned for performance
                    g.InterpolationMode  = InterpolationMode.NearestNeighbor;
                    g.PixelOffsetMode    = PixelOffsetMode.Half;
                    g.SmoothingMode      = SmoothingMode.HighSpeed;
                    g.CompositingMode    = CompositingMode.SourceOver;
                    g.CompositingQuality = CompositingQuality.HighSpeed;
                    g.TextRenderingHint  = TextRenderingHint.SystemDefault;

                    // draw text
                    using (SolidBrush brush = new SolidBrush(color))
                    {
                        g.DrawString(Object.Name, FONT, brush, new RectangleF(0, 0, width, height), STRINGFORMAT);
                    }
                }

                return(bitmap);
            }
Exemplo n.º 2
0
        /// <summary>
        /// Returns a A8R8G8B8 (pow2 sized) bitmap with the name of the object drawn in it.
        /// </summary>
        /// <param name="Object"></param>
        /// <returns></returns>
        public static Bitmap GetBitmapForName(ObjectBase Object)
        {
            const int ESTIMATEDCHARWIDTH = 16;
            const int HEIGHT             = 32;

            int width = (int)MathUtil.NextPowerOf2((uint)(Object.Name.Length * ESTIMATEDCHARWIDTH));

            // get color to use for name based on objectflags
            Color color = Color.FromArgb((int)NameColors.GetColorFor(Object.Flags));

            // font to use
            Font font = new Font(FontFamily.GenericSansSerif, 20,
                                 FontStyle.Regular, GraphicsUnit.Pixel);

            // create bitmap to draw on
            Bitmap bitmap = new Bitmap(width, HEIGHT, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            // draw name into bitmap
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                string text = Object.Name;

                StringFormat format = new StringFormat();
                format.Alignment     = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;

                // draw text
                using (SolidBrush brush = new SolidBrush(color))
                {
                    g.Clear(Color.Transparent);
                    g.DrawString(text, font, brush, new RectangleF(0, 0, width, HEIGHT), format);
                }
            }

            return(bitmap);
        }