Exemple #1
0
        public static Texture2D DrawText(string text, TextDescription description, SizeF size=default(SizeF), bool isHighlighted = false, bool isSelected=false)
        {
            //Our return value.
            Texture2D t;

            using (Font font = description.ToFont())
            {
                //The size of the rendered string.
                SizeF strsize;

                //Create a dummy GDI object so we can figure out the size of the string...
                using (Bitmap dummyimage = new Bitmap(1, 1))
                {
                    using (System.Drawing.Graphics dummygraphics = System.Drawing.Graphics.FromImage(dummyimage))
                    {
                        //Measure the string...
                        strsize = dummygraphics.MeasureString(text, font, PointF.Empty, StringFormat);

                        if (size.Width > 0  && strsize.Width > size.Width)
                        {
                            string[] lines = WrapText(text, size.Width,  font);
                            strsize = new SizeF(size.Width, strsize.Height * lines.Length);
                        }
                    }
                }

                //Now create the REAL Drawing objects to render the text with.
                using (Bitmap image = new Bitmap((int)strsize.Width, (int)strsize.Height, PixelFormat.Format32bppArgb))
                {
                    using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(image))
                    {
                        //Fill the rect with a transparent Color.
                        graphics.Clear(Color.FromArgb(0));

                        //Draw the text
                        Color textColor = (isHighlighted ? description.HighlightedColor :
                            (isSelected ? description.SelectedColor : description.Color)).ToColor();
                        using (Brush brush = new SolidBrush(textColor))
                        {
                            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                            graphics.SmoothingMode = SmoothingMode.HighQuality;

                            GraphicsPath path = new GraphicsPath();
                            //path.AddString(text, font.FontFamily, (int)description.FontStyle, description.Size,
                                //PointF.Empty, StringFormat);
                            path.AddString(text,font.FontFamily, (int)description.FontStyle, description.Size,
                                new RectangleF(PointF.Empty, strsize), StringFormat);
                            if (description.IsOutlined)
                                graphics.DrawPath(Pens.Black, path);
                            graphics.FillPath(brush, path);
                        }
                        t = ImageHelper.TextureFromBitmap(image);
                    }
                }
            }
            return t;
        }