Exemplo n.º 1
0
        public WriteableBitmap GetImageFromText(string text, int maxWidth = 800, int lineHeight = 0)
        {
            if (string.IsNullOrEmpty(text) || CharCount == 0)
            {
                return(null);
            }
            else
            {
                int             textWidth   = 0;
                int             actualWidth = 0;
                int             textLine    = 1;
                WriteableBitmap resultBmp   = null;


                foreach (char c in text)
                {
                    textWidth += BitmapCharDic[c].CharImage.PixelWidth;
                }

                if (textWidth <= maxWidth)
                {
                    resultBmp = GetSubStringBitmap(text, lineHeight);
                }

                else
                {
                    int i        = 0;
                    int j        = 0;
                    int minWidth = 0;
                    textWidth = 0;
                    int textHeight = LineHeight;
                    if (lineHeight > 0)
                    {
                        textHeight = lineHeight;
                    }
                    List <string> listSubString = new List <string>();
                    for (i = 0; i < text.Length; i++)
                    {
                        actualWidth = textWidth;
                        textWidth  += BitmapCharDic[text[i]].CharImage.PixelWidth;
                        if (textWidth > maxWidth)
                        {
                            string s = text.Substring(j, i - j - 1);
                            j = i - 1;
                            listSubString.Add(s);
                            if (actualWidth > minWidth)
                            {
                                minWidth = actualWidth;
                            }
                            textWidth = BitmapCharDic[text[i]].CharImage.PixelWidth;
                        }
                    }

                    string lastSubStr = text.Substring(j);
                    listSubString.Add(lastSubStr);
                    textLine = listSubString.Count;

                    resultBmp = new WriteableBitmap(minWidth, textLine * textHeight);

                    for (i = 0; i < listSubString.Count; i++)
                    {
                        string          s      = listSubString[i];
                        WriteableBitmap wrTmp  = GetSubStringBitmap(s, textHeight);
                        int             offset = (minWidth - wrTmp.PixelWidth) / 2;
                        BitmapUtils.PasteBitmap(ref resultBmp, offset, i * textHeight, wrTmp);
                    }
                }


                return(resultBmp);
            }
        }