Esempio n. 1
0
        /// <summary>
        /// 计算所需高(暂不考虑英文的word wrapper)
        /// </summary>
        /// <param name="width">目标宽度</param>
        /// <param name="pagePadding">页边距</param>
        /// <param name="lineDistance">行距</param>
        /// <param name="font">字体</param>
        /// <returns>分行完毕的文本</returns>
        private List<string> CalculateHeight(int width, int pagePadding, int lineDistance, PixelFont font, string text)
        {
            List<string> result = new List<string>();
            int availableWidth = width - pagePadding * 2;//除去页边距之后的可用宽度
            List<char> line = new List<char>();
            string[] lines = text.Split(new string[] {"\r\n" }, StringSplitOptions.None);
            int length = 0;
            foreach (var l in lines)
            {
                line.Clear();
                length = 0;
                foreach (var c in l)
                {
                    int len = font.MeasureCharDrawingLength(c);
                    if (len + length > availableWidth)
                    {
                        result.Add(new string(line.ToArray()));
                        line.Clear();
                        line.Add(c);
                        length = len;
                    }
                    else
                    {
                        length += len;
                        line.Add(c);
                    }
                }
                result.Add(new string(line.ToArray()));

            }
            return result;
        }
Esempio n. 2
0
 private Bitmap DrawImage(int width, int height, int pagePadding, int lineDistance, int fontSize)
 {
     int halfFontSize = fontSize / 2;
     string unicodeFontFileName = "songti-" + fontSize + "x" + fontSize + ".bin";
     string asciiFontFileName = "terminus-ascii-" + halfFontSize + "x" + fontSize + "-95chars.bmp";
     PixelFont font = new PixelFont(Application.StartupPath + "\\Resource\\" + unicodeFontFileName, Application.StartupPath + "\\Resource\\" + asciiFontFileName, Color.Black);
     List<string> lines = CalculateHeight(width, pagePadding, lineDistance, font, textBoxInputText.Text);
     Bitmap target = new Bitmap(width, lines.Count * lineDistance + 2 * pagePadding);
     Graphics g = Graphics.FromImage(target);
     g.Clear(Color.White);
     SolidBrush b = new SolidBrush(Color.Black);
     for(int i = 0; i < lines.Count;i ++)
     {
         font.DrawString(g, b, lines[i], pagePadding, pagePadding + i * lineDistance);
     }
     g.Dispose();
     return target;
 }