locked
Need to set line-height for System.Drawing.Font RRS feed

  • Question

  • User-1134246250 posted

     Hey all,

    I'm have a few issues with setting line-height when generating images. I pass a string with a few line breaks in, and the resulting image's line-height is way to high. I need it alot tighter. Below is the code:

      

    double mth = Math.Ceiling(Convert.ToDouble(text.Length) / Convert.ToDouble(23));        
            double dblHeight = Math.Round(mth, 0);        
            //Initial height of image (height is a public var)
            height = (Convert.ToInt32(dblHeight) * 20);                
            Bitmap Bmp = new Bitmap(150, height);
            Graphics Graph = Graphics.FromImage(Bmp);
    
            for (int i = 0; i < dblHeight; i++)
            {
                if (text.Length > 23 * i)
                {
                    //Splits string into different segments
                    text = PageString(text, 23 );
                    text = text.Replace("|", Environment.NewLine);
                }
            }
    
            Graph.Clear(Color.White);
    
            Graph.TextRenderingHint = TextRenderingHint.AntiAlias;
            Graph.SmoothingMode = SmoothingMode.AntiAlias;
            
            //Colour brush to use for generator
            Brush textBrush = new SolidBrush(Color.FromArgb(255,55,63));
    
            //font to write as
            PrivateFontCollection pfc = new PrivateFontCollection();
            string appDir = HttpContext.Current.Server.MapPath("/Fonts/insig.ttf");
            pfc.AddFontFile(appDir);        
    
            FontFamily ff = pfc.Families[0];
    
            Font fnt = new Font(ff, 14, GraphicsUnit.Pixel);                        
    
            if (ff.IsStyleAvailable(FontStyle.Regular)) fnt = new Font(ff, 14, FontStyle.Regular, GraphicsUnit.Pixel);
    
            //Point to start at
            Point pnt = new Point(0, 0);
                    
            Graph.DrawString(text, fnt, textBrush, pnt);
            MemoryStream imgStream = new MemoryStream();
    
            try
            {
                Bmp.Save(imgStream, ImageFormat.Gif);
                Bmp.Dispose();
                Graph.Dispose();
            }
            catch
            {
                Bmp.Dispose();
                Graph.Dispose();
            } 
            return imgStream;
     
    Friday, January 30, 2009 2:25 AM

Answers

  • User-1134246250 posted

     Hey all. I managed to figure something out. I just split the string, then for called the DrawString method for each segment of the string.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 3, 2009 2:53 AM